1#include "Library/Nerve/NerveStateCtrl.h"
2
3#include "Library/Nerve/NerveKeeper.h"
4
5namespace al {
6
7NerveStateCtrl::NerveStateCtrl(s32 maxStates) : mMaxStates(maxStates) {
8 mStates = new State[maxStates];
9 for (s32 i = 0; i < mMaxStates; i++)
10 mStates[i] = {.state: nullptr, .nerve: nullptr, .name: nullptr};
11}
12
13// adds a state to the list of states in the controller
14void NerveStateCtrl::addState(NerveStateBase* state, const Nerve* nerve, const char* name) {
15 mStates[mStateCount] = {.state: state, .nerve: nerve, .name: name};
16 mStateCount++;
17}
18
19// run the state's update function, if there is a current state active
20bool NerveStateCtrl::updateCurrentState() {
21 if (!mCurrentState)
22 return false;
23
24 return mCurrentState->state->update();
25}
26
27void NerveStateCtrl::startState(const Nerve* nerve) {
28 mCurrentState = findStateInfo(nerve);
29
30 if (mCurrentState) {
31 mCurrentState->state->appear();
32 if (mCurrentState->state->getNerveKeeper())
33 mCurrentState->state->getNerveKeeper()->tryChangeNerve();
34 }
35}
36
37// UNUSED FUNCTION
38// uses a supplied nerve pointer to compare it with the nerves contained in states
39// returns the matching nerve, if any
40NerveStateCtrl::State* NerveStateCtrl::findStateInfo(const Nerve* nerve) {
41 for (s32 i = 0; i < mStateCount; i++)
42 if (mStates[i].nerve == nerve)
43 return &mStates[i];
44
45 return nullptr;
46}
47
48// determines if the current state on the controller has ended
49// this can occur if there is no state, or if the base is not considered dead
50bool NerveStateCtrl::isCurrentStateEnd() const {
51 if (!mCurrentState)
52 return true;
53
54 return mCurrentState->state->isDead() != 0;
55}
56
57// attempt to end the currently active state by "killing" the state, then killing the state
58// controller contained in the nerve keeper
59void NerveStateCtrl::tryEndCurrentState() {
60 if (mCurrentState) {
61 if (!mCurrentState->state->isDead())
62 mCurrentState->state->kill();
63
64 NerveKeeper* keeper = mCurrentState->state->getNerveKeeper();
65
66 if (keeper) {
67 NerveStateCtrl* ctrl = keeper->getStateCtrl();
68
69 if (ctrl)
70 ctrl->tryEndCurrentState();
71 }
72
73 mCurrentState = nullptr;
74 }
75}
76} // namespace al
77