Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2016 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/acce725515cc9cf95f28ebaef7ac3881 to your computer and use it in GitHub Desktop.
Save anonymous/acce725515cc9cf95f28ebaef7ac3881 to your computer and use it in GitHub Desktop.
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