-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
253 lines (217 loc) · 7.17 KB
/
Engine.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
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
#include <assert.h>
#include "Engine.h"
#include "os.h"
#include "ChaserAgent.h"
#include "Config.h"
#include "DinoState.h"
#include "EngineInput.h"
#include "Level.h"
#include "PropSpec.h"
#include "Timer.h"
#include <iostream>
using namespace std;
Engine::Engine() {
currentLevel_ = new Level(1);
init();
}
Engine::Engine(int level) {
currentLevel_ = new Level(level);
init();
}
Engine::~Engine() {
for (int i = 0; i < currentLevel_->getNumNPCs(); ++i) delete agents_[i];
delete levelTimer_;
delete powerupTimer_;
delete [] agents_;
delete currentLevel_;
}
void Engine::init() {
agents_ = new ChaserAgent*[currentLevel_->getNumNPCs()];
for (int i = 0; i < currentLevel_->getNumNPCs(); ++i) {
agents_[i] = new ChaserAgent(*(currentLevel_->getPCState()),
currentLevel_->getNPCState(i));
}
levelTimer_ = new Timer();
numDotsLeft_ = currentLevel_->getDots()->size();
currentEffect_ = PE_NONE;
powerupTimer_ = new Timer();
isModelFrozen_ = false;
isPlayerInvincible_ = false;
firstTick_ = true;
win_ = NULL;
gameOver_ = false;
}
EngineState Engine::evolve(const EngineInput &inputs) {
// Check if we're 'live'.
if (isModelFrozen_) return ES_RUNNING;
if (firstTick_) {
levelTimer_->start();
firstTick_ = false;
}
// Potentially expire any effective powerups.
if (currentEffect_ != PE_NONE &&
powerupTimer_->getTimeInMicroseconds() >= Config::POWERUP_DURATION_IN_MICROSECONDS) {
currentEffect_ = PE_NONE;
powerupTimer_->pause();
}
// Exert inputs on player character.
if (inputs.playerMovingForward())
currentLevel_->getPCState()->setMovingForward();
else if (inputs.playerMovingBackward())
currentLevel_->getPCState()->setMovingBackward();
else
currentLevel_->getPCState()->stopMoving();
if (inputs.playerTurningLeft())
currentLevel_->getPCState()->setTurningLeft();
else if (inputs.playerTurningRight())
currentLevel_->getPCState()->setTurningRight();
else
currentLevel_->getPCState()->stopTurning();
// Move the PC according to the given inputs.
currentLevel_->getPCState()->evolve(currentEffect_);
// PC may have fallen off of the board: If so, constrain PC back into
// the level.
currentLevel_->getPCState()->clipPosition(0, 0, currentLevel_->getWidth(), currentLevel_->getHeight());
for (int i = 0; i < currentLevel_->getNumNPCs(); ++i) {
agents_[i]->control();
currentLevel_->getNPCState(i)->evolve(currentEffect_);
currentLevel_->getNPCState(i)->clipPosition(0, 0, currentLevel_->getWidth(), currentLevel_->getHeight());
}
for (list<PropSpec*>::iterator it = currentLevel_->getProps()->begin();
it != currentLevel_->getProps()->end();
++it) {
PropSpec* p = *it;
// If the PC has staggered into any props, boot it out of there.
if (p->contains(currentLevel_->getPCState()->getOrientation())) {
Orientation clipped = p->clip(currentLevel_->getPCState()->getOrientation());
currentLevel_->getPCState()->setOrientation(clipped);
}
// Same for NPCs.
for (int i = 0; i < currentLevel_->getNumNPCs(); ++i) {
if (p->contains(currentLevel_->getNPCState(i)->getOrientation())) {
Orientation clipped = p->clip(currentLevel_->getNPCState(i)->getOrientation());
currentLevel_->getNPCState(i)->setOrientation(clipped);
}
}
}
// Eat any dots that the player may have run into.
if (!gameOver_) {
for (list<PropSpec*>::iterator d_it = currentLevel_->getDots()->begin();
d_it != currentLevel_->getDots()->end();
++d_it) {
PropSpec* d = *d_it;
if (d->isActive() && d->contains(currentLevel_->getPCState()->getOrientation())) {
d->setActive(false);
numDotsLeft_--;
}
}
}
// Check for game over (in a good way).
if (numDotsLeft_ == 0 && !gameOver_) {
handleWin();
setInvincible(true);
gameOver_ = true;
}
for (list<PropSpec*>::iterator pup_it = currentLevel_->getPowerups()->begin();
pup_it != currentLevel_->getPowerups()->end();
++pup_it) {
PropSpec* pu = *pup_it;
if (pu->isActive() && pu->contains(currentLevel_->getPCState()->getOrientation())) {
// This is hacky, and indicates need for a map or something, but we leave
// it for now since there are only a few powerup types.
pu->setActive(false);
if (pu->getType() == FEAR_POWERUP_PROP) {
currentEffect_ = PE_FEAR;
}
else if (pu->getType() == SPEED_POWERUP_PROP) {
currentEffect_ = PE_SPEED;
}
else if (pu->getType() == FREEZE_POWERUP_PROP) {
currentEffect_ = PE_FREEZE;
}
else {
assert(false);
}
powerupTimer_->reset();
if (!powerupTimer_->running())
powerupTimer_->start();
}
}
// Bump all the dinos together.
for (int i = 0; i < currentLevel_->getNumNPCs(); ++i) {
if (!isPlayerInvincible_ && currentLevel_->getNPCState(i)->isHittingOtherDino(*currentLevel_->getPCState())) {
setInvincible(true);
levelTimer_->pause();
gameOver_ = true;
}
currentLevel_->getNPCState(i)->crashWithDino(currentLevel_->getPCState());
for (int j = 0; j < currentLevel_->getNumNPCs(); ++j) {
if (i != j)
currentLevel_->getNPCState(i)->crashWithDino(currentLevel_->getNPCState(j));
}
}
return getEngineState();
}
EngineState Engine::getEngineState() const {
if (!gameOver_) return ES_RUNNING;
else if (numDotsLeft_ == 0) return ES_WON;
else return ES_LOST;
}
void Engine::toggleInvincible() {
isPlayerInvincible_ = !isPlayerInvincible_;
if (isPlayerInvincible_) cout << "Invincibility ON" << endl;
else cout << "Invincibility OFF" << endl;
}
void Engine::setInvincible(bool to) {
isPlayerInvincible_ = to;
}
void Engine::toggleFrozen() {
if (isModelFrozen_) levelTimer_->start();
else levelTimer_->pause();
isModelFrozen_ = !isModelFrozen_;
}
bool Engine::isFrozen() const {
return isModelFrozen_;
}
const Level& Engine::getCurrentLevel() const {
return *currentLevel_;
}
int Engine::getNumDotsLeft() const {
return numDotsLeft_;
}
long Engine::getMillisecondsSpentOnLevel() const {
return levelTimer_->getTimeInMilliseconds();
}
PowerupEffect Engine::getCurrentPowerup() const {
return currentEffect_;
}
long Engine::getTimeLeftInPowerup() const {
return Config::POWERUP_DURATION_IN_MICROSECONDS -
powerupTimer_->getTimeInMicroseconds();
}
WinRecord* Engine::getWinInfo() const {
assert(win_ != NULL);
return win_;
}
void Engine::handleWin() {
// Stop timing!
levelTimer_->pause();
// Generate win record.
win_ = new WinRecord;
win_->newTime = levelTimer_->getTimeInMilliseconds();
win_->par = currentLevel_->getPar();
win_->level = currentLevel_->getLevelNumber();
if (currentLevel_->getLevelNumber() == Level::getNumLevelsUnlocked() &&
win_->newTime <= currentLevel_->getPar() * 1000) {
win_->unlockedNextLevel = true;
} else {
win_->unlockedNextLevel = false;
}
for (vector<long>::const_iterator it = currentLevel_->getRecordTimes().begin();
it != currentLevel_->getRecordTimes().end();
++it) {
win_->oldTimes.push_back(*it);
}
// Now update data.
currentLevel_->handleWin(levelTimer_->getTimeInMilliseconds());
}