Instantly share code, notes, and snippets.
Created Dec 13, 2016
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class GuardStateMachine : ByTheTale.StateMachine.MachineBehaviour | |
| { | |
| public override void AddStates() | |
| { | |
| AddState<PatrolState>(); | |
| AddState<IdleState>(); | |
| //AddState<PauseState>(); | |
| SetInitialState<PatrolState>(); | |
| } | |
| } | |
| public class StateGuard : ByTheTale.StateMachine.State | |
| { | |
| protected NavAgent navAgent; | |
| public override void Enter() | |
| { | |
| navAgent = GetMachine<GuardStateMachine>().GetComponent<NavAgent>(); | |
| // Resets Timer | |
| navAgent.ResetTimeSinceLastTransition(); | |
| } | |
| } | |
| public class PatrolState : StateGuard | |
| { | |
| public override void Enter() | |
| { | |
| Debug.Log("Entering PatrolState"); | |
| base.Enter(); | |
| navAgent.GetPatrol().GetComponent<Renderer>().material.color = Color.green; | |
| } | |
| public override void Execute() | |
| { | |
| // Checks if the timer is past wait time | |
| if ((navAgent.GetTimer()) > (navAgent.GetPatrolTime())) | |
| { | |
| // Changes to Idle state | |
| machine.ChangeState<IdleState>(); | |
| } | |
| //else if (Input.GetKeyDown(KeyCode.Alpha1)) | |
| //{ | |
| // machine.ChangeState<PauseState>(); | |
| // navAgent.GetPaused().GetComponent<Renderer>().material.color = Color.grey; | |
| //} | |
| } | |
| public override void Exit() | |
| { | |
| Debug.Log("Leaving PatrolState"); | |
| navAgent.GetPatrol().GetComponent<Renderer>().material.color = Color.green; | |
| } | |
| } | |
| public class IdleState : StateGuard | |
| { | |
| public override void Enter() | |
| { | |
| Debug.Log("Entering IdleState"); | |
| base.Enter(); | |
| navAgent.Stop(true); // I tried to get them to stop when they enter the IdleState, but it's giving me an error. Same for navAgent.Resume(); | |
| navAgent.GetPatrol().GetComponent<Renderer>().material.color = new Color32(16, 88, 0, 255); //Changes color to Dark Green | |
| } | |
| public override void Execute() | |
| { | |
| // Checks if the timer is past wait time | |
| if ((navAgent.GetTimer()) > (navAgent.GetPatrolTime())) | |
| { | |
| machine.ChangeState<PatrolState>(); | |
| } | |
| } | |
| public override void Exit() | |
| { | |
| Debug.Log("Leaving IdleState"); | |
| navAgent.Resume(); | |
| navAgent.GetPatrol().GetComponent<Renderer>().material.color = Color.green; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment