-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trap.cpp
430 lines (373 loc) · 10.4 KB
/
Trap.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "StdAfx.h"
#include "Trap.h"
#include "dxManager.h"
Trap::Trap(dxManager* dx, char* name, float xPos, float yPos) : ICollidable(dx)
{
//get a ref to the dxmanager display device to make sprites here
ID3D10Device* pD3DDevice = dx->getPD3DDevice();
activated = false;
startX = xPosition = xPos;
startY = yPosition = yPos;
isDangerous = true;
moveDist = 0;
crossHairCountdown = 0;
tileSize = 1; //how wide the tiles are; will be set if important using setTileSize
moveDirection = 1;
//if it's a saw...
if(strcmp(name,"Images/saw1.png") == 0)
{
numAnimations = 2;
animationArrayT = new SimpleSprite*[numAnimations];
trapType = 0;
stepTime = 10;
WCHAR* spriteNames[] = {L"Images/saw1.png", L"Images/saw2.png"};
//load saw sprites
for (int i = 0; i < numAnimations; i ++)
{
dx->MakeSprite(spriteNames[i], &animationArrayT[i]);
animationArrayT[i]->Visible = false;
animationArrayT[i]->SetPosition(xPos, yPos, 0);
}
mySprite = animationArrayT[0];
mySprite->Visible = true;
moveDist = 2;
SetWidthHeight();
}
//if its a flame trap
else if(strcmp(name,"Images/flame1.png") == 0)
{
numAnimations = 2;
animationArrayT = new SimpleSprite*[numAnimations];
trapType = 1;
//stepTime = 3000; //3 seconds between switching states; currently being set in maploader instead
WCHAR* spriteNameA[] = {L"Images/flame1.png", L"Images/flame2.png"};
//load sprites
for (int i = 0; i < numAnimations; i ++)
{
dx->MakeSprite(spriteNameA[i], &animationArrayT[i]);
animationArrayT[i]->Visible = false;
animationArrayT[i]->SetPosition(xPos, yPos, 0);
}
mySprite = animationArrayT[0];
mySprite->Visible = true;
isDangerous = false; //starts as off
SetWidthHeight();
}
else if(strcmp(name,"Images/Turret.png") == 0)
{
numAnimations = 1;
animationArrayT = new SimpleSprite*[numAnimations];
trapType = 3;
stepTime = 3000; //3 seconds between switching states; currently being set in maploader instead
WCHAR* spriteNameA[] = {L"Images/Turret.png"};
//load sprites
for (int i = 0; i < numAnimations; i ++)
{
dx->MakeSprite(spriteNameA[i], &animationArrayT[i]);
animationArrayT[i]->Visible = false;
animationArrayT[i]->SetPosition(xPos, yPos, 0);
}
mySprite = animationArrayT[0];
mySprite->Visible = true;
AdjustScale(2.0f,2.0f);
isDangerous = false; //turret doesnt damage, but needs to be true to do collide
SetWidthHeight();
}
else if(strcmp(name,"crosshairs") == 0)
{
numAnimations = 5;
animationArrayT = new SimpleSprite*[numAnimations];
trapType = 4;
stepTime = 200; //0.2 seconds between switching frames
WCHAR* spriteNameA[] = {L"Images/crosshairs.png",L"Images/crosshairs2.png",L"Images/explode1.png",L"Images/explode2.png",L"Images/explode3.png"};
//load sprites
for (int i = 0; i < numAnimations; i ++)
{
dx->MakeSprite(spriteNameA[i], &animationArrayT[i]);
animationArrayT[i]->Visible = false;
animationArrayT[i]->SetPosition(xPos, yPos, 0);
}
mySprite = animationArrayT[0];
mySprite->Visible = false; //starts out invisible
isDangerous = false; //starts out inactive
SetWidthHeight();
}
else if(strcmp(name,"Images/spikes.png") == 0)
{
numAnimations = 1;
animationArrayT = new SimpleSprite*[numAnimations];
trapType = 2;
//stepTime = 3000; //3 seconds between switching states; currently being set in maploader instead
WCHAR* spriteNameA[] = {L"Images/spikes.png"};
//load sprites
for (int i = 0; i < numAnimations; i ++)
{
dx->MakeSprite(spriteNameA[i], &animationArrayT[i]);
animationArrayT[i]->Visible = false;
animationArrayT[i]->SetPosition(xPos, yPos, 0);
}
mySprite = animationArrayT[0];
mySprite->Visible = true;
SetWidthHeight();
}
else if(strcmp(name,"Images/spikesHang.png") == 0)
{
numAnimations = 1;
animationArrayT = new SimpleSprite*[numAnimations];
trapType = 2;
//stepTime = 3000; //3 seconds between switching states; currently being set in maploader instead
WCHAR* spriteNameA[] = {L"Images/spikesHang.png"};
//load sprites
for (int i = 0; i < numAnimations; i ++)
{
dx->MakeSprite(spriteNameA[i], &animationArrayT[i]);
animationArrayT[i]->Visible = false;
animationArrayT[i]->SetPosition(xPos, yPos, 0);
}
mySprite = animationArrayT[0];
mySprite->Visible = true;
SetWidthHeight();
}
movingIndex = 0;
prevTime = GetCurrentTime();
}
//returns the left postion of the trap
float Trap::Left()
{
return xPosition - halfXWidth;
}
void Trap::SetTriggerTarget(Trap* target)
{
triggerTarg = target;
}
//returns the right position of the trap
float Trap::Right()
{
return xPosition + halfXWidth;
}
//returns the top position of the trap
float Trap::Top()
{
return yPosition + halfYWidth;
}
//returns the bottom postion of the trap
float Trap::Bottom()
{
return yPosition - halfYWidth;
}
void Trap::OnPlayerCollide(PlayerActor* collidedPlayer)
{
//send player to last checkpoint on collision
//if its a turret, set off its crosshair
if(trapType == 3)
{
if(!triggerTarg->activated)
{
triggerTarg->Activate();
}
}
else
{
collidedPlayer->Die();
}
}
//sets the current frame of animations
void Trap::SetAnimSprite(int idx)
{
mySprite->Visible = false;
mySprite = animationArrayT[idx];
mySprite->Visible = true;
}
//sets the current frame of animations
void Trap::SetAnimStepTime(int newStepTime)
{
stepTime = newStepTime;
}
bool Trap::trapWillKill()
{
return isDangerous;
}
void Trap::setTileSize(float size)
{
tileSize = size;
}
//updates the player actor in time by the specified
//elapsing of time
void Trap::Update(float timePassed)
{
//sawBlade
if(trapType == 0)
{
currentTime = GetCurrentTime();
int timeElapsed = currentTime - prevTime;
//if its been more than stepTime since our last animation update...
if(timeElapsed >= stepTime)
{
//increment our animation index
movingIndex++;
//loop around if at max (goes 0 1 2 3, with 3 being frame '1' so it alternates. See below.)
if(movingIndex >= numAnimations)
{
movingIndex = 0;
}
//reset time counter
prevTime = currentTime;
//set the new animation sprite
SetAnimSprite(movingIndex);
}
//start moving forwards
Move(moveDirection*0.001f,0);
//if we're at or beyond our moveDistance, move backwards
if(xPosition >= startX + tileSize*moveDist && moveDirection != -1)
{
moveDirection = -1;
}
//if we're at or beyond our starting position, move forwards
if(xPosition <= startX && moveDirection != 1)
{
moveDirection = 1;
}
}
//Flame
if(trapType == 1)
{
currentTime = GetCurrentTime();
int timeElapsed = currentTime - prevTime;
//if its been more than stepTime since our last animation update...
if(timeElapsed >= stepTime)
{
//increment our animation index
movingIndex++;
//loop around if at max (goes 0 1 2 3, with 3 being frame '1' so it alternates. See below.)
if(movingIndex >= numAnimations)
{
movingIndex = 0;
}
//swap whether the trap is on or off
isDangerous = !isDangerous;
//reset time counter
prevTime = currentTime;
//set the new animation sprite
SetAnimSprite(movingIndex);
}
}
if(trapType == 4) //crosshairs
{
if(activated)
{
//time since last animation
currentTime = GetCurrentTime();
int timeElapsed = currentTime - prevTime;
if(timeElapsed >= stepTime)
{
if(crossHairCountdown < 10) // 2 seconds of blinking crosshairs at 200 step
{
//increment our animation index
movingIndex++;
//loop around if at max (goes 0 1 2 3, with 3 being frame '1' so it alternates. See below.)
if(movingIndex >= 2)
{
movingIndex = 0;
}
}
else if(crossHairCountdown < 15)
{
//if we're at 10, start the explosion animation
if(crossHairCountdown == 10)
{
movingIndex = 2;
}
else
{
//increment our animation index
movingIndex++;
}
isDangerous = true;
//loop around if at max (goes 0 1 2 3, with 3 being frame '1' so it alternates. See below.)
if(movingIndex >= 5)
{
movingIndex = 2;
}
}
//cooldown time between 15-20 where it can't be activated
if(crossHairCountdown >= 20)
{
activated = false;
crossHairCountdown = 0;
prevTime = 0;
}
//if its above 15, make it invisible and non dangerous
else if(crossHairCountdown >= 15)
{
//reset time counter
prevTime = currentTime;
//when our countdown is at 15, stop the animation and reset the trap
movingIndex = 0;
mySprite->Visible = false; //make crosshairs visible
mySprite = animationArrayT[0];
isDangerous = false;
crossHairCountdown++;
}
//if < 15...
else
{
//reset time counter
prevTime = currentTime;
//increment our 'countdown' until explosion
crossHairCountdown++;
//set the new animation sprite
SetAnimSprite(movingIndex);
}
}
}
}
}
//Multiplies the current scale by the specified amount.
void Trap::AdjustScale(float x, float y)
{
mySprite->AdjustScaling(x,y);
}
//retrieve x of trap
float Trap::getX()
{
return xPosition;
}
//retrieve y of trap
float Trap::getY()
{
return yPosition;
}
//activate this trap (crosshairs)
void Trap::Activate()
{
mySprite = animationArrayT[0];
mySprite->Visible = true; //make crosshairs visible
isDangerous = false;
crossHairCountdown = 0;
prevTime = GetCurrentTime();
activated = true;
}
//adjust the scale for all the animation frames
void Trap::FixFrameScales(float adjustX, float adjustY)
{
//cycles through the sprites to resize
for(int i=0; i < numAnimations; i++)
{
float adjustX2 = 1.0f;
float adjustY2 = 1.0f;
int height = animationArrayT[i]->spriteHeight;
int width = animationArrayT[i]->spriteWidth;
if(width > height)
{
adjustY2 = ((float)height)/width;
}
else if(width < height)
{
adjustX2 = ((float)width)/height;
}
animationArrayT[i]->AdjustScaling(adjustX2, adjustY2);
}
}
Trap::~Trap(void)
{
}