Spaces:
Runtime error
Runtime error
File size: 4,695 Bytes
ce81a16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
using Photon.Deterministic;
using System;
namespace Quantum
{
public static unsafe partial class HFSMManager
{
public static unsafe partial class ThreadSafe
{
// ========== PUBLIC METHODS ==================================================================================
/// <summary>
/// Initializes the HFSM, making the current state to be equals the initial state
/// </summary>
public static unsafe void Init(FrameThreadSafe frame, EntityRef entity, HFSMRoot root)
{
if (frame.TryGetPointer(entity, out HFSMAgent* agent))
{
HFSMData* hfsmData = &agent->Data;
Init(frame, hfsmData, entity, root);
}
else
{
Log.Error("[Bot SDK] Tried to initialize an entity which has no HfsmAgent component");
}
}
/// <summary>
/// Initializes the HFSM, making the current state to be equals the initial state
/// </summary>
public static unsafe void Init(FrameThreadSafe frame, HFSMData* hfsm, EntityRef entity, HFSMRoot root)
{
hfsm->Root = root;
if (hfsm->Root.Equals(default) == false)
{
HFSMState initialState = frame.FindAsset<HFSMState>(root.InitialState.Id);
ChangeState(initialState, frame, hfsm, entity, "");
}
}
/// <summary>
/// Update the state of the HFSM.
/// </summary>
/// <param name="deltaTime">Usually the current deltaTime so the HFSM accumulates the time stood on the current state</param>
public static void Update(FrameThreadSafe frame, FP deltaTime, EntityRef entity)
{
if (frame.TryGetPointer(entity, out HFSMAgent* agent))
{
HFSMData* hfsmData = &agent->Data;
Update(frame, deltaTime, hfsmData, entity);
}
else
{
Log.Error("[Bot SDK] Tried to update an entity which has no HFSMAgent component");
}
}
/// <summary>
/// Update the state of the HFSM.
/// </summary>
/// <param name="deltaTime">Usually the current deltaTime so the HFSM accumulates the time stood on the current state</param>
public static void Update(FrameThreadSafe frame, FP deltaTime, HFSMData* hfsmData, EntityRef entity)
{
HFSMState currentState = frame.FindAsset<HFSMState>(hfsmData->CurrentState.Id);
currentState.UpdateState(frame, deltaTime, hfsmData, entity);
}
/// <summary>
/// Triggers an event if the target HFSM listens to it
/// </summary>
public static unsafe void TriggerEvent(FrameThreadSafe frame, EntityRef entity, string eventName)
{
if (frame.TryGetPointer(entity, out HFSMAgent* agent))
{
HFSMData* hfsmData = &agent->Data;
TriggerEvent(frame, hfsmData, entity, eventName);
}
else
{
Log.Error("[Bot SDK] Tried to trigger an event to an entity which has no HFSMAgent component");
}
}
/// <summary>
/// Triggers an event if the target HFSM listens to it
/// </summary>
public static unsafe void TriggerEvent(FrameThreadSafe frame, HFSMData* hfsmData, EntityRef entity, string eventName)
{
Int32 eventInt = 0;
HFSMRoot hfsmRoot = frame.FindAsset<HFSMRoot>(hfsmData->Root.Id);
if (hfsmRoot.RegisteredEvents.TryGetValue(eventName, out eventInt))
{
if (hfsmData->CurrentState.Equals(default) == false)
{
HFSMState currentState = frame.FindAsset<HFSMState>(hfsmData->CurrentState.Id);
currentState.Event(frame, hfsmData, entity, eventInt);
}
}
}
/// <summary>
/// Triggers an event if the target HFSM listens to it
/// </summary>
public static unsafe void TriggerEventNumber(FrameThreadSafe frame, HFSMData* hfsmData, EntityRef entity, Int32 eventInt)
{
if (hfsmData->CurrentState.Equals(default) == false)
{
HFSMState currentState = frame.FindAsset<HFSMState>(hfsmData->CurrentState.Id);
currentState.Event(frame, hfsmData, entity, eventInt);
}
}
// ========== INTERNAL METHODS ================================================================================
/// <summary>
/// Executes the On Exit actions for the current state, then changes the current state
/// </summary>
internal static void ChangeState(HFSMState nextState, FrameThreadSafe frame, HFSMData* hfsmData, EntityRef entity, string transitionId)
{
Assert.Check(nextState != null, "Tried to change HFSM to a null state");
HFSMState currentState = frame.FindAsset<HFSMState>(hfsmData->CurrentState.Id);
currentState?.ExitState(nextState, frame, hfsmData, entity);
hfsmData->CurrentState = nextState;
if (frame.IsVerified == true && entity != default(EntityRef))
{
StateChanged?.Invoke(entity, hfsmData->CurrentState.Id.Value, transitionId);
}
nextState.EnterState(frame, hfsmData, entity);
}
}
}
}
|