浏览代码
Merge branch 'state-machine' of https://github.com/DeivSky/open-project-1 into state-machine
/main
Merge branch 'state-machine' of https://github.com/DeivSky/open-project-1 into state-machine
/main
DeivSky
4 年前
当前提交
33f1fbae
共有 8 个文件被更改,包括 212 次插入 和 212 次删除
-
96UOP1_Project/Assets/Scripts/StateMachine/Core/State.cs
-
34UOP1_Project/Assets/Scripts/StateMachine/Core/StateAction.cs
-
58UOP1_Project/Assets/Scripts/StateMachine/Core/StateTransition.cs
-
72UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableState.cs
-
36UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateAction.cs
-
30UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateCondition.cs
-
96UOP1_Project/Assets/Scripts/StateMachine/Scriptables/ScriptableStateTransition.cs
-
2UOP1_Project/Assets/Scripts/StateMachineTest/ScriptableChaseAction.cs
|
|||
namespace DeivSky.StateMachine |
|||
{ |
|||
public class State |
|||
{ |
|||
public string Name { get; internal set; } |
|||
public class State |
|||
{ |
|||
public string Name { get; internal set; } |
|||
internal StateMachine _stateMachine; |
|||
internal StateTransition[] _transitions; |
|||
internal StateAction[] _actions; |
|||
internal StateMachine _stateMachine; |
|||
internal StateTransition[] _transitions; |
|||
internal StateAction[] _actions; |
|||
internal State() { } |
|||
internal State() { } |
|||
public State( |
|||
StateMachine stateMachine, |
|||
StateTransition[] transitions, |
|||
StateAction[] actions) |
|||
{ |
|||
_stateMachine = stateMachine; |
|||
_transitions = transitions; |
|||
_actions = actions; |
|||
} |
|||
public State( |
|||
StateMachine stateMachine, |
|||
StateTransition[] transitions, |
|||
StateAction[] actions) |
|||
{ |
|||
_stateMachine = stateMachine; |
|||
_transitions = transitions; |
|||
_actions = actions; |
|||
} |
|||
public void OnStateEnter() |
|||
{ |
|||
public void OnStateEnter() |
|||
{ |
|||
for (int i = 0; i < comps.Length; i++) |
|||
comps[i].OnStateEnter(); |
|||
} |
|||
OnStateEnter(_transitions); |
|||
OnStateEnter(_actions); |
|||
} |
|||
for (int i = 0; i < comps.Length; i++) |
|||
comps[i].OnStateEnter(); |
|||
} |
|||
OnStateEnter(_transitions); |
|||
OnStateEnter(_actions); |
|||
} |
|||
public void OnUpdate() |
|||
{ |
|||
for (int i = 0; i < _actions.Length; i++) |
|||
_actions[i].Perform(); |
|||
} |
|||
public void OnUpdate() |
|||
{ |
|||
for (int i = 0; i < _actions.Length; i++) |
|||
_actions[i].Perform(); |
|||
} |
|||
public void OnStateExit() |
|||
{ |
|||
void OnStateExit(IStateComponent[] comps) |
|||
{ |
|||
for (int i = 0; i < comps.Length; i++) |
|||
comps[i].OnStateExit(); |
|||
} |
|||
OnStateExit(_transitions); |
|||
OnStateExit(_actions); |
|||
} |
|||
public void OnStateExit() |
|||
{ |
|||
void OnStateExit(IStateComponent[] comps) |
|||
{ |
|||
for (int i = 0; i < comps.Length; i++) |
|||
comps[i].OnStateExit(); |
|||
} |
|||
OnStateExit(_transitions); |
|||
OnStateExit(_actions); |
|||
} |
|||
public bool TryGetTransition(out State state) |
|||
{ |
|||
for (int i = 0; i < _transitions.Length; i++) |
|||
if (_transitions[i].TryGetTransiton(out state)) |
|||
return true; |
|||
public bool TryGetTransition(out State state) |
|||
{ |
|||
for (int i = 0; i < _transitions.Length; i++) |
|||
if (_transitions[i].TryGetTransiton(out state)) |
|||
return true; |
|||
state = null; |
|||
return false; |
|||
} |
|||
} |
|||
state = null; |
|||
return false; |
|||
} |
|||
} |
|||
} |
|
|||
namespace DeivSky.StateMachine |
|||
{ |
|||
/// <summary>
|
|||
/// An object representing an action.
|
|||
/// </summary>
|
|||
public abstract class StateAction : IStateComponent |
|||
{ |
|||
/// <summary>
|
|||
/// Perform the action this object represents.
|
|||
/// </summary>
|
|||
public abstract void Perform(); |
|||
/// <summary>
|
|||
/// An object representing an action.
|
|||
/// </summary>
|
|||
public abstract class StateAction : IStateComponent |
|||
{ |
|||
/// <summary>
|
|||
/// Perform the action this object represents.
|
|||
/// </summary>
|
|||
public abstract void Perform(); |
|||
/// <summary>
|
|||
/// Awake is called when creating a new instance. Use this method to cache the components needed for the action.
|
|||
/// </summary>
|
|||
/// <param name="stateMachine">The state machine this instance belongs to.</param>
|
|||
public virtual void Awake(StateMachine stateMachine) { } |
|||
/// <summary>
|
|||
/// Awake is called when creating a new instance. Use this method to cache the components needed for the action.
|
|||
/// </summary>
|
|||
/// <param name="stateMachine">The state machine this instance belongs to.</param>
|
|||
public virtual void Awake(StateMachine stateMachine) { } |
|||
public virtual void OnStateEnter() { } |
|||
public virtual void OnStateExit() { } |
|||
} |
|||
public virtual void OnStateEnter() { } |
|||
public virtual void OnStateExit() { } |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue