1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-02-10 14:14:05 +00:00

Remove some unnecessary code from StateMachine

This commit is contained in:
fgenesis 2015-03-08 20:42:29 +01:00
parent db2faf026c
commit dee156cf7a
2 changed files with 6 additions and 80 deletions

View file

@ -28,7 +28,6 @@ StateMachine::StateMachine ()
enqueuedState = nextState = prevState = currentState = STATE_NONE; enqueuedState = nextState = prevState = currentState = STATE_NONE;
stateCounter = 0; stateCounter = 0;
currentStateData = enqueuedStateData = 0;
} }
int StateMachine::getState() int StateMachine::getState()
@ -46,7 +45,7 @@ int StateMachine::getPrevState()
return prevState; return prevState;
} }
void StateMachine::perform(int state, float time, void *stateData) void StateMachine::perform(int state, float time)
{ {
//debugLog("in perform"); //debugLog("in perform");
stateExtraDT = 0; stateExtraDT = 0;
@ -56,33 +55,30 @@ void StateMachine::perform(int state, float time, void *stateData)
// do this to prevent scripts from repeating endlessly when running main loops // do this to prevent scripts from repeating endlessly when running main loops
enqueuedState = STATE_NONE; enqueuedState = STATE_NONE;
enqueuedStateData = 0;
onExitState(currentState); onExitState(currentState);
stateCounter = 0; stateCounter = 0;
stateTime = time; stateTime = time;
currentState = state; currentState = state;
nextState = STATE_NONE; nextState = STATE_NONE;
this->currentStateData = stateData;
//debugLog("onActionInit"); //debugLog("onActionInit");
onEnterState(currentState); onEnterState(currentState);
//debugLog("done"); //debugLog("done");
} }
void StateMachine::setState(int state, float time, bool force, void *stateData) void StateMachine::setState(int state, float time, bool force)
{ {
if (canSetState(state)) if (canSetState(state))
{ {
if (force) if (force)
{ {
perform(state, time, stateData); perform(state, time);
} }
else else
{ {
enqueuedState = state; enqueuedState = state;
enqueuedTime = time; enqueuedTime = time;
enqueuedStateData = stateData;
} }
} }
} }
@ -140,64 +136,9 @@ void StateMachine::onUpdate (float dt)
} }
if (enqueuedState != STATE_NONE) if (enqueuedState != STATE_NONE)
{ {
perform(enqueuedState, enqueuedTime, enqueuedStateData); perform(enqueuedState, enqueuedTime);
enqueuedState = STATE_NONE; enqueuedState = STATE_NONE;
enqueuedTime = -1; enqueuedTime = -1;
} }
} }
void StateMachine::addCooldown(int state, float time)
{
Cooldown c;
c.state = state;
c.timer.start(time);
cooldowns.push_back(c);
}
bool StateMachine::isCooldown(int state)
{
for (Cooldowns::iterator i = cooldowns.begin(); i != cooldowns.end(); i++)
{
if ((*i).state == state && (*i).timer.isActive())
{
return true;
}
}
return false;
}
void StateMachine::removeCooldown(int state)
{
for (Cooldowns::iterator i = cooldowns.begin(); i != cooldowns.end(); i++)
{
if ((*i).state == state)
{
cooldowns.erase(i);
break;
}
}
}
void StateMachine::updateCooldowns(float dt)
{
std::queue<int> coolqueue;
for (Cooldowns::iterator i = cooldowns.begin(); i != cooldowns.end(); i++)
{
Cooldown *c = &((*i));
if (c->timer.updateCheck(dt)) {
coolqueue.push(c->state);
}
}
while (!coolqueue.empty())
{
removeCooldown(coolqueue.back());
coolqueue.pop();
}
}
void StateMachine::clearCooldowns()
{
cooldowns.clear();
}

View file

@ -23,20 +23,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "Base.h" #include "Base.h"
struct Cooldown
{
int state;
Timer timer;
};
typedef std::list<Cooldown> Cooldowns;
class StateMachine class StateMachine
{ {
public: public:
StateMachine (); StateMachine ();
virtual ~StateMachine() {} virtual ~StateMachine() {}
void setState(int state, float time = -1, bool force = false, void* stateData=0); void setState(int state, float time = -1, bool force = false);
void stopState(int state); void stopState(int state);
bool isState(int state); bool isState(int state);
int getState(); int getState();
@ -51,15 +44,8 @@ public:
}; };
virtual bool canSetState(int state); virtual bool canSetState(int state);
void addCooldown(int state, float time);
void removeCooldown(int state);
void updateCooldowns(float dt);
bool isCooldown(int state);
void clearCooldowns();
protected: protected:
void *currentStateData; void perform(int state, float time = -1);
void *enqueuedStateData;
void perform(int state, float time = -1, void *stateData=0);
virtual void onEnterState(int state); virtual void onEnterState(int state);
virtual void onExitState(int state); virtual void onExitState(int state);
@ -71,7 +57,6 @@ protected:
{ stateCounter = 0; } { stateCounter = 0; }
private: private:
Cooldowns cooldowns;
float stateCounter; float stateCounter;
}; };