-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFSM_OOStateDynamic.h
117 lines (111 loc) · 2.83 KB
/
FSM_OOStateDynamic.h
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
namespace FSMOOState {
struct State;
struct Machine {
State *state = nullptr;
float sleepiness;
float hunger;
float huntTimer;
float eatTimer;
inline void UpdateState( State *newState );
inline ~Machine();
};
struct State {
virtual State * Update( Machine &M, float deltaTime ) = 0;
};
struct Sleeping final : public State {
virtual State * Update( Machine &M, float deltaTime ) override;
};
struct Hunting final : public State {
virtual State * Update( Machine &M, float deltaTime ) override;
};
struct Eating final : public State {
virtual State * Update( Machine &M, float deltaTime ) override;
};
struct Exploring final : public State {
virtual State * Update( Machine &M, float deltaTime ) override;
};
Sleeping m_commonSleeping;
Hunting m_commonHunting;
Eating m_commonEating;
Exploring m_commonExploring;
struct Data {
Machine machine[NUM_MACHINES];
Data() {
pcg32_random_t rng;
pcg32_srandom_r(&rng, 1234, 5678);
for( int m = 0; m < NUM_MACHINES; ++m ) {
Machine &M = machine[m];
M.state = new Sleeping;
M.sleepiness = pcg32_random_r_rangef(&rng, 0.0f, 0.2f );
M.hunger = pcg32_random_r_rangef(&rng, 0.5f, 0.9f );
M.huntTimer = HUNTING_TIME;
M.eatTimer = EATING_TIME;
}
}
void Update( float deltaTime ) {
for( int m = 0; m < NUM_MACHINES; ++m ) {
Machine &M = machine[m];
M.UpdateState( M.state->Update( M, deltaTime ) );
}
}
};
// inlines
inline void Machine::UpdateState( State *newState ) {
if( newState ) {
//if( state )
//delete state;
state = newState;
}
}
inline Machine::~Machine() {
//if( state )
//delete state;
state = nullptr;
}
State * Sleeping::Update( Machine &M, float deltaTime ) {
M.hunger += deltaTime * SLEEP_HUNGER;
M.sleepiness += deltaTime * SLEEP_SLEEP;
if( M.sleepiness <= 0.0f ) {
M.sleepiness = 0.0f;
if( M.hunger > HUNGER_TRIGGER ) {
return new Hunting;
} else {
return new Exploring;
}
}
}
State * Hunting::Update( Machine &M, float deltaTime ) {
M.hunger += deltaTime * HUNT_HUNGER;
M.sleepiness += deltaTime * HUNT_SLEEP;
if( M.sleepiness > SLEEP_TRIGGER ) {
return new Sleeping;
}
if( M.huntTimer <= 0.0f ) {
M.huntTimer = HUNTING_TIME;
M.eatTimer = EATING_TIME;
return new Eating;
}
}
State * Eating::Update( Machine &M, float deltaTime ) {
M.hunger += deltaTime * EAT_HUNGER;
M.sleepiness += deltaTime * EAT_SLEEP;
M.eatTimer -= deltaTime;
if( M.eatTimer <= 0.0f ) {
if( M.hunger > HUNGER_TRIGGER ) {
return new Hunting;
} else {
return new Exploring;
}
}
}
State * Exploring::Update( Machine &M, float deltaTime ) {
M.hunger += deltaTime * EXPLORE_HUNGER;
M.sleepiness += deltaTime * EXPLORE_SLEEP;
if( M.hunger > HUNGER_TRIGGER ) {
return new Hunting;
}
if( M.sleepiness > SLEEP_TRIGGER ) {
return new Sleeping;
}
}
}