1
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public interface IMonoProxy
|
||||
{
|
||||
|
||||
}
|
||||
public class MonoProxy : Mono
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public IMonoProxy proxy;
|
||||
BehaviorUpdater updater = new();
|
||||
protected override void Awake()
|
||||
{
|
||||
updater.Init(proxy);
|
||||
updater.OnAwake();
|
||||
}
|
||||
protected override void Start()
|
||||
{
|
||||
updater.OnStart();
|
||||
}
|
||||
protected override void Update()
|
||||
{
|
||||
updater.OnUpdate(Time.deltaTime);
|
||||
}
|
||||
protected override void FixedUpdate()
|
||||
{
|
||||
updater.OnFixedUpdate(Time.fixedDeltaTime);
|
||||
}
|
||||
protected override void LateUpdate()
|
||||
{
|
||||
updater.OnLateUpdate(Time.deltaTime);
|
||||
}
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
updater.OnDestroy();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 27745a46fda75cf4da1766691754f81a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -10,7 +10,7 @@ using UnityEngine.UIElements;
|
||||
namespace BITKit.StateMachine
|
||||
{
|
||||
[Serializable]
|
||||
public class MonoStateMachine<TState> : IStateMachine<TState>, IMonoProxy where TState : IState
|
||||
public class MonoStateMachine<TState> : IStateMachine<TState> where TState : IState
|
||||
{
|
||||
public bool Enabled
|
||||
{
|
||||
@@ -34,9 +34,12 @@ namespace BITKit.StateMachine
|
||||
|
||||
[SerializeReference, SubclassSelector] public List<TState> states = new();
|
||||
[SerializeField,ReadOnly] private string _currentStateName;
|
||||
|
||||
[SerializeField] private bool debug;
|
||||
[SerializeField] private bool transitionOnNextFrame;
|
||||
public IDictionary<Type, TState> StateDictionary { get; } = new Dictionary<Type, TState>();
|
||||
private bool _enabled;
|
||||
private bool _enabled = true;
|
||||
private readonly DoubleBuffer<TState> _nextState = new();
|
||||
protected bool cancelUpdateStateThisFrame;
|
||||
public void Initialize()
|
||||
{
|
||||
foreach (var state in states)
|
||||
@@ -52,8 +55,17 @@ namespace BITKit.StateMachine
|
||||
}
|
||||
public void UpdateState(float deltaTime)
|
||||
{
|
||||
if(transitionOnNextFrame && _nextState.TryGetRelease(out var nextState))
|
||||
TransitionStateInternal(nextState);
|
||||
if (Enabled)
|
||||
CurrentState?.OnStateUpdate(deltaTime);
|
||||
{
|
||||
if (cancelUpdateStateThisFrame is false)
|
||||
{
|
||||
CurrentState?.OnStateUpdate(deltaTime);
|
||||
}
|
||||
cancelUpdateStateThisFrame = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void DisposeState()
|
||||
@@ -65,14 +77,31 @@ namespace BITKit.StateMachine
|
||||
TransitionState(nextState);
|
||||
}
|
||||
public void TransitionState(TState newState)
|
||||
{
|
||||
if(transitionOnNextFrame)
|
||||
_nextState.Release(newState);
|
||||
else
|
||||
{
|
||||
TransitionStateInternal(newState);
|
||||
cancelUpdateStateThisFrame = true;
|
||||
}
|
||||
}
|
||||
private void TransitionStateInternal(TState newState)
|
||||
{
|
||||
if (newState.Equals(CurrentState)) return;
|
||||
|
||||
var oldState = CurrentState;
|
||||
if (oldState is not null)
|
||||
{
|
||||
oldState.OnStateExit(oldState, newState);
|
||||
oldState.Enabled = false;
|
||||
}
|
||||
|
||||
if (debug)
|
||||
{
|
||||
BIT4Log.Log<MonoStateMachine<TState>>($"TransitionState from {_currentStateName} to {newState.GetType().Name}");
|
||||
}
|
||||
|
||||
newState.OnStateEntry(oldState);
|
||||
CurrentState = newState;
|
||||
OnStateChanged?.Invoke(oldState, newState);
|
||||
|
Reference in New Issue
Block a user