mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utilities.StateMachine
|
||||
{
|
||||
public interface IStateChanger
|
||||
{
|
||||
event Action<State> Changed;
|
||||
|
||||
StateMachine ChangeState<TState>() where TState : State;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b705c6047e545c483833a0fe80a6850
|
||||
timeCreated: 1770390018
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utilities.StateMachine
|
||||
{
|
||||
public abstract class State
|
||||
{
|
||||
public event Action Entered;
|
||||
|
||||
public event Action Exited;
|
||||
|
||||
public virtual void OnEnter() => Entered?.Invoke();
|
||||
|
||||
public virtual void OnExit() => Exited?.Invoke();
|
||||
|
||||
public virtual void Update(float deltaTime) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 991e020273854d7683766af332608683
|
||||
timeCreated: 1770376146
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utilities.StateMachine
|
||||
{
|
||||
public abstract class StateMachine : IStateChanger
|
||||
{
|
||||
public event Action<State> Changed;
|
||||
|
||||
public State Current { get; private set; }
|
||||
|
||||
private readonly Dictionary<Type, State> _states = new();
|
||||
private bool _isRunning;
|
||||
|
||||
public StateMachine(params State[] states)
|
||||
{
|
||||
foreach (State state in states)
|
||||
Add(state);
|
||||
}
|
||||
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
if (_isRunning == false)
|
||||
return;
|
||||
|
||||
Current?.Update(deltaTime);
|
||||
}
|
||||
|
||||
public StateMachine Add(State state)
|
||||
{
|
||||
if (state == null)
|
||||
throw new ArgumentNullException(nameof(state));
|
||||
|
||||
Type type = state.GetType();
|
||||
|
||||
if (_states.TryAdd(type, state) == false)
|
||||
throw new InvalidOperationException($"[StateMachine] State {type.Name} is already registered");
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public StateMachine Remove<TState>() where TState : State
|
||||
{
|
||||
Type type = typeof(TState);
|
||||
|
||||
if (_states.Remove(type) == false)
|
||||
throw new InvalidOperationException($"[StateMachine] State {type.Name} is not registered");
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public StateMachine ChangeState<TState>() where TState : State
|
||||
{
|
||||
Type type = typeof(TState);
|
||||
|
||||
if (_states.TryGetValue(type, out State state) == false)
|
||||
throw new InvalidOperationException($"[StateMachine] State {type.Name} is not registered");
|
||||
|
||||
SwitchState(state);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private void SwitchState(State newState)
|
||||
{
|
||||
if (newState == null)
|
||||
throw new ArgumentNullException(nameof(newState));
|
||||
|
||||
if (Current != null)
|
||||
Current.OnExit();
|
||||
else
|
||||
_isRunning = true;
|
||||
|
||||
Current = newState;
|
||||
Changed?.Invoke(Current);
|
||||
Current.OnEnter();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a9e1e9ad9be48b083a7678034b4312e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user