-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSM.cpp
78 lines (70 loc) · 1.53 KB
/
FSM.cpp
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
/*
* AIMachine.cpp
* tractionedge
*
* Created by Steven Hamilton on 28/09/10.
* Copyright 2010 n/a. All rights reserved.
*
*/
#include "FSM.h"
#include "Logger.h"
FSM::FSM()
{
currentState=NULL;
}
FSM::~FSM()
{
states.clear();
}
void FSM::initMachine(int machineType)
{
type=machineType;
switch (type) {
case AI_MACH_GIMP:
addState(new AIStatePatrol(), true);
addState(new AIStateAttack(), false);
addState(new AIStateRun(),false);
break;
case AI_MACH_SLITHER:
addState(new AIStatePatrol(), true);
addState(new AIStateCharge(), false);
addState(new AIStateAttackMelee(), false);
addState(new AIStateRun(),false);
break;
default:
break;
}
}
void FSM::addState(FSMState * newState, bool makeCurrent)
{
states.push_back(newState);
if (makeCurrent){
currentState=newState;
currentState->enter();
}
}
bool FSM::changeState(Creature * creature,int newType)
{
FSMState * gotoState = NULL;
for(stateIt=states.begin(); stateIt!=states.end();stateIt++){
if ((*stateIt)->type == newType){
gotoState=(*stateIt);
}
}
if (gotoState==NULL)
{
//LOG << creature->objectId << " ERROR: FSM Trying to change to non-existent state";
return false;
}
currentState->exit(creature);
gotoState->enter(creature);
currentState = gotoState;
return true;
}
void FSM::update(Creature* creature)
{
//exit if no state loaded
if (currentState == NULL) return;
//execute current state
currentState->update(creature);
}