Skip to content

Commit

Permalink
增加创建有限状态机重载
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang Yin committed Nov 8, 2019
1 parent 199862f commit d613bb3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
43 changes: 43 additions & 0 deletions GameFramework/Fsm/Fsm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public static Fsm<T> Create(string name, T owner, params FsmState<T>[] states)
Fsm<T> fsm = ReferencePool.Acquire<Fsm<T>>();
fsm.Name = name;
fsm.m_Owner = owner;
fsm.m_IsDestroyed = false;
foreach (FsmState<T> state in states)
{
if (state == null)
Expand All @@ -163,7 +164,49 @@ public static Fsm<T> Create(string name, T owner, params FsmState<T>[] states)
state.OnInit(fsm);
}

return fsm;
}

/// <summary>
/// 创建有限状态机。
/// </summary>
/// <param name="name">有限状态机名称。</param>
/// <param name="owner">有限状态机持有者。</param>
/// <param name="states">有限状态机状态集合。</param>
/// <returns>创建的有限状态机。</returns>
public static Fsm<T> Create(string name, T owner, List<FsmState<T>> states)
{
if (owner == null)
{
throw new GameFrameworkException("FSM owner is invalid.");
}

if (states == null || states.Count < 1)
{
throw new GameFrameworkException("FSM states is invalid.");
}

Fsm<T> fsm = ReferencePool.Acquire<Fsm<T>>();
fsm.Name = name;
fsm.m_Owner = owner;
fsm.m_IsDestroyed = false;
foreach (FsmState<T> state in states)
{
if (state == null)
{
throw new GameFrameworkException("FSM states is invalid.");
}

Type stateType = state.GetType();
if (fsm.m_States.ContainsKey(stateType))
{
throw new GameFrameworkException(Utility.Text.Format("FSM '{0}' state '{1}' is already exist.", Utility.Text.GetFullName<T>(name), stateType));
}

fsm.m_States.Add(stateType, state);
state.OnInit(fsm);
}

return fsm;
}

Expand Down
32 changes: 32 additions & 0 deletions GameFramework/Fsm/FsmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,38 @@ public IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) w
return fsm;
}

/// <summary>
/// 创建有限状态机。
/// </summary>
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
/// <param name="owner">有限状态机持有者。</param>
/// <param name="states">有限状态机状态集合。</param>
/// <returns>要创建的有限状态机。</returns>
public IFsm<T> CreateFsm<T>(T owner, List<FsmState<T>> states) where T : class
{
return CreateFsm(string.Empty, owner, states);
}

/// <summary>
/// 创建有限状态机。
/// </summary>
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
/// <param name="name">有限状态机名称。</param>
/// <param name="owner">有限状态机持有者。</param>
/// <param name="states">有限状态机状态集合。</param>
/// <returns>要创建的有限状态机。</returns>
public IFsm<T> CreateFsm<T>(string name, T owner, List<FsmState<T>> states) where T : class
{
if (HasFsm<T>(name))
{
throw new GameFrameworkException(Utility.Text.Format("Already exist FSM '{0}'.", Utility.Text.GetFullName<T>(name)));
}

Fsm<T> fsm = Fsm<T>.Create(name, owner, states);
m_Fsms.Add(Utility.Text.GetFullName<T>(name), fsm);
return fsm;
}

/// <summary>
/// 销毁有限状态机。
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions GameFramework/Fsm/IFsmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ int Count
/// <returns>要创建的有限状态机。</returns>
IFsm<T> CreateFsm<T>(string name, T owner, params FsmState<T>[] states) where T : class;

/// <summary>
/// 创建有限状态机。
/// </summary>
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
/// <param name="owner">有限状态机持有者。</param>
/// <param name="states">有限状态机状态集合。</param>
/// <returns>要创建的有限状态机。</returns>
IFsm<T> CreateFsm<T>(T owner, List<FsmState<T>> states) where T : class;

/// <summary>
/// 创建有限状态机。
/// </summary>
/// <typeparam name="T">有限状态机持有者类型。</typeparam>
/// <param name="name">有限状态机名称。</param>
/// <param name="owner">有限状态机持有者。</param>
/// <param name="states">有限状态机状态集合。</param>
/// <returns>要创建的有限状态机。</returns>
IFsm<T> CreateFsm<T>(string name, T owner, List<FsmState<T>> states) where T : class;

/// <summary>
/// 销毁有限状态机。
/// </summary>
Expand Down

0 comments on commit d613bb3

Please sign in to comment.