-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameState.h
305 lines (275 loc) · 6.68 KB
/
GameState.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifndef GAMESTATE_H_
#define GAMESTATE_H_
#include <vector>
#include "math/Vector.h"
#include "event/Event.h"
#include "core/Error.h"
using namespace anengine;
struct SkyportEventClass
{
enum SkyportEventClass_t
{
GameState = 0x40000000,
Sound = 0x40000001,
};
};
struct GameStateEventCodes
{
enum GameStateEventCodes_t
{
NewGameState,
StateProcessed
};
};
enum class SkyportAction
{
None,
Move,
Pass,
Upgrade,
Mine,
Laser,
Motar,
Droid
};
enum class Direction
{
None,
Up,
Down,
Right_Up,
Left_Up,
Left_Down,
Right_Down
};
enum class Weapon
{
Laser,
Motar,
Droid
};
class ActionState
{
public:
static const int MaxDroidCommands = 5;
private:
SkyportAction myAction;
Direction myDirection;
Weapon myWeapon;
VectorI2 myCoordinate;
Direction myCommands[MaxDroidCommands];
ActionState(SkyportAction action) : myAction(action) { }
public:
ActionState() : myAction(SkyportAction::None) { }
static ActionState CreateMovement(Direction dir)
{
ActionState state(SkyportAction::Move);
state.myDirection = dir;
return state;
}
static ActionState CreatePass()
{
return ActionState(SkyportAction::Pass);
}
static ActionState CreateUpgrade(Weapon wep)
{
ActionState state(SkyportAction::Upgrade);
state.myWeapon = wep;
return state;
}
static ActionState CreateMine()
{
return ActionState(SkyportAction::Mine);
}
static ActionState CreateLaser(Direction dir, VectorI2 offset)
{
ActionState state(SkyportAction::Laser);
state.myDirection = dir;
state.myCoordinate = offset;
return state;
}
static ActionState CreateMotar(VectorI2 coord)
{
ActionState state(SkyportAction::Motar);
state.myCoordinate = coord;
return state;
}
static ActionState CreateDroid(Direction *commands, int count)
{
ActionState state(SkyportAction::Droid);
if(count > MaxDroidCommands)
throw Error(Error::InvalidValue, "Too many droid actions");
for(int i = 0; i < count; i++)
state.myCommands[i] = commands[i];
for(int i = count; i < MaxDroidCommands; i++)
state.myCommands[i] = Direction::None;
return state;
}
SkyportAction GetAction() { return myAction; }
Direction GetDirection() { return myDirection; }
Weapon GetWeapon() { return myWeapon; }
VectorI2 GetCoordinate() { return myCoordinate; }
Direction *GetCommands() { return myCommands; }
};
uint StrSum(std::string name);
class PlayerState
{
static const ColorF Colors[16];
public:
uint Index;
std::string Name;
uint Health;
uint Score;
Weapon PrimaryWeapon;
Weapon SecondaryWeapon;
anengine::VectorI2 Position;
ColorF Color;
PlayerState(uint index, std::string name, uint health, uint score,
Weapon primary, Weapon secondary, anengine::VectorI2 pos)
: Index(index), Name(name), Health(health), Score(score),
PrimaryWeapon(primary), SecondaryWeapon(secondary), Position(pos),
Color(Colors[StrSum(name) % 16]) { }
~PlayerState() { }
};
class MapState
{
VectorI2 mySize;
char *myData;
public:
MapState(const MapState &other)
: mySize(other.mySize), myData(new char[mySize[X]*mySize[Y]])
{
std::copy(other.myData, other.myData + mySize[X]*mySize[Y], myData);
}
MapState(VectorI2 size)
: mySize(size), myData(new char[size[X]*size[Y]]) { }
MapState()
: mySize(ZeroI2), myData(new char[0]) { }
~MapState()
{
delete [] myData;
}
MapState &operator= (const MapState &other)
{
delete [] myData;
mySize = other.mySize;
myData = new char[mySize[X]*mySize[Y]];
std::copy(other.myData, other.myData + mySize[X]*mySize[Y], myData);
return *this;
}
VectorI2 GetSize() const
{
return mySize;
}
char operator ()(int j, int k) const
{
return myData[k*mySize[X]+j];
}
char &operator ()(int j, int k)
{
return myData[k*mySize[X]+j];
}
};
class GameState
{
std::vector<PlayerState> myPlayers;
MapState myMap;
int myTurn;
int myActionCount;
int myPlayerIndex;
std::string myTitle;
std::string mySubtitle;
ActionState myActions[3];
public:
typedef std::vector<PlayerState>::const_iterator Players_iterator;
GameState()
: myTurn(-1), myActionCount(0), myPlayerIndex(0) { }
void SetPlayer(std::string name, uint health, uint score, Weapon primary,
Weapon secondary, anengine::VectorI2 pos);
void SetTurn(int turn)
{
if(myTurn == -1 && turn != 0)
throw Error(Error::InvalidValue, "Got no turn 0.");
if(myTurn != -1 && turn == 0)
throw Error(Error::InvalidValue, "Got multiple turn 0.");
myTurn = turn;
myActionCount = 0;
myPlayerIndex = 0;
}
int GetTurn() const
{
return myTurn;
}
Players_iterator Players_begin() const
{
return myPlayers.begin();
}
Players_iterator Players_end() const
{
return myPlayers.end();
}
int PlayerCount() const
{
return myPlayers.size();
}
const MapState &GetMap() const
{
return myMap;
}
void SetMap(const MapState &map)
{
myMap = map;
}
void AddAction(ActionState action)
{
if(myActionCount >= 3)
throw Error(Error::InvalidState, "Too many actions in one turn.");
myActions[myActionCount++] = action;
}
uint GetActionCount() const
{
return myActionCount;
}
const ActionState &GetAction(uint i) const
{
Bug(i >= 3, "Request wrong action");
return myActions[i];
}
void SetTitle(std::string title)
{
myTitle = title;
}
void SetSubtitle(std::string subtitle)
{
mySubtitle = subtitle;
}
std::string GetTitle() const
{
return myTitle;
}
std::string GetSubtitle() const
{
return mySubtitle;
}
bool operator == (const GameState &other) const
{
return false;
}
bool operator != (const GameState &other) const
{
return true;
}
};
const char *GetWeaponName(Weapon wep);
class GameStateEvent : public Event
{
GameState myState;
public:
GameStateEvent(uint event, Entity *source, const GameState &state)
: Event(SkyportEventClass::GameState, event, source), myState(state) { }
const GameState &GetState()
{
return myState;
}
};
#endif