forked from cgvr/DeltaVR
refactor NPCController script, make it abstract, extendable; separate AlienNPC and CafeWaiterNPC
This commit is contained in:
8
Assets/_PROJECT/Scripts/ModeGeneration/NPCs.meta
Normal file
8
Assets/_PROJECT/Scripts/ModeGeneration/NPCs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8993dd48830472642b4b2c8a4c6e291c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
15
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/AlienNPC.cs
Normal file
15
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/AlienNPC.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class AlienNPC : NPCController
|
||||
{
|
||||
protected override void OnPlayerApproach()
|
||||
{
|
||||
Debug.Log("Alien NPC: player approached");
|
||||
AudioManager.Instance.PlayDialogue(voiceLineKeys[0], gameObject);
|
||||
}
|
||||
|
||||
protected override void OnPlayerLeave()
|
||||
{
|
||||
Debug.Log("Alien NPC: player left");
|
||||
}
|
||||
}
|
||||
11
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/AlienNPC.cs.meta
Normal file
11
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/AlienNPC.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea1b6453c1ef8e24d921a6dbaa549eba
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
16
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/CafeWaiterNPC.cs
Normal file
16
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/CafeWaiterNPC.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CafeWaiterNPC : NPCController
|
||||
{
|
||||
protected override void OnPlayerApproach()
|
||||
{
|
||||
AudioManager.Instance.PlayDialogue(voiceLineKeys[0], gameObject);
|
||||
}
|
||||
|
||||
protected override void OnPlayerLeave()
|
||||
{
|
||||
AudioManager.Instance.PlayDialogue(voiceLineKeys[1], gameObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a215290e2c2e1d43983b61cb160e241
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
99
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/NPCController.cs
Normal file
99
Assets/_PROJECT/Scripts/ModeGeneration/NPCs/NPCController.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class NPCController : MonoBehaviour
|
||||
{
|
||||
private Vector3 mouthClosedScale;
|
||||
private Vector3 mouthOpenScale;
|
||||
private bool isTalking;
|
||||
protected Transform playerTransform;
|
||||
|
||||
public Transform mouthTransform;
|
||||
public float mouthScalingMultiplier = 2.5f;
|
||||
public float mouthMovementDuration = 0.25f;
|
||||
|
||||
public string[] voiceLineKeys;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Awake()
|
||||
{
|
||||
mouthClosedScale = mouthTransform.localScale;
|
||||
mouthOpenScale = new Vector3(mouthClosedScale.x, mouthClosedScale.y * mouthScalingMultiplier, mouthClosedScale.z);
|
||||
|
||||
isTalking = false;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (playerTransform != null)
|
||||
{
|
||||
// As if player is on same Y coordinate as this object, to not rotate around Y axis.
|
||||
Vector3 lookTargetPos = new Vector3(playerTransform.position.x, transform.position.y, playerTransform.position.z);
|
||||
transform.LookAt(lookTargetPos);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.gameObject.tag == "Player Head")
|
||||
{
|
||||
playerTransform = other.transform;
|
||||
OnPlayerApproach();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnPlayerApproach() {}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (other.gameObject.tag == "Player Head")
|
||||
{
|
||||
playerTransform = null;
|
||||
OnPlayerLeave();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnPlayerLeave() {}
|
||||
|
||||
public void SpeakVoiceLine(int voiceLineId)
|
||||
{
|
||||
AudioManager.Instance.PlayDialogue(voiceLineKeys[voiceLineId], gameObject);
|
||||
}
|
||||
|
||||
public void StartTalking()
|
||||
{
|
||||
isTalking = true;
|
||||
MoveMouth();
|
||||
}
|
||||
|
||||
public void Stoptalking()
|
||||
{
|
||||
isTalking = false;
|
||||
}
|
||||
|
||||
|
||||
private void MoveMouth()
|
||||
{
|
||||
if (!isTalking)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (mouthTransform.localScale == mouthClosedScale)
|
||||
{
|
||||
mouthTransform.DOScale(mouthOpenScale, mouthMovementDuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
mouthTransform.DOScale(mouthClosedScale, mouthMovementDuration);
|
||||
}
|
||||
|
||||
Invoke("MoveMouth", mouthMovementDuration + 0.01f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: baa11cfc03dc213438961722257e2e94
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user