forked from cgvr/DeltaVR
refactor NPCController script, make it abstract, extendable; separate AlienNPC and CafeWaiterNPC
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user