add project files
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace OHM.UnityToolkit.FSM
|
||||
{
|
||||
public class FiniteStateMachine<StateType> where StateType : StateBase<StateType>
|
||||
{
|
||||
private float _currentStateStartTime;
|
||||
|
||||
public StateType CurrentState { get; private set; }
|
||||
|
||||
public void SetState(StateType state)
|
||||
{
|
||||
if (state == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (CurrentState != null)
|
||||
{
|
||||
CurrentState.Leave();
|
||||
}
|
||||
_currentStateStartTime = Time.time;
|
||||
CurrentState = state;
|
||||
state.Enter();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (CurrentState == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CurrentState.Update();
|
||||
if (CurrentState == null || CurrentState.NextState == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (Time.time - _currentStateStartTime >= CurrentState.MaxDuration)
|
||||
{
|
||||
SetState(CurrentState.NextState as StateType);
|
||||
}
|
||||
}
|
||||
|
||||
public float GetCurrentStateDuration()
|
||||
{
|
||||
return Time.time - _currentStateStartTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe38a94f0266a7f489a17d4ee5f664fd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
namespace OHM.UnityToolkit.FSM
|
||||
{
|
||||
public class StateBase<T> where T : StateBase<T>
|
||||
{
|
||||
public Action startAction;
|
||||
|
||||
public Action updateAction;
|
||||
|
||||
public Action leaveAction;
|
||||
|
||||
public float MaxDuration { get; private set; }
|
||||
|
||||
public StateBase<T> NextState { get; private set; }
|
||||
|
||||
public void SetAutoTransition(StateBase<T> nextState, float duration)
|
||||
{
|
||||
MaxDuration = duration;
|
||||
NextState = nextState;
|
||||
}
|
||||
|
||||
public void Enter()
|
||||
{
|
||||
if (startAction != null)
|
||||
{
|
||||
startAction();
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (updateAction != null)
|
||||
{
|
||||
updateAction();
|
||||
}
|
||||
}
|
||||
|
||||
public void Leave()
|
||||
{
|
||||
if (leaveAction != null)
|
||||
{
|
||||
leaveAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fceeb876c34166646ba3b9dcda3bfa17
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user