2023-08-23 01:59:26 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-03-31 23:31:00 +08:00
|
|
|
using System.Linq;
|
2023-08-23 01:59:26 +08:00
|
|
|
using BITKit.Entities;
|
|
|
|
using BITKit.StateMachine;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BITKit.Entities
|
|
|
|
{
|
2023-11-06 01:17:23 +08:00
|
|
|
public class StateBasedBehavior<T> : EntityBehavior,IStateMachine<T> where T : IState
|
2023-08-23 01:59:26 +08:00
|
|
|
{
|
|
|
|
[SerializeField] private MonoStateMachine<T> stateMachine;
|
|
|
|
public bool Enabled
|
|
|
|
{
|
|
|
|
get => stateMachine.Enabled;
|
|
|
|
set => stateMachine.Enabled = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T CurrentState
|
|
|
|
{
|
|
|
|
get => stateMachine.CurrentState;
|
|
|
|
set => stateMachine.CurrentState = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public event Action<T, T> OnStateChanged
|
|
|
|
{
|
|
|
|
add => stateMachine.OnStateChanged += value;
|
|
|
|
remove => stateMachine.OnStateChanged -= value;
|
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
|
|
|
|
public event Action<T> OnStateRegistered;
|
|
|
|
public event Action<T> OnStateUnRegistered;
|
2023-08-23 01:59:26 +08:00
|
|
|
public IDictionary<Type, T> StateDictionary => stateMachine.StateDictionary;
|
2024-03-31 23:31:00 +08:00
|
|
|
public override void OnAwake()
|
2023-08-23 01:59:26 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
base.OnAwake();
|
2023-09-01 14:35:05 +08:00
|
|
|
if (stateMachine is null)
|
|
|
|
{
|
|
|
|
Debug.LogWarning(GetType().Name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
foreach (var x in stateMachine.states)
|
|
|
|
{
|
|
|
|
Entity.Inject(x);
|
|
|
|
}
|
2023-09-01 14:35:05 +08:00
|
|
|
stateMachine.Initialize();
|
|
|
|
}
|
2023-08-23 01:59:26 +08:00
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
void IStateMachine<T>.Initialize()
|
2023-08-23 01:59:26 +08:00
|
|
|
{
|
|
|
|
stateMachine.Initialize();
|
|
|
|
}
|
2023-09-01 14:35:05 +08:00
|
|
|
public void UpdateState(float deltaTime)
|
2023-08-23 01:59:26 +08:00
|
|
|
{
|
2023-09-01 14:35:05 +08:00
|
|
|
stateMachine.UpdateState(deltaTime);
|
2023-08-23 01:59:26 +08:00
|
|
|
}
|
|
|
|
public void DisposeState()
|
|
|
|
{
|
|
|
|
stateMachine.DisposeState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TransitionState<State>() where State : T
|
|
|
|
{
|
|
|
|
stateMachine.TransitionState<State>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TransitionState(T state)
|
|
|
|
{
|
|
|
|
stateMachine.TransitionState(state);
|
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
|
|
|
|
public virtual void Register(T newState)
|
|
|
|
{
|
|
|
|
Entity.Inject(newState);
|
|
|
|
StateMachineUtils.Register(this, newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void UnRegister(T newState)=>StateMachineUtils.UnRegister(this,newState);
|
|
|
|
public void InvokeOnStateRegistered(T state)
|
|
|
|
{
|
|
|
|
OnStateRegistered?.Invoke(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void InvokeOnStateUnRegistered(T state)
|
|
|
|
{
|
|
|
|
OnStateUnRegistered?.Invoke(state);
|
|
|
|
}
|
2023-08-23 01:59:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|