-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmissile_commander.c
532 lines (440 loc) · 19.6 KB
/
missile_commander.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
/*******************************************************************************************
*
* raylib - classic game: missile commander
*
* Sample game developed by Marc Palau and Ramon Santamaria
*
* This game has been created using raylib v1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Some Defines
//----------------------------------------------------------------------------------
#define MAX_MISSILES 100
#define MAX_INTERCEPTORS 30
#define MAX_EXPLOSIONS 100
#define LAUNCHERS_AMOUNT 3 // Not a variable, should not be changed
#define BUILDINGS_AMOUNT 6 // Not a variable, should not be changed
#define LAUNCHER_SIZE 80
#define BUILDING_SIZE 60
#define EXPLOSION_RADIUS 40
#define MISSILE_SPEED 1
#define MISSILE_LAUNCH_FRAMES 80
#define INTERCEPTOR_SPEED 10
#define EXPLOSION_INCREASE_TIME 90 // In frames
#define EXPLOSION_TOTAL_TIME 210 // In frames
#define EXPLOSION_COLOR (Color){ 125, 125, 125, 125 }
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct Missile {
Vector2 origin;
Vector2 position;
Vector2 objective;
Vector2 speed;
bool active;
} Missile;
typedef struct Interceptor {
Vector2 origin;
Vector2 position;
Vector2 objective;
Vector2 speed;
bool active;
} Interceptor;
typedef struct Explosion {
Vector2 position;
float radiusMultiplier;
int frame;
bool active;
} Explosion;
typedef struct Launcher {
Vector2 position;
bool active;
} Launcher;
typedef struct Building {
Vector2 position;
bool active;
} Building;
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
static int screenWidth = 800;
static int screenHeight = 450;
static int framesCounter = 0;
static bool gameOver = false;
static bool pause = false;
static int score = 0;
static Missile missile[MAX_MISSILES] = { 0 };
static Interceptor interceptor[MAX_INTERCEPTORS] = { 0 };
static Explosion explosion[MAX_EXPLOSIONS] = { 0 };
static Launcher launcher[LAUNCHERS_AMOUNT] = { 0 };
static Building building[BUILDINGS_AMOUNT] = { 0 };
static int explosionIndex = 0;
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
//------------------------------------------------------------------------------------
static void InitGame(void); // Initialize game
static void UpdateGame(void); // Update game (one frame)
static void DrawGame(void); // Draw game (one frame)
static void UnloadGame(void); // Unload game
static void UpdateDrawFrame(void); // Update and Draw (one frame)
// Additional module functions
static void UpdateOutgoingFire();
static void UpdateIncomingFire();
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
InitWindow(screenWidth, screenHeight, "classic game: missile commander");
InitGame();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
#else
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update and Draw
//----------------------------------------------------------------------------------
UpdateDrawFrame();
//----------------------------------------------------------------------------------
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadGame(); // Unload loaded data (textures, sounds, models...)
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//--------------------------------------------------------------------------------------
// Game Module Functions Definition
//--------------------------------------------------------------------------------------
// Initialize game variables
void InitGame(void)
{
// Initialize missiles
for (int i = 0; i < MAX_MISSILES; i++)
{
missile[i].origin = (Vector2){ 0, 0 };
missile[i].speed = (Vector2){ 0, 0 };
missile[i].position = (Vector2){ 0, 0 };
missile[i].active = false;
}
// Initialize interceptors
for (int i = 0; i < MAX_INTERCEPTORS; i++)
{
interceptor[i].origin = (Vector2){ 0, 0 };
interceptor[i].speed = (Vector2){ 0, 0 };
interceptor[i].position = (Vector2){ 0, 0 };
interceptor[i].active = false;
}
// Initialize explosions
for (int i = 0; i < MAX_EXPLOSIONS; i++)
{
explosion[i].position = (Vector2){ 0, 0 };
explosion[i].frame = 0;
explosion[i].active = false;
}
// Initialize buildings and launchers
int sparcing = screenWidth/(LAUNCHERS_AMOUNT + BUILDINGS_AMOUNT + 1);
// Buildings and launchers placing
launcher[0].position = (Vector2){ 1*sparcing, screenHeight - LAUNCHER_SIZE/2 };
building[0].position = (Vector2){ 2*sparcing, screenHeight - BUILDING_SIZE/2 };
building[1].position = (Vector2){ 3*sparcing, screenHeight - BUILDING_SIZE/2 };
building[2].position = (Vector2){ 4*sparcing, screenHeight - BUILDING_SIZE/2 };
launcher[1].position = (Vector2){ 5*sparcing, screenHeight - LAUNCHER_SIZE/2 };
building[3].position = (Vector2){ 6*sparcing, screenHeight - BUILDING_SIZE/2 };
building[4].position = (Vector2){ 7*sparcing, screenHeight - BUILDING_SIZE/2 };
building[5].position = (Vector2){ 8*sparcing, screenHeight - BUILDING_SIZE/2 };
launcher[2].position = (Vector2){ 9*sparcing, screenHeight - LAUNCHER_SIZE/2 };
// Buildings and launchers activation
for (int i = 0; i < LAUNCHERS_AMOUNT; i++) launcher[i].active = true;
for (int i = 0; i < BUILDINGS_AMOUNT; i++) building[i].active = true;
// Initialize game variables
score = 0;
}
// Update game (one frame)
void UpdateGame(void)
{
if (!gameOver)
{
if (IsKeyPressed('P')) pause = !pause;
if (!pause)
{
framesCounter++;
static
float distance;
// Interceptors update
for (int i = 0; i < MAX_INTERCEPTORS; i++)
{
if (interceptor[i].active)
{
// Update position
interceptor[i].position.x += interceptor[i].speed.x;
interceptor[i].position.y += interceptor[i].speed.y;
// Distance to objective
distance = sqrt( pow(interceptor[i].position.x - interceptor[i].objective.x, 2) +
pow(interceptor[i].position.y - interceptor[i].objective.y, 2));
if (distance < INTERCEPTOR_SPEED)
{
// Interceptor dissapears
interceptor[i].active = false;
// Explosion
explosion[explosionIndex].position = interceptor[i].position;
explosion[explosionIndex].active = true;
explosion[explosionIndex].frame = 0;
explosionIndex++;
if (explosionIndex == MAX_EXPLOSIONS) explosionIndex = 0;
break;
}
}
}
// Missiles update
for (int i = 0; i < MAX_MISSILES; i++)
{
if (missile[i].active)
{
// Update position
missile[i].position.x += missile[i].speed.x;
missile[i].position.y += missile[i].speed.y;
// Collision and missile out of bounds
if (missile[i].position.y > screenHeight) missile[i].active = false;
else
{
// CHeck collision with launchers
for (int j = 0; j < LAUNCHERS_AMOUNT; j++)
{
if (launcher[j].active)
{
if (CheckCollisionPointRec(missile[i].position, (Rectangle){ launcher[j].position.x - LAUNCHER_SIZE/2, launcher[j].position.y - LAUNCHER_SIZE/2,
LAUNCHER_SIZE, LAUNCHER_SIZE }))
{
// Missile dissapears
missile[i].active = false;
// Explosion and destroy building
launcher[j].active = false;
explosion[explosionIndex].position = missile[i].position;
explosion[explosionIndex].active = true;
explosion[explosionIndex].frame = 0;
explosionIndex++;
if (explosionIndex == MAX_EXPLOSIONS) explosionIndex = 0;
break;
}
}
}
// CHeck collision with buildings
for (int j = 0; j < BUILDINGS_AMOUNT; j++)
{
if (building[j].active)
{
if (CheckCollisionPointRec(missile[i].position, (Rectangle){ building[j].position.x - BUILDING_SIZE/2, building[j].position.y - BUILDING_SIZE/2,
BUILDING_SIZE, BUILDING_SIZE }))
{
// Missile dissapears
missile[i].active = false;
// Explosion and destroy building
building[j].active = false;
explosion[explosionIndex].position = missile[i].position;
explosion[explosionIndex].active = true;
explosion[explosionIndex].frame = 0;
explosionIndex++;
if (explosionIndex == MAX_EXPLOSIONS) explosionIndex = 0;
break;
}
}
}
// CHeck collision with explosions
for (int j = 0; j < MAX_EXPLOSIONS; j++)
{
if (explosion[j].active)
{
if (CheckCollisionPointCircle(missile[i].position, explosion[j].position, EXPLOSION_RADIUS*explosion[j].radiusMultiplier))
{
// Missile dissapears and we earn 100 points
missile[i].active = false;
score += 100;
explosion[explosionIndex].position = missile[i].position;
explosion[explosionIndex].active = true;
explosion[explosionIndex].frame = 0;
explosionIndex++;
if (explosionIndex == MAX_EXPLOSIONS) explosionIndex = 0;
break;
}
}
}
}
}
}
// Explosions update
for (int i = 0; i < MAX_EXPLOSIONS; i++)
{
if (explosion[i].active)
{
explosion[i].frame++;
if (explosion[i].frame <= EXPLOSION_INCREASE_TIME) explosion[i].radiusMultiplier = explosion[i].frame/(float)EXPLOSION_INCREASE_TIME;
else if (explosion[i].frame <= EXPLOSION_TOTAL_TIME) explosion[i].radiusMultiplier = 1 - (explosion[i].frame - (float)EXPLOSION_INCREASE_TIME)/(float)EXPLOSION_TOTAL_TIME;
else
{
explosion[i].frame = 0;
explosion[i].active = false;
}
}
}
// Fire logic
UpdateOutgoingFire();
UpdateIncomingFire();
// Game over logic
int checker = 0;
for (int i = 0; i < LAUNCHERS_AMOUNT; i++)
{
if (!launcher[i].active) checker++;
if (checker == LAUNCHERS_AMOUNT) gameOver = true;
}
checker = 0;
for (int i = 0; i < BUILDINGS_AMOUNT; i++)
{
if (!building[i].active) checker++;
if (checker == BUILDINGS_AMOUNT) gameOver = true;
}
}
}
else
{
if (IsKeyPressed(KEY_ENTER))
{
InitGame();
gameOver = false;
}
}
}
// Draw game (one frame)
void DrawGame(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
if (!gameOver)
{
// Draw missiles
for (int i = 0; i < MAX_MISSILES; i++)
{
if (missile[i].active)
{
DrawLine(missile[i].origin.x, missile[i].origin.y, missile[i].position.x, missile[i].position.y, RED);
if (framesCounter % 16 < 8) DrawCircle(missile[i].position.x, missile[i].position.y, 3, YELLOW);
}
}
// Draw interceptors
for (int i = 0; i < MAX_INTERCEPTORS; i++)
{
if (interceptor[i].active)
{
DrawLine(interceptor[i].origin.x, interceptor[i].origin.y, interceptor[i].position.x, interceptor[i].position.y, GREEN);
if (framesCounter % 16 < 8) DrawCircle(interceptor[i].position.x, interceptor[i].position.y, 3, BLUE);
}
}
// Draw explosions
for (int i = 0; i < MAX_EXPLOSIONS; i++)
{
if (explosion[i].active) DrawCircle(explosion[i].position.x, explosion[i].position.y, EXPLOSION_RADIUS*explosion[i].radiusMultiplier, EXPLOSION_COLOR);
}
// Draw buildings and launchers
for (int i = 0; i < LAUNCHERS_AMOUNT; i++)
{
if (launcher[i].active) DrawRectangle(launcher[i].position.x - LAUNCHER_SIZE/2, launcher[i].position.y - LAUNCHER_SIZE/2, LAUNCHER_SIZE, LAUNCHER_SIZE, GRAY);
}
for (int i = 0; i < BUILDINGS_AMOUNT; i++)
{
if (building[i].active) DrawRectangle(building[i].position.x - BUILDING_SIZE/2, building[i].position.y - BUILDING_SIZE/2, BUILDING_SIZE, BUILDING_SIZE, LIGHTGRAY);
}
// Draw score
DrawText(TextFormat("SCORE %4i", score), 20, 20, 40, LIGHTGRAY);
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
}
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
EndDrawing();
}
// Unload game variables
void UnloadGame(void)
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
}
// Update and Draw (one frame)
void UpdateDrawFrame(void)
{
UpdateGame();
DrawGame();
}
//--------------------------------------------------------------------------------------
// Additional module functions
//--------------------------------------------------------------------------------------
static void UpdateOutgoingFire()
{
static int interceptorNumber = 0;
int launcherShooting = 0;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) launcherShooting = 1;
if (IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON)) launcherShooting = 2;
if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) launcherShooting = 3;
if (launcherShooting > 0 && launcher[launcherShooting - 1].active)
{
float module;
float sideX;
float sideY;
// Activate the interceptor
interceptor[interceptorNumber].active = true;
// Assign start position
interceptor[interceptorNumber].origin = launcher[launcherShooting - 1].position;
interceptor[interceptorNumber].position = interceptor[interceptorNumber].origin;
interceptor[interceptorNumber].objective = GetMousePosition();
// Calculate speed
module = sqrt( pow(interceptor[interceptorNumber].objective.x - interceptor[interceptorNumber].origin.x, 2) +
pow(interceptor[interceptorNumber].objective.y - interceptor[interceptorNumber].origin.y, 2));
sideX = (interceptor[interceptorNumber].objective.x - interceptor[interceptorNumber].origin.x)*INTERCEPTOR_SPEED/module;
sideY = (interceptor[interceptorNumber].objective.y - interceptor[interceptorNumber].origin.y)*INTERCEPTOR_SPEED/module;
interceptor[interceptorNumber].speed = (Vector2){ sideX, sideY };
// Update
interceptorNumber++;
if (interceptorNumber == MAX_INTERCEPTORS) interceptorNumber = 0;
}
}
static void UpdateIncomingFire()
{
static int missileIndex = 0;
// Launch missile
if (framesCounter%MISSILE_LAUNCH_FRAMES == 0)
{
float module;
float sideX;
float sideY;
// Activate the missile
missile[missileIndex].active = true;
// Assign start position
missile[missileIndex].origin = (Vector2){ GetRandomValue(20, screenWidth - 20), -10 };
missile[missileIndex].position = missile[missileIndex].origin;
missile[missileIndex].objective = (Vector2){ GetRandomValue(20, screenWidth - 20), screenHeight + 10 };
// Calculate speed
module = sqrt( pow(missile[missileIndex].objective.x - missile[missileIndex].origin.x, 2) +
pow(missile[missileIndex].objective.y - missile[missileIndex].origin.y, 2));
sideX = (missile[missileIndex].objective.x - missile[missileIndex].origin.x)*MISSILE_SPEED/module;
sideY = (missile[missileIndex].objective.y - missile[missileIndex].origin.y)*MISSILE_SPEED/module;
missile[missileIndex].speed = (Vector2){ sideX, sideY };
// Update
missileIndex++;
if (missileIndex == MAX_MISSILES) missileIndex = 0;
}
}