This repository was archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaser.cpp
More file actions
407 lines (354 loc) · 12.6 KB
/
Copy pathLaser.cpp
File metadata and controls
407 lines (354 loc) · 12.6 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Members: Garrett Young, Nick Tallents, Oliver Spryn, Zac Stahl
* Course: 322, Obj. Oriented / Adv. Programming
* Date: 04 Dec 2013
* Description: This file implements the Laser class
*/
#include "Laser.h"
/**
* CONSTRUCTOR
*
* This constructor initializes all of the variables and default
* settings for this Laser object.
*
* The color of the laser is initialized to red and will not self
* destruct, by default.
*
* @access public
* @param Game* game A pointer to the controlling Game object
* @param Graphics* graphics A pointer to the Graphics objectstore
* @param DWORD color The color of the laser graphic
* @return void
*/
Laser::Laser(Game* game, Graphics* graphics, DWORD color) : collisions(0), color(color), destroyConst(0), destroyCounter(0), destroyMethod(Laser::NO_DESTROY), destroyPending(false), game(game), graphics(graphics), lastPixelIndex(0), timeConst(1000.0 / CLOCKS_PER_SEC), timeStart(0), tailImages(laserNS::TAIL, Image()) {
this->collisionType = entityNS::ROTATED_BOX;
this->mass = 1.0f;
this->spriteData.height = laserNS::HEAD_HEIGHT;
this->spriteData.scale = 1.0f;
this->spriteData.width = laserNS::HEAD_WIDTH;
this->spriteData.x = laserNS::X;
this->spriteData.y = laserNS::Y;
this->velocity.x = laserNS::VELOCITY;
this->velocity.y = laserNS::VELOCITY;
}
/**
* Reactivates the laser head and tail and places the laser
* at a given starting position. It will also start a clock
* to keep track of amount of time that the laser has been
* active, if the laser is set to self destruct after a certain
* period of time.
*
* @access private
* @param float x The X coordinate to place the laser
* @param float y The Y coordinate to place the laser
* @return void
*/
void Laser::activate(float x, float y) {
//Activate the head pixel of the laser and set its position
this->setActive(true);
this->setVisible(true);
this->setX(x);
this->setY(y);
//Activate each of the tail pixels and set their position
for (vector<Image>::iterator it = this->tailImages.begin(); it != this->tailImages.end(); ++it) {
it->setX(x);
it->setY(y);
it->setVisible(true);
}
//Start the clock
if (this->destroyMethod == Laser::TIMER_DESTROY)
this->timeStart = clock();
}
/**
* DEGREE MODE
*
* Change the direction of the laser while it is in motion, without
* changing its location. The laser must have been already fired and
* active. This will not fire the laser if it is inactive.
*
* This method follows the standards of the cartesian corrdinate system,
* where an angle of 0 degrees will point along the positive X axis, 90
* degrees will point along the positive Y axis, 180 degrees will point
* along the negative X axis, etc...
*
* @access public
* @param float angle The angle to fire the laser, in degrees
* @return void
*/
void Laser::changeDirDeg(float angle) {
//Calculate the angle of the velocity vector
this->velocity.x = laserNS::VELOCITY * sin(static_cast<float>(PI) / 180.0f * (90.0f + angle));
this->velocity.y = laserNS::VELOCITY * cos(static_cast<float>(PI) / 180.0f * (90.0f + angle));
}
/**
* RADIAN MODE
*
* Change the direction of the laser while it is in motion, without
* changing its location. The laser must have been already fired and
* active. This will not fire the laser if it is inactive.
*
* This method follows the standards of the cartesian corrdinate system,
* where an angle of 0 degrees will point along the positive X axis, 90
* degrees will point along the positive Y axis, 180 degrees will point
* along the negative X axis, etc...
*
* @access public
* @param float angle The angle to fire the laser, in degrees
* @return void
*/
void Laser::changeDirRad(float angle) {
//Calculate the angle of the velocity vector
this->velocity.x = laserNS::VELOCITY * sin(static_cast<float>(PI) / 2.0f + angle);
this->velocity.y = laserNS::VELOCITY * cos(static_cast<float>(PI) / 2.0f + angle);
}
/**
* Determines whether or not the laser should self destruct.
*
* @access private
* @return bool Whether or not the laser has destroyed itself
*/
bool Laser::checkDestory() {
//Do not destroy the laser while in this state
if (this->destroyMethod == Laser::NO_DESTROY)
return false;
//Destroy the laser after N number of collisions
if (this->destroyMethod == Laser::COLLISION_DESTROY && this->collisions >= this->destroyConst) {
this->destroy();
return true;
}
//Destroy the laser after N milliseconds
if (this->destroyMethod == Laser::TIMER_DESTROY && (clock() - this->timeStart) * this->timeConst >= this->destroyConst) {
this->destroy();
return true;
}
return false;
}
/**
* DOES NOT DESTROY THE LASER.
*
* Sets an internal flag for the laser to be destroyed. The laser
* is not fully destroyed until the tail of the laser has collapsed.
*
* @access public
* @return void
*/
void Laser::destroy() {
this->destroyPending = true;
}
/**
* Draw the laser head and tail sprites on the screen.
*
* @access public
* @return void
*/
void Laser::draw() {
Image::draw(this->color);
for (vector<Image>::iterator it = this->tailImages.begin(); it != this->tailImages.end(); ++it) {
it->draw(this->color);
}
}
/**
* Called once tail of the laser has fully collapsed. This method will
* deactivate and hide the laser head and tail, and reset all of the
* variables which have been used to track the state of the laser.
*
* In other words, this function removes the laser from the screen and
* prepares it firing again later.
*
* @access private
* @return void
*/
void Laser::finalizeDestruction() {
//Disable the laser and tail
this->setActive(false);
this->setVisible(false);
for (vector<Image>::iterator it = this->tailImages.begin(); it != this->tailImages.end(); ++it) {
it->setVisible(false);
}
//Reset some of the variables used to track the state of the laser
this->collisions = 0;
this->destroyCounter = 0;
this->destroyPending = false;
this->lastPixelIndex = 0;
}
/**
* DEGREE MODE
*
* Reactivates the laser and fires it from a given X and Y position
* at a given angle.
*
* This method follows the standards of the cartesian corrdinate system,
* where an angle of 0 degrees will point along the positive X axis, 90
* degrees will point along the positive Y axis, 180 degrees will point
* along the negative X axis, etc...
*
* @access public
* @param float x The X coordinate to place the laser
* @param float y The Y coordinate to place the laser
* @param float angle The angle to fire the laser, in degrees
* @return void
*/
void Laser::fireDeg(float x, float y, float angle) {
if (!this->active) {
//Calculate the angle of the velocity vector
this->velocity.x = laserNS::VELOCITY * sin(static_cast<float>(PI) / 180.0f * (90.0f + angle));
this->velocity.y = laserNS::VELOCITY * cos(static_cast<float>(PI) / 180.0f * (90.0f + angle));
//Activate the laser
this->activate(x, y);
}
}
/**
* RADIAN MODE
*
* Reactivates the laser and fires it from a given X and Y position
* at a given angle.
*
* This method follows the standards of the cartesian corrdinate system,
* where an angle of 0 radians will point along the positive X axis, PI/2
* radians will point along the positive Y axis, PI radians will point
* along the negative X axis, etc...
*
* @access public
* @param float x The X coordinate to place the laser
* @param float y The Y coordinate to place the laser
* @param float angle The angle to fire the laser, in radians
* @return void
*/
void Laser::fireRad(float x, float y, float angle) {
if (!this->active) {
//Calculate the angle of the velocity vector
this->velocity.x = laserNS::VELOCITY * sin(static_cast<float>(PI) / 2.0f + angle);
this->velocity.y = laserNS::VELOCITY * cos(static_cast<float>(PI) / 2.0f + angle);
//Activate the laser
this->activate(x, y);
}
}
/**
* Return the number of collisions the laser has experienced since
* it was last fired.
*
* @access public
* @return int The number of collisions the laser has experienced
*/
int Laser::getCollisions() {
return this->collisions;
}
/**
* Increment the number of collisions the laser has experienced.
*
* @access public
* @param int number The number by which to incremement the collision counter
* @return void
*/
void Laser::increaseCollision(int number) {
this->collisions += number;
}
/**
* Initialize the Laser object and each of the objects which
* constitue the tail.
*
* @access public
* @param Game* gamePtr A pointer to the Game object which controls the game logic
* @param int width The width of the head of laser
* @param int height The height of the head of the laser
* @param int ncols The number of columns in the texture
* @param TextureManager* textureM The object which contains the texture which is used by this object
* @return void
*/
bool Laser::initialize() {
//Initialize the texture
tm.initialize(this->graphics, laserNS::GRAPHIC);
//Initialize the head and tail objects
bool retVal = Entity::initialize(this->game, laserNS::HEAD_WIDTH, laserNS::HEAD_HEIGHT, 1, &tm);
this->setActive(false);
this->setVisible(false);
for (vector<Image>::iterator it = this->tailImages.begin(); it != this->tailImages.end(); ++it) {
it->initialize(this->graphics, laserNS::HEAD_WIDTH, laserNS::HEAD_HEIGHT, 1, &tm);
it->setX(laserNS::X);
it->setY(laserNS::Y);
it->setVisible(false);
}
return retVal;
}
/**
* Set the method by which the laser will self destruct.
*
* For the first parameter, there are three possible options:
* - Laser::COLLISION_DESTROY - Self destruct after N collisions
* - Laser::NO_DESTROY - Do not self destruct (default)
* - Laser::TIMER_DESTROY - Self destruct after N milliseconds
*
* For the second parameter, there are three possible options:
* - Number of collisions which must occur before doing a self-
* destruct [if using the Laser::COLLISION_DESTROY option]
* - No value is needed if using the LASER::NO_DESTROY option
* - Number of milliseconds which must occur before doing a self-
* destruct [if using the Laser::TIMER_DESTROY option]
*
* @access public
* @param char method The method by which the laser will self-destruct
* @param int destroyConst The number of collisions or milliseconds after which the laser will self-destruct
* @return void
*/
void Laser::setSelfDestructMethod(char method, int destroyConst) {
if (method == Laser::COLLISION_DESTROY || method == Laser::NO_DESTROY || method == Laser::TIMER_DESTROY)
this->destroyMethod = method;
if (destroyConst >= 0)
this->destroyConst = destroyConst;
}
/**
* This method will update the laser at each frame and perform the
* following actions:
* - Check to see if the laser should self-destruct.
* - Update the position of the head of the laser.
* - Check the head for collisions against the edge of the window.
* - Update the collision counter when the laser hits the edge.
* - Update the tail to follow the path of the head of the laser.
* - Collapse the tail of the laser if it is set for destruction.
* - Hide the laser after it has been fully destroyed.
*
* @access public
* @param float frameTime A constant used to maintain the speed of moving objects
* @return void
*/
void Laser::update(float frameTime) {
Entity::update(frameTime);
//Check to see if this laser should be destroyed
this->checkDestory();
//Update the tail location
this->tailImages[this->lastPixelIndex].setX(this->spriteData.x);
this->tailImages[this->lastPixelIndex].setY(this->spriteData.y);
this->lastPixelIndex = (this->lastPixelIndex == laserNS::TAIL - 1 ? 0 : ++this->lastPixelIndex);
if (!this->destroyPending) {
//Update the location of the head
this->spriteData.x += frameTime * this->velocity.x;
this->spriteData.y += frameTime * this->velocity.y;
//Detect collisions against the edge of the window
if (this->spriteData.x > GAME_WIDTH - laserNS::HEAD_WIDTH) { // Right edge
this->spriteData.x = GAME_WIDTH - laserNS::HEAD_WIDTH;
this->velocity.x = -this->velocity.x;
++this->collisions;
this->destroy();
} else if (this->spriteData.x < 0) { // Left edge
this->spriteData.x = 0;
this->velocity.x = -this->velocity.x;
++this->collisions;
this->destroy();
}
if (this->spriteData.y > GAME_HEIGHT - laserNS::HEAD_HEIGHT) { // Bottom edge
this->spriteData.y = GAME_HEIGHT - laserNS::HEAD_HEIGHT;
this->velocity.y = -this->velocity.y;
++this->collisions;
this->destroy();
} else if (spriteData.y < 0) { // Top edge
this->spriteData.y = 0;
this->velocity.y = -this->velocity.y;
++this->collisions;
this->destroy();
}
} else {
if (++this->destroyCounter == laserNS::TAIL) {
this->finalizeDestruction();
}
}
}