Untitled
unknown
csharp
a year ago
5.1 kB
6
Indexable
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using System; using Random = UnityEngine.Random; using FMODUnity; using FMOD.Studio; public class StateMachine : MonoBehaviour { private FMOD.Studio.EventInstance VOCAL; [SerializeField] private CapsuleCollider triggerCollider; [SerializeField] private ParticleSystem stunParticle; [Header("Abilities")] public float walkingSpeed = 3.2f; public float runningSpeed = 4.3f; [Serializable] public class ActionState { public Transform point; public bool rotateToThis; public float waitTime; } Dictionary<string, BaseState> stateDictionary = new Dictionary<string, BaseState>(); public List<ActionState> ActionStates = new List<ActionState>(); private BaseState currentState; private int currentAction = 0; public NavMeshAgent Agent => GetComponent<NavMeshAgent>(); [SerializeField] private Animator animator; //=> GetComponentInChildren<Animator>(); public FieldOfView FOV => GetComponentInChildren <FieldOfView>(); [Space(5)] [SerializeField] private Transform[] searchPoints; private void Awake() { stateDictionary.Add("Idle", new Idle(this)); stateDictionary.Add("Chase", new Chase(this)); stateDictionary.Add("Searching", new Searching(this)); stateDictionary.Add("Shout", new Shout(this)); stateDictionary.Add("Stun", new Stun(this)); stateDictionary.Add("Receive", new Receive(this)); stateDictionary.Add("Patrol", new Patrol(this)); stateDictionary.Add("WaitTo", new WaitTo(this)); stateDictionary.Add("RotateTo", new RotateTo(this)); stateDictionary.Add("GoTo", new GoTo(this)); } private void Start() { VOCAL = FMODUnity.RuntimeManager.CreateInstance("event:/SFX/VOCALS/Guard/freeze"); activeCollider(true, false); currentState = GetInitialState("Patrol"); if (currentState != null) { currentState.Enter(); } } private void Update() { if(currentState != null) { currentState.Update(); } } public void AddAction() { currentAction++; if(currentAction > ActionStates.Count -1) { currentAction = 0; } } public int GetCurrentAction() { return currentAction; } public void ChangeState(String stateName) { if(currentState != null) { currentState.Exit(); } currentState = stateDictionary[stateName]; if (stateName == "RotateTo" || stateName == "GoTo") { currentState.SetValues(ActionStates[currentAction].point, ActionStates[currentAction].rotateToThis, ActionStates[currentAction].waitTime); } currentState.Enter(); } protected virtual BaseState GetInitialState(String stateName) { BaseState newState; newState = stateDictionary[stateName]; return newState; } private void OnGUI() { string content = currentState != null ? currentState.name : "(no current state)"; GUILayout.Label($"<color='black'><size=40>){content}</size></color>"); } public Transform GetNextSearchPoint(int number) { return searchPoints[number]; } public int GetLengthOfSearchPoints() { return searchPoints.Length; } public void playSound(string charName, int track) { // VOCAL.setParameterByName("Guard", track); VOCAL.start(); } private void OnTriggerEnter(Collider other) { if(other.tag == "Player" && Game_Manager.Instance.isPlayerWanted) { Game_Manager.Instance.GameOver(); } if(other.tag == "Trap") { ChangeState("Stun"); Destroy(other.gameObject); } if(other.tag == "Slower" && !TryGetComponent<Slower>(out Slower slower)) { gameObject.AddComponent<Slower>(); } if(other.tag == "Freezer" && !TryGetComponent<Freezer>(out Freezer freezer)) { gameObject.AddComponent<Freezer>(); } } public void ChangeAnimationStatus(bool moving, float blend, string trigger) { animator.SetBool("isMoving", moving); animator.SetFloat("Blend", blend); if(trigger != null) { animator.SetTrigger(trigger); } } public void activeCollider(bool active, bool isTrigger) { triggerCollider.enabled = active; triggerCollider.isTrigger = isTrigger; } public void ReceiveMessage() { string content = currentState.name; if (content != "Chase" && content != "Stun" && content != "Receive") { ChangeState("Receive"); } } public void ActiveStunParticle() { stunParticle.gameObject.SetActive(true); stunParticle.Play(); } public void ChangeMovingSpeed(float speed) { Agent.speed = speed; } }
Editor is loading...
Leave a Comment