-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmc.c
843 lines (738 loc) · 23.1 KB
/
mc.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/**
* mc.c
* By Henry Zhu (henryzhu@seas.upenn.edu) and Jessica Yingjiao Li
*/
#include "lc4libc.h"
/************************************************
* DATA STRUCTURES FOR GAME STATE
***********************************************/
/** Width and height of lc4 */
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 124
/** Number of cities and missile commands in the game */
#define NUMOFCITIES 2
#define NUMOFMISSILECOMMANDS 1
/** Number of targets meteors can target */
#define NUMOFTARGETS NUMOFCITIES + NUMOFMISSILECOMMANDS
/** Delay before another meteor appears */
static int delay_counter = 0;
#define DELAY 5
/** wave_counter counts of the wave that the player is on */
static int wave_counter = 1;
/** Number of meteors per wave */
static int meteors_left_per_wave;
static int meteors_per_wave;
#define NUM_METEORS_PER_WAVE 5;
/** Cooldown timer between missile launches */
static int cooldown_counter =0;
#define COOLDOWN_DELAY 5
/** Number of maximum missiles and meteors allowed on the screen */
#define MAX_MISSILES 3
#define MAX_METEORS 3
/** Number of missiles allowed per wave*/
#define MISSILES_PER_WAVE 8
/** The y position of where the cities and missile commands are located */
#define GROUND_LEVEL 116
/** The x positions of where the cities and missile commands are located */
#define LEFT_CITY_XPOS 20
#define RIGHT_CITY_XPOS 100
#define MISSILE_COMMAND_XPOS 60
/** The squared distance where a meteor will blow up if it is in the raidus */
#define BLOWUP_RADIUS 250
/** Delay between GETC_TIMER */
#define GETC_TIMER_DELAY 10
/** 2D array presenting the cursor */
lc4uint cursorImage[] = {
0x00,
0x00,
0x18,
0x3C,
0x3C,
0x18,
0x00,
0x00
};
/** Array of targets that the meteors will target */
lc4uint targets[NUMOFTARGETS];
/** City struct that consists of the x position and 2D array representing the city's image */
typedef struct {
lc4bool isDestroyed;
lc4uint x;
lc4uint cityImage[8];
}City;
/** Array consisting of cities */
City cities[NUMOFCITIES];
/** Missile Command Struct that consists of the number missiles that the command has left,
its x position and 2D array representing the missile command's image */
typedef struct {
lc4bool isDestroyed;
int missilesLeft;
int x;
lc4uint commandImage[8];
}MissileCommand;
/** Instance of the MissileCommand Struct */
MissileCommand missileCommand;
/************************************************
* Projectile Struct
* lc4bool isDestroyed - boolean representing if the projectile is destroyed or not
* lc4uint fromX, fromY - uints representing the projectile's starting location when the projectile is fired
* lc4uint currentX, currentY - uints representing the projectile's current location from the starting location
* lc4uint fromX, fromY - uints representing the projectile's final location where the projectile will explode
* int dx1, dx2, dy1, dx2, - ints representing the direction the projectile will travel
* int startLongest, longest, shortest, numerator - each is a part of the line algorithm
***********************************************/
typedef struct {
lc4bool isDestroyed;
lc4uint fromX, fromY, currentX, currentY, finalX, finalY;
int dx1;
int dy1;
int dx2;
int dy2;
int startLongest;
int longest;
int shortest;
int numerator;
}Projectile;
/** Array consisting of missiles on the screen */
Projectile missiles[MAX_MISSILES];
/** Array consisting of meteors on the screen */
Projectile meteors[MAX_METEORS];
/** Cursor struct consisting of the cursor's x and y position */
typedef struct{
int x;
int y;
}Cursor;
/** Instance of the cursor*/
Cursor cursor;
/************************************************
* Printnum - Useful for debugging
* Prints out the value on the lc4
***********************************************/
void printnum (int n) {
int abs_n;
char str[10], *ptr;
// Corner case (n == 0)
if (n == 0) {
lc4_puts ((lc4uint*)"0");
return;
}
abs_n = (n < 0) ? -n : n;
// Corner case (n == -32768) no corresponding +ve value
if (abs_n < 0) {
lc4_puts ((lc4uint*)"-32768");
return;
}
ptr = str + 10; // beyond last character in string
*(--ptr) = 0; // null termination
while (abs_n) {
*(--ptr) = (abs_n % 10) + 48; // generate ascii code for digit
abs_n /= 10;
}
// Handle -ve numbers by adding - sign
if (n < 0) *(--ptr) = '-';
lc4_puts((lc4uint*)ptr);
}
void endl () {
lc4_puts((lc4uint*)"\n");
}
/************************************************
* UpdateLine -
* Draws the line from its start location to the current location if the projectile is not destroyed
***********************************************/
void UpdateLine(Projectile projectile, lc4uint color)
{
if(projectile.currentX == projectile.finalX && projectile.currentY == projectile.finalY) return;
lc4_draw_line(projectile.fromX, projectile.fromY, projectile.currentX, projectile.currentY, color);
}
/************************************************
* DrawCursor -
* Draws the cursor sprite in white
***********************************************/
void DrawCursor()
{
lc4_draw_sprite(cursor.x, cursor.y, (lc4uint)WHITE, &cursorImage[0]);
}
/************************************************
* DrawMissileCommand -
* Draws the missile command in white based on the number of missiles left
***********************************************/
void DrawMissileCommand()
{
if(missileCommand.missilesLeft == 8) {
missileCommand.commandImage[0] = 0x42;
missileCommand.commandImage[1] = 0xA5;
missileCommand.commandImage[2] = 0x42;
missileCommand.commandImage[3] = 0xA5;
missileCommand.commandImage[4] = 0x42;
missileCommand.commandImage[5] = 0xA5;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 7) {
missileCommand.commandImage[0] = 0x02;
missileCommand.commandImage[1] = 0x05;
missileCommand.commandImage[2] = 0x42;
missileCommand.commandImage[3] = 0xA5;
missileCommand.commandImage[4] = 0x42;
missileCommand.commandImage[5] = 0xA5;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 6) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x42;
missileCommand.commandImage[3] = 0xA5;
missileCommand.commandImage[4] = 0x42;
missileCommand.commandImage[5] = 0xA5;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 5) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x02;
missileCommand.commandImage[3] = 0x05;
missileCommand.commandImage[4] = 0x42;
missileCommand.commandImage[5] = 0xA5;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 4) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x00;
missileCommand.commandImage[3] = 0x00;
missileCommand.commandImage[4] = 0x42;
missileCommand.commandImage[5] = 0xA5;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 3) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x00;
missileCommand.commandImage[3] = 0x00;
missileCommand.commandImage[4] = 0x02;
missileCommand.commandImage[5] = 0x05;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 2) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x00;
missileCommand.commandImage[3] = 0x00;
missileCommand.commandImage[4] = 0x00;
missileCommand.commandImage[5] = 0x00;
missileCommand.commandImage[6] = 0x42;
missileCommand.commandImage[7] = 0xA5;
} else if(missileCommand.missilesLeft == 1) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x00;
missileCommand.commandImage[3] = 0x00;
missileCommand.commandImage[4] = 0x00;
missileCommand.commandImage[5] = 0x00;
missileCommand.commandImage[6] = 0x02;
missileCommand.commandImage[7] = 0x05;
} else if(missileCommand.missilesLeft == 0) {
missileCommand.commandImage[0] = 0x00;
missileCommand.commandImage[1] = 0x00;
missileCommand.commandImage[2] = 0x00;
missileCommand.commandImage[3] = 0x00;
missileCommand.commandImage[4] = 0x00;
missileCommand.commandImage[5] = 0x00;
missileCommand.commandImage[6] = 0x00;
missileCommand.commandImage[7] = 0x00;
}
lc4_draw_sprite(missileCommand.x, GROUND_LEVEL, (lc4uint)WHITE, &missileCommand.commandImage[0]);
}
/************************************************
* DrawCities -
* Draws the cities in white
***********************************************/
void DrawCities()
{
int i = 0;
for(i = 0; i < NUMOFCITIES; i++)
{
lc4_draw_sprite(cities[i].x, GROUND_LEVEL, (lc4uint)RED, &cities[i].cityImage[0]);
}
}
/************************************************
* DrawMeteors -
* Draws each meteor (if it is not destroyed)
***********************************************/
void DrawMeteors()
{
int i = 0;
for(i = 0; i < MAX_METEORS; i++)
{
if(meteors[i].isDestroyed == FALSE)
{
lc4_draw_line(meteors[i].fromX, meteors[i].fromY,
meteors[i].currentX, meteors[i].currentY, (lc4uint)YELLOW);
}
}
}
/************************************************
* DrawMissiles
* Dras each missile (if it is not destroyed)
***********************************************/
void DrawMissiles()
{
int i = 0;
for(i = 0; i < MAX_MISSILES; i++)
{
if(missiles[i].isDestroyed == FALSE)
{
lc4_draw_line(missiles[i].fromX, missiles[i].fromY,
missiles[i].currentX, missiles[i].currentY, (lc4uint)GREEN);
}
}
}
/************************************************
* Redraw -
* Assuming that the PennSim is run with double buffered mode, (using the command -> (java -jar PennSim.jar -d))
* First, we should clear the video memory buffer using lc4_reset_vmem
* Then we draw the scene and then swap the video memory buffer using lc4_blt_vmem
***********************************************/
void Redraw()
{
lc4_reset_vmem();
DrawCursor();
DrawMissileCommand();
DrawCities();
DrawMissiles();
DrawMeteors();
lc4_blt_vmem();
}
/************************************************
* Reset -
* Clears the screen
***********************************************/
void reset()
{
lc4_reset_vmem();
lc4_blt_vmem();
}
/************************************************
* ResetGame -
* Resets the game.
* For each missile command, reset the number of missiles, position and image
* For each city, reset position and reset the image
* For each projectile (meteor/missile), ensure that they are destroyed
* Ensure that that the targets the meteors will be aiming at are correct
***********************************************/
void ResetGame()
{
int i = 0;
for(i = 0; i < NUMOFMISSILECOMMANDS; i++)
{
missileCommand.isDestroyed = FALSE;
missileCommand.missilesLeft = 8;
missileCommand.x = MISSILE_COMMAND_XPOS;
}
cities[0].x = LEFT_CITY_XPOS;
cities[1].x = RIGHT_CITY_XPOS;
for(i = 0; i < NUMOFCITIES; i++)
{
cities[i].isDestroyed = FALSE;
cities[i].cityImage[0] = 0x18;
cities[i].cityImage[1] = 0x18;
cities[i].cityImage[2] = 0x3B;
cities[i].cityImage[3] = 0x3B;
cities[i].cityImage[4] = 0x7D;
cities[i].cityImage[5] = 0x7D;
cities[i].cityImage[6] = 0xFF;
cities[i].cityImage[7] = 0xFF;
}
for(i = 0; i < MAX_MISSILES; i++)
{
missiles[i].isDestroyed = TRUE;
}
for(i = 0; i < MAX_METEORS; i++)
{
meteors[i].isDestroyed = TRUE;
}
targets[0] = LEFT_CITY_XPOS;
targets[1] = MISSILE_COMMAND_XPOS;
targets[2] = RIGHT_CITY_XPOS;
wave_counter = 1;
meteors_per_wave = NUM_METEORS_PER_WAVE;
meteors_left_per_wave = meteors_per_wave;
}
/************************************************
* abs -
* returns the absolute value
***********************************************/
int abs(int value)
{
if(value < 0)
return -value;
return value;
}
/************************************************
* BlowUpMeteor -
* Set the index meteor to being destroyed
***********************************************/
void BlowUpMeteor(int index)
{
meteors[index].isDestroyed = TRUE;
meteors_left_per_wave--;
}
/************************************************
* CheckForMeteorCollsion -
* Destroys the building if the meteor lands on it
***********************************************/
void CheckForMeteorCollsion(int i)
{
//destroy the building
if(meteors[i].finalX == LEFT_CITY_XPOS)
{
cities[0].isDestroyed = TRUE;
cities[0].cityImage[0] = 0x00;
cities[0].cityImage[1] = 0x00;
cities[0].cityImage[2] = 0x00;
cities[0].cityImage[3] = 0x00;
cities[0].cityImage[4] = 0x7F;
cities[0].cityImage[5] = 0xCF;
cities[0].cityImage[6] = 0xFF;
cities[0].cityImage[7] = 0xFF;
}
else if(meteors[i].finalX == RIGHT_CITY_XPOS)
{
cities[1].isDestroyed = TRUE;
cities[1].cityImage[0] = 0x00;
cities[1].cityImage[1] = 0x00;
cities[1].cityImage[2] = 0x00;
cities[1].cityImage[3] = 0x00;
cities[1].cityImage[4] = 0x7F;
cities[1].cityImage[5] = 0xCF;
cities[1].cityImage[6] = 0xFF;
cities[1].cityImage[7] = 0xFF;
}
else
{
missileCommand.isDestroyed = TRUE;
missileCommand.missilesLeft = 0;
}
BlowUpMeteor(i);
}
/************************************************
* BlowUpMissile -
* Check if each meteor is in the blow up radius and mark
* the meteor as destroyed if the meteor is in the radius
* Make sure to mark the missile as destroyed as well
***********************************************/
void BlowUpMissile(int index)
{
int i = 0;
for(i = 0; i < MAX_METEORS; i++)
{
if(meteors[i].isDestroyed == FALSE)
{
if((abs((meteors[i].currentX - missiles[index].finalX) *
(meteors[i].currentX - missiles[index].finalX)) +
((meteors[i].currentY - missiles[index].finalY) *
(meteors[i].currentY - missiles[index].finalY))) < BLOWUP_RADIUS)
{
BlowUpMeteor(i);
}
}
}
missiles[index].isDestroyed = TRUE;
}
/************************************************
* UpdateProjectile -
* This function draws new lines each frame, so that the
* missiles and meteors look like they are animated
* This implementation functions exactly like the line
* algorithm where the dx and dy are applied to each
* projectile.
* For the meteors, ensure that there is a longer delay before
* the meteor moves by using the delay counter.
***********************************************/
void UpdateProjectiles()
{
int i = 0;
for(i = 0; i < MAX_MISSILES; i++)
{
if(missiles[i].isDestroyed == FALSE)
{
missiles[i].numerator += missiles[i].shortest;
if(missiles[i].numerator >= missiles[i].longest)
{
missiles[i].numerator -= missiles[i].longest;
missiles[i].currentX += missiles[i].dx1;
missiles[i].currentY += missiles[i].dy1;
} else {
missiles[i].currentX += missiles[i].dx2;
missiles[i].currentY += missiles[i].dy2;
}
if(missiles[i].startLongest > missiles[i].longest)
{
BlowUpMissile(i);
}
missiles[i].startLongest++;
}
}
if(delay_counter == DELAY)
{
for(i = 0; i < MAX_METEORS; i++)
{
if(meteors[i].isDestroyed == FALSE)
{
meteors[i].numerator += meteors[i].shortest;
if(meteors[i].numerator >= meteors[i].longest)
{
meteors[i].numerator -= meteors[i].longest;
meteors[i].currentX += meteors[i].dx1;
meteors[i].currentY += meteors[i].dy1;
} else {
meteors[i].currentX += meteors[i].dx2;
meteors[i].currentY += meteors[i].dy2;
}
if(meteors[i].startLongest > meteors[i].longest)
{
CheckForMeteorCollsion(i);
}
meteors[i].startLongest++;
}
}
delay_counter = 0;
} else {
delay_counter++;
}
}
/************************************************
* CalculateMissileMovement -
* This function calculates the dx and dy and other necessary variables
* required in the line algrothm for each missile at an index
***********************************************/
void CalculateMissileMovement(int i)
{
int w = missiles[i].finalX - missiles[i].fromX;
int h = missiles[i].finalY - missiles[i].fromY;
int dx1 = 0;
int dy1 = 0;
int dx2 = 0;
int dy2 = 0;
if (w<0) dx1 = -1;
if (w>0) dx1 = 1;
if (h<0) dy1 = -1;
if (h>0) dy1 = 1;
if (w<0) dx2 = -1;
if (w>0) dx2 = 1;
missiles[i].longest = abs(w);
missiles[i].shortest = abs(h);
if(missiles[i].longest <= missiles[i].shortest)
{
missiles[i].longest = abs(h);
missiles[i].shortest = abs(w);
if (h<0) dy2 = -1;
if (h>0) dy2 = 1;
dx2 = 0;
}
missiles[i].dx1 = dx1;
missiles[i].dy1 = dy1;
missiles[i].dx2 = dx2;
missiles[i].dy2 = dy2;
missiles[i].numerator = missiles[i].longest >> 1;
missiles[i].startLongest = 0;
}
/************************************************
* CalculateMeteorMovement -
* This function does the same thing as CalculateMissileMovement, but
* is for meteors
***********************************************/
void CalculateMeteorMovement(int i)
{
int w = meteors[i].finalX - meteors[i].fromX;
int h = meteors[i].finalY - meteors[i].fromY;
int dx1 = 0;
int dy1 = 0;
int dx2 = 0;
int dy2 = 0;
if (w<0) dx1 = -1;
if (w>0) dx1 = 1;
if (h<0) dy1 = -1;
if (h>0) dy1 = 1;
if (w<0) dx2 = -1;
if (w>0) dx2 = 1;
meteors[i].longest = abs(w);
meteors[i].shortest = abs(h);
if(meteors[i].longest <= meteors[i].shortest)
{
meteors[i].longest = abs(h);
meteors[i].shortest = abs(w);
if (h<0) dy2 = -1;
if (h>0) dy2 = 1;
dx2 = 0;
}
meteors[i].dx1 = dx1;
meteors[i].dy1 = dy1;
meteors[i].dx2 = dx2;
meteors[i].dy2 = dy2;
meteors[i].numerator = meteors[i].longest >> 1;
meteors[i].startLongest = 0;
}
/************************************************
* CreateNewMissile -
* This function creates a new missile when the user presses the fire button.
* This function should check if the missile is destroyed and set the starting
* and final location. In addition, it should also calculate the dx and dy
* by calling CalculateMissileMovement.
***********************************************/
void CreateNewMissile()
{
int i = 0;
for(i = 0; i < MAX_MISSILES; i++)
{
if(missiles[i].isDestroyed == TRUE)
{
missiles[i].isDestroyed = FALSE;
missiles[i].fromX = missileCommand.x + 4;
missiles[i].fromY = GROUND_LEVEL + 4;
missiles[i].currentX = missiles[i].fromX;
missiles[i].currentY = missiles[i].fromY;
missiles[i].finalX = cursor.x;
missiles[i].finalY = cursor.y;
CalculateMissileMovement(i);
missileCommand.missilesLeft--;
return;
}
}
}
/************************************************
* CreateNewMeteors -
* This function creates a new meteor like how CreateNewMissile works, but only if the meteors left during the wave is greater than 0.
***********************************************/
void CreateNewMeteors()
{
int i = 0;
for(i = 0; i < MAX_METEORS; i++)
{
if(meteors[i].isDestroyed == TRUE)
{
meteors[i].isDestroyed = FALSE;
meteors[i].fromX = rand16();
meteors[i].fromY = 0;
meteors[i].currentX = meteors[i].fromX;
meteors[i].currentY = meteors[i].fromY;
meteors[i].finalX = targets[(rand16() * 3)/128];
meteors[i].finalY = GROUND_LEVEL;
CalculateMeteorMovement(i);
}
}
}
/************************************************
* CreateNewMeteors -
* This function executes cursor movement and firing the missile based on the keyboard input.
* If the input is 'a', then move the cursor to the right
* If the input is 'd', then move the cursor to the left
* If the input is 'w', then move the cursor to the up
* If the input is 's', then move the cursor to the down
* Ensure that the cursor will not go out of bounds (ie. beyond the screen height/width)
* If the input is 'r', then create a new missile that will fly to the final position
* Ensure that the there is a cooldown interval between each fire
***********************************************/
void ExecuteInput(lc4int input)
{
if (input == 'a')
{
cursor.x--;
if(cursor.x < 0) cursor.x = 0;
}
else if (input == 'd')
{
cursor.x++;
if(cursor.x == SCREEN_WIDTH) cursor.x = SCREEN_WIDTH - 1;
}
else if (input == 'w')
{
cursor.y--;
if(cursor.y < 0) cursor.y = 0;
}
else if (input == 's')
{
cursor.y++;
if(cursor.y == SCREEN_HEIGHT) cursor.y = SCREEN_HEIGHT - 1;
}
else if (input == 'r' && missileCommand.isDestroyed == FALSE && missileCommand.missilesLeft > 0)
{
if(cooldown_counter > COOLDOWN_DELAY)
{
CreateNewMissile();
}
}
cooldown_counter++;
}
int rand16 ()
{
int lfsr;
// Advance the lfsr seven times
lfsr = lc4_lfsr();
lfsr = lc4_lfsr();
lfsr = lc4_lfsr();
lfsr = lc4_lfsr();
lfsr = lc4_lfsr();
lfsr = lc4_lfsr();
lfsr = lc4_lfsr();
// return the last 7 bits
return (lfsr & 0x7F);
}
/************************************************
* PrepareForNextWave -
* If the meteors per wave is less than or equal to 0, then
* increment the number of meteors per wave and set the number of meteors left for that wave to that number
* Increment the current wave number and print that onto the screen
* and reset the number of missile left for the missile command and redraw the missileCommand if it was destroyed.
***********************************************/
void PrepareForNextWave()
{
if(meteors_left_per_wave <= 0)
{
int i = 0;
for(i = 0; i < MAX_METEORS; i++)
{
meteors[i].isDestroyed = TRUE;
}
meteors_per_wave += NUM_METEORS_PER_WAVE;
meteors_left_per_wave = meteors_per_wave;
wave_counter++;
lc4_puts ((lc4uint*)"Current wave: ");
printnum(wave_counter);
lc4_puts ((lc4uint*)"\n");
missileCommand.missilesLeft = 8;
missileCommand.isDestroyed = FALSE;
}
}
/************************************************
* main -
* Initalize game state by reseting the game state
* Loops until the the user loses
***********************************************/
int main ()
{
//** Print to screen and initalize game state. */
lc4_puts ((lc4uint*)"Welcome to the Missile Command!\n");
ResetGame();
while(1)
{
//** Print to screen the instructions of the game */
lc4_puts ((lc4uint*)"Press r to shoot a missile\n");
lc4_puts ((lc4uint*)"Press w, a, s and d to move the cursor\n");
//** Main game loop calls the functions above to update the */
//** states of the missiles, meteors, cursors, etc and redraws the necessary images *
//** If the user loses, be sure to print out "---GAME OVER---" before resetting the game
while(1)
{
if(cities[0].isDestroyed && cities[1].isDestroyed)
break;
Redraw();
ExecuteInput(lc4_getc_timer(GETC_TIMER_DELAY));
CreateNewMeteors();
UpdateProjectiles();
PrepareForNextWave();
}
lc4_puts ((lc4uint*)"---GAME OVER---\n");
ResetGame();
}
return 0;
}