1
0
forked from cgvr/DeltaVR

refactor NPCController script, make it abstract, extendable; separate AlienNPC and CafeWaiterNPC

This commit is contained in:
2026-01-28 14:23:16 +02:00
parent eb8e33bb3d
commit a8e65514f4
15 changed files with 823 additions and 41 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8993dd48830472642b4b2c8a4c6e291c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea1b6453c1ef8e24d921a6dbaa549eba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8a215290e2c2e1d43983b61cb160e241
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: baa11cfc03dc213438961722257e2e94
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: