add unity project
add FSM examples
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58349f5d0a8e4705a97cef8b771d69dc
|
||||
timeCreated: 1744126586
|
||||
@@ -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
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class FireButton : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InputAction _inputAction;
|
||||
|
||||
private bool _isPressed;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_inputAction.Enable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_inputAction.Disable();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_isPressed = _isPressed || _inputAction.triggered;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (_isPressed)
|
||||
{
|
||||
_isPressed = false;
|
||||
// spawn bullet
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d58160624a544519c368f312ed50b39
|
||||
timeCreated: 1744124273
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e084138a20b47b4aef9bbf9e40bf53f
|
||||
timeCreated: 1744128938
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e3b4976997c44c9e87e42f3589f2519
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,49 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace DefaultNamespace.StateMachineBehaviours
|
||||
{
|
||||
public class Test : UnityEngine.StateMachineBehaviour
|
||||
{
|
||||
private static readonly int IS_EXIT_ID = Animator.StringToHash("isExit");
|
||||
|
||||
[SerializeField] private InputAction _inputAction;
|
||||
|
||||
public override void OnStateEnter(UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo stateInfo,
|
||||
int layerIndex)
|
||||
{
|
||||
Debug.Log("OnStateEnter");
|
||||
_inputAction.Enable();
|
||||
}
|
||||
|
||||
public override void OnStateExit(UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo stateInfo,
|
||||
int layerIndex)
|
||||
{
|
||||
Debug.Log("OnStateExit");
|
||||
_inputAction.Disable();
|
||||
}
|
||||
|
||||
public override void OnStateUpdate(UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo stateInfo,
|
||||
int layerIndex)
|
||||
{
|
||||
Debug.Log("OnStateUpdate");
|
||||
|
||||
if (_inputAction.triggered)
|
||||
{
|
||||
animator.SetBool(IS_EXIT_ID, !animator.GetBool(IS_EXIT_ID));
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnStateMove(UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo stateInfo,
|
||||
int layerIndex)
|
||||
{
|
||||
Debug.Log("OnStateMove");
|
||||
}
|
||||
|
||||
public override void OnStateIK(UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo stateInfo,
|
||||
int layerIndex)
|
||||
{
|
||||
Debug.Log("OnStateIK");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0eabf46f6ebc4205b98427a83dbe96e2
|
||||
timeCreated: 1744122422
|
||||
Reference in New Issue
Block a user