GameVsJam/3d Prototyp/Assets/Animations/NPCAnimationController.cs

242 lines
8.3 KiB
C#
Raw Blame History

using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class NPCAnimationController : MonoBehaviour
{
public Transform workPosition;
public GameObject Cup;
public GameObject HandCup;
public Transform toiletTransform;
public Transform dancePlace;
public Transform toiletLookAtTransform;// Die Transform-Position der Toilette
private NavMeshAgent agent;
private Animator animator;
public Developer Developer;
[SerializeField]
private bool _canTakeNewJob;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
HandCup.SetActive(false);
StartCoroutine(GetToWorkRoutine());
}
private IEnumerator GetToWorkRoutine()
{
yield return TakeLock();
animator.SetLayerWeight(animator.GetLayerIndex("Typing"), 0);
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("WalkBack"));
MoveTo(workPosition.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
animator.ResetTrigger("Walk"); // Setze den Lauf-Trigger zur<75>ck
animator.SetTrigger("SitDown");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
transform.rotation = workPosition.rotation;
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 1, 1f));
ReleaseLock();
}
private IEnumerator FadeLayerWeight(int layerIndex, float targetWeight, float duration)
{
float time = 0;
float startWeight = animator.GetLayerWeight(layerIndex);
while (time < duration)
{
animator.SetLayerWeight(layerIndex, Mathf.Lerp(startWeight, targetWeight, time / duration));
time += Time.deltaTime;
yield return null;
}
animator.SetLayerWeight(layerIndex, targetWeight); // Sicherstellen, dass der Zielwert erreicht wird
}
public void DrinkCoffee()
{
StartCoroutine(DrinkCoffeeRoutine());
}
private IEnumerator TakeLock()
{
yield return new WaitUntil(() => _canTakeNewJob);
_canTakeNewJob = false;
}
private void ReleaseLock()
{
_canTakeNewJob = true;
}
private IEnumerator DrinkCoffeeRoutine()
{
yield return TakeLock();
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 1, 1f)); // 1 Sekunde zum Einblenden
animator.SetTrigger("DrinkCoffee");
yield return new WaitForSeconds(1.5f);
Cup.SetActive(false);
yield return new WaitForSeconds(1.5f);
HandCup.SetActive(true);
yield return new WaitForSeconds(3.5f);
HandCup.SetActive(false);
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 0, 1f)); // 1 Sekunde zum Ausblenden
animator.ResetTrigger("DrinkCoffee");
Cup.SetActive(true);
ReleaseLock();
}
public void GoToToilet()
{
StartCoroutine(GoToToiletRoutine());
}
private IEnumerator GoToToiletRoutine()
{
yield return TakeLock();
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 0, 1f));
while (ToiletManager.Instance.IsOccupied)
{
yield return new WaitForSeconds(2);
}
ToiletManager.Instance.IsOccupied = true;
// Beginne mit dem Laufen zur Toilette.
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Walk"));
MoveTo(toiletTransform.position);
// Warte, bis der Charakter am Ziel angekommen ist.
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
// H<>rt auf zu laufen und beginnt zu sitzen.
animator.ResetTrigger("Walk"); // Es ist wichtig, den vorherigen Trigger zur<75>ckzusetzen
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
animator.SetTrigger("SitDown");
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Ready"));
Developer.Pee(1.0, true);
// Stehe auf und gehe zur<75>ck zum Ausgangspunkt.
transform.LookAt(new Vector3(toiletLookAtTransform.position.x, toiletLookAtTransform.position.y, toiletLookAtTransform.position.z));
animator.ResetTrigger("SitDown"); // Setze den Sitzen-Trigger zur<75>ck
ToiletManager.Instance.IsOccupied = false;
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("WalkBack"));
MoveTo(workPosition.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
animator.ResetTrigger("Walk"); // Setze den Lauf-Trigger zur<75>ck
animator.SetTrigger("SitDown");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
transform.rotation = workPosition.rotation;
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 1, 1f));
ReleaseLock();
}
// Hier kannst du entscheiden, ob der Charakter wieder sitzt oder steht.
// Beispiel: Setze IsSitting oder IsStanding entsprechend.
public void GettingMad()
{
StartCoroutine(GettingMadRoutine());
}
private IEnumerator GettingMadRoutine()
{
yield return TakeLock();
animator.SetTrigger("GetMad");
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 1, 2f)); // 1 Sekunde zum Einblenden
yield return new WaitForSeconds(8f);
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Coffee"), 0, 1f)); // 1 Sekunde zum Ausblenden
animator.ResetTrigger("GetMad");
ReleaseLock();
}
public void CaffeinOverdose()
{
StartCoroutine(CaffeinRoutine());
}
private IEnumerator CaffeinRoutine()
{
yield return TakeLock();
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 0, 1f));
// Beginne mit dem Laufen zur Toilette.
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Walk"));
MoveTo(dancePlace.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
animator.ResetTrigger("Walk");
animator.SetTrigger("TooMuchCaffein");
yield return new WaitWhile(() => Developer.IsOvercaffeinated);
animator.ResetTrigger("TooMuchCaffein");
animator.SetTrigger("Walk");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("WalkBack"));
MoveTo(workPosition.position);
yield return new WaitUntil(() => agent.remainingDistance <= agent.stoppingDistance);
transform.rotation = workPosition.rotation;
animator.ResetTrigger("Walk"); // Setze den Lauf-Trigger zur<75>ck
animator.SetTrigger("SitDown");
yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).IsName("Sit"));
yield return StartCoroutine(FadeLayerWeight(animator.GetLayerIndex("Typing"), 1, 1f));
ReleaseLock();
}
private void MoveTo(Vector3 destination)
{
agent.destination = destination;
}
private void Update()
{
// Pr<50>ft, ob der Buchstabe 'C' gedr<64>ckt wurde
if (Input.GetKeyDown(KeyCode.C))
{
DrinkCoffee();
}
// Pr<50>ft, ob der Buchstabe 'T' gedr<64>ckt wurde
if (Input.GetKeyDown(KeyCode.T))
{
GoToToilet();
}
// Pr<50>ft, ob der Buchstabe 'M' gedr<64>ckt wurde
if (Input.GetKeyDown(KeyCode.M))
{
GettingMad();
}
// Pr<50>ft, ob der Buchstabe 'C' gedr<64>ckt wurde
if (Input.GetKeyDown(KeyCode.O))
{
CaffeinOverdose();
}
}
}