add unity project
add FSM examples
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace.FSM
|
||||
{
|
||||
public class ChasePlayerState : IState
|
||||
{
|
||||
private GameObject _owner;
|
||||
private PlayerController _player;
|
||||
|
||||
public ChasePlayerState(GameObject owner, PlayerController player)
|
||||
{
|
||||
_owner = owner;
|
||||
_player = player;
|
||||
}
|
||||
|
||||
public void Enter(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("CHASE: Enter");
|
||||
}
|
||||
|
||||
public void Update(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("CHASE: Update");
|
||||
|
||||
// player visibility check
|
||||
if (Physics.Raycast(_owner.transform.position, _owner.transform.forward, out RaycastHit hit))
|
||||
{
|
||||
if (hit.transform.GetComponent<PlayerController>() != _player)
|
||||
{
|
||||
ctx.SetState(new IdleState(_owner, _player, 60.0f));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx.SetState(new IdleState(_owner, _player, 60.0f));
|
||||
}
|
||||
}
|
||||
|
||||
public void Exit(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("CHASE: Exit");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7b0823b31e24c109b472e77ec48bf3b
|
||||
timeCreated: 1744128282
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace.FSM
|
||||
{
|
||||
public class ChaseSystem : MonoBehaviour, IFiniteStateMachine
|
||||
{
|
||||
[SerializeField] private IState _defaultState;
|
||||
[SerializeField] private PlayerController _player;
|
||||
|
||||
private IState _currentState = null;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// _currentState = _defaultState; // isn't working, because an interface can't be serialized by the Unity Serializer
|
||||
_currentState = new IdleState(gameObject, _player, 60.0f);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_currentState?.Enter(this);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// if (_currentState == null) return;
|
||||
_currentState?.Update(this);
|
||||
}
|
||||
|
||||
public void SetState(IState state)
|
||||
{
|
||||
_currentState.Exit(this);
|
||||
_currentState = state;
|
||||
_currentState.Enter(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99c993fa1e7746fa8c1f19ca160390c5
|
||||
timeCreated: 1744126665
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace DefaultNamespace.FSM
|
||||
{
|
||||
public interface IFiniteStateMachine
|
||||
{
|
||||
void SetState(IState state);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3f7abcc3bb1468dacaf26771b1a8641
|
||||
timeCreated: 1744127058
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace DefaultNamespace.FSM
|
||||
{
|
||||
public interface IState
|
||||
{
|
||||
void Enter(IFiniteStateMachine ctx);
|
||||
// IState Update(); // option 1 next state as return value
|
||||
void Update(IFiniteStateMachine ctx); // option 2 we call setState when we want to change
|
||||
void Exit(IFiniteStateMachine ctx);
|
||||
}
|
||||
|
||||
// alternative
|
||||
// public interface IStateEnter
|
||||
// {
|
||||
// void Enter(IFiniteStateMachine ctx);
|
||||
// }
|
||||
//
|
||||
// public interface IStateUpdate
|
||||
// {
|
||||
// void Update(IFiniteStateMachine ctx);
|
||||
// }
|
||||
//
|
||||
// public interface IStateExit
|
||||
// {
|
||||
// void Exit(IFiniteStateMachine ctx);
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 575dd241edb84a3492106b17ba18fad8
|
||||
timeCreated: 1744126750
|
||||
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace.FSM
|
||||
{
|
||||
public class IdleState : IState
|
||||
{
|
||||
private GameObject _owner;
|
||||
private PlayerController _player;
|
||||
|
||||
private float _viewAngle;
|
||||
private float _cosViewAngle;
|
||||
|
||||
public IdleState(GameObject owner, PlayerController player, float viewAngle)
|
||||
{
|
||||
_owner = owner;
|
||||
_player = player;
|
||||
_cosViewAngle = Mathf.Cos(viewAngle * Mathf.Deg2Rad);
|
||||
}
|
||||
|
||||
public void Enter(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("IDLE: Enter");
|
||||
}
|
||||
|
||||
public void Update(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("IDLE: Update");
|
||||
|
||||
float angle = Vector3.Angle(_owner.transform.forward, _player.transform.position - _owner.transform.position);
|
||||
// if (angle > _viewAngle)
|
||||
// if (cosAngle > _cosViewAngle)
|
||||
|
||||
// player visibility check
|
||||
if (Physics.Raycast(_owner.transform.position, _owner.transform.forward, out RaycastHit hit))
|
||||
{
|
||||
if (hit.transform.GetComponent<PlayerController>() == _player)
|
||||
{
|
||||
ctx.SetState(new ChasePlayerState(_owner, _player));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Exit(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("IDLE: Exit");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 503155bb57a0476b8588d29f0b2be6a3
|
||||
timeCreated: 1744127402
|
||||
Reference in New Issue
Block a user