框架上传
This commit is contained in:
39
Assets/00.StaryEvo/Runtime/Utility/FSM/FSMIState.cs
Normal file
39
Assets/00.StaryEvo/Runtime/Utility/FSM/FSMIState.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public interface FSMIState {
|
||||
IFsmSystem FsmSystem { get; }
|
||||
string Name { get; }
|
||||
float Timer { get; set; }
|
||||
//进入该状态时调用
|
||||
void OnEnter();
|
||||
//每帧调用
|
||||
void OnUpdate();
|
||||
//退出该状态时调用
|
||||
void OnExit();
|
||||
}
|
||||
|
||||
public abstract class AbstractFSMIState : FSMIState
|
||||
{
|
||||
public IFsmSystem FsmSystem { get; }
|
||||
public string Name { get; }
|
||||
public abstract float Timer { get; set; }
|
||||
|
||||
public AbstractFSMIState(IFsmSystem system)
|
||||
{
|
||||
FsmSystem = system;
|
||||
Name = GetType().Name;
|
||||
}
|
||||
|
||||
public abstract void OnEnter();
|
||||
|
||||
public virtual void OnUpdate()
|
||||
{
|
||||
Timer += Time.deltaTime;
|
||||
}
|
||||
|
||||
public abstract void OnExit();
|
||||
}
|
||||
}
|
||||
11
Assets/00.StaryEvo/Runtime/Utility/FSM/FSMIState.cs.meta
Normal file
11
Assets/00.StaryEvo/Runtime/Utility/FSM/FSMIState.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1dc351fc8dc35ec4ca388c24444a80a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
70
Assets/00.StaryEvo/Runtime/Utility/FSM/FSMISystem.cs
Normal file
70
Assets/00.StaryEvo/Runtime/Utility/FSM/FSMISystem.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public interface IFsmSystem
|
||||
{
|
||||
FSMIState CurState { get; set; }
|
||||
void AddState(FSMIState state);
|
||||
void RemoveState(FSMIState state);
|
||||
void SetCurState(string name);
|
||||
FSMIState GetStateWithName(string name);
|
||||
HashMap<string, FSMIState> States { get; set; }
|
||||
}
|
||||
|
||||
public class FsmSystem : IFsmSystem
|
||||
{
|
||||
public FSMIState CurState { get; set; }
|
||||
public HashMap<string, FSMIState> States { get; set; }
|
||||
|
||||
public FsmSystem()
|
||||
{
|
||||
States = new HashMap<string, FSMIState>();
|
||||
}
|
||||
|
||||
public void AddState(FSMIState state)
|
||||
{
|
||||
Debug.Log(state.Name);
|
||||
if (!States.ContainsKey(state.Name))
|
||||
{
|
||||
States.Add(state.Name, state);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogErrorFormat("States状态机容器里已存在名字为--【{0}】--的状态", state.Name.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveState(FSMIState state)
|
||||
{
|
||||
if (States.ContainsKey(state.Name))
|
||||
{
|
||||
States.Remove(state.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogErrorFormat("States状态机容器里不存在名字为--【{0}】--的状态", state.Name.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCurState(string name)
|
||||
{
|
||||
if (CurState != null)
|
||||
CurState.OnExit();
|
||||
FSMIState state = GetStateWithName(name);
|
||||
CurState = state;
|
||||
CurState.OnEnter();
|
||||
}
|
||||
|
||||
public FSMIState GetStateWithName(string name)
|
||||
{
|
||||
if (States.ContainsKey(name))
|
||||
{
|
||||
return States[name];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 572d20e55965479cb8846f9fad4873a2
|
||||
timeCreated: 1625997700
|
||||
Reference in New Issue
Block a user