-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
348 lines (276 loc) · 10.8 KB
/
game.c
File metadata and controls
348 lines (276 loc) · 10.8 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
/*******************************************************************************************
*
* Smurf Invaders. Technical Demo as an explorative project of Raylib.
* Random game based on Space Invaders and alike.
*
* Copyright (c) 2020 Oscar Lostes Cazorla, under MIT license.
*
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Raylib by Ramon Santamaria (@raysan5), Copyright (c) 2013-2020
*
********************************************************************************************/
#include "game.h"
State gameCore()
{
// Initialization
//--------------------------------------------------------------------------------------
// Player related variables
Player player;
Mesh playerMesh;
Model playerModel;
BoundingBox playerBounds;
int score = 0;
int health = maxHealth;
// Bullet related variables
Bullet bulletPlayerPool[maxBullets];
Bullet bulletEnemyPool[maxBullets];
Mesh bulletMesh;
Model bulletModel;
BoundingBox bulletBounds;
// Enemy related variables
Enemy enemyPool[maxEnemies];
Mesh enemyMesh;
Model enemyModel;
BoundingBox enemyBounds;
// Timer to control enemy spawn rate
float timerEnemy = 0.0f;
// "Physics" timer
float timerPhysics = 0.0f;
// Enemy fire rate timer
float timerFire = 0.0f;
// HUD texts
char scoreDisp[TEXTSIZE];
char healthDisp[TEXTSIZE];
bool isPaused = false;
State gameState = GAME;
Camera camera;
// Camera configuration
// TODO Make this configurations constants
camera.position = (Vector3){0.0f, 5.0f, -10.0f};
camera.target = (Vector3){0.0f, 0.0f, 100.0f};
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
camera.fovy = 60.0f;
camera.type = CAMERA_PERSPECTIVE;
SetCameraMode(camera, CAMERA_CUSTOM); // Custom mode gives us full controll of the camera. No predefined behaviour
// Generate all meshes
playerMesh = GenMeshPoly(3, 2.0f);
bulletMesh = GenMeshSphere(0.5f, 10, 10);
enemyMesh = GenMeshCube(1.0f, 1.0f, 1.0f);
// Generate all models
playerModel = LoadModelFromMesh(playerMesh);
bulletModel = LoadModelFromMesh(bulletMesh);
enemyModel = LoadModelFromMesh(enemyMesh);
// Generate all reference BoundingBoxes
playerBounds = MeshBoundingBox(playerMesh);
bulletBounds = MeshBoundingBox(bulletMesh);
enemyBounds = MeshBoundingBox(enemyMesh);
// Initialization of entities
initEntity(&player, &playerModel, &playerBounds, BLUE, BLACK,
(Vector3){0.0f, yPos, 0.0f}, basePlayerSpeed, 1.0f, 0, true);
initEntityPool(bulletPlayerPool, maxBullets, &bulletModel, &bulletBounds, GREEN, BLACK,
(Vector3){0.0f, yPos, 2.5f}, baseBulletPlayerSpeed, 0.5f, baseEnemyScore);
initEntityPool(bulletEnemyPool, maxBullets, &bulletModel, &bulletBounds, DARKGREEN, BLACK,
(Vector3){0.0f, yPos, 2.5f}, baseBulletEnemySpeed, 0.5f, -100);
initEntityPool(enemyPool, maxEnemies, &enemyModel, &enemyBounds, BLACK, PURPLE,
(Vector3){0.0f, yPos, 8.0f}, baseEnemySpeed, 1.2f, -150);
// Initaize HUD displays
TextCopy(healthDisp, TextFormat("LIVES: %d", health));
TextCopy(scoreDisp, TextFormat("SCORE: %d", score));
SetExitKey(KEY_PAUSE);
//--------------------------------------------------------------------------------------
// Main game loop
while (gameState == GAME)
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_ESCAPE)){
if (isPaused) gameState = MAIN_M;
else isPaused = true;
}
if (IsKeyPressed(KEY_ENTER) && isPaused) isPaused = false;
if (!isPaused){
// Timers control. Increment timer with time elapsed since last frame.
timerEnemy += GetFrameTime();
timerPhysics += GetFrameTime();
timerFire += GetFrameTime();
// Get player input to move player entity.
// speed * frameTime = framerate-independant movement
player.position.x += player.speed * IsKeyDown(KEY_A) * GetFrameTime();
player.position.x -= player.speed * IsKeyDown(KEY_D) * GetFrameTime();
moveEntity(&player, Clamp(player.position.x, rightXLimit, leftXLimit), player.position.y, player.position.z);
//TraceLog(LOG_DEBUG, "Player X = %.2f", player.position.x);
// Bullet shooting
if (IsKeyReleased(KEY_SPACE))
{
int i = getEntityFromPool(bulletPlayerPool, maxBullets);
moveEntity(&bulletPlayerPool[i], player.position.x, yPos, 2.5f);
}
}
if (timerPhysics >= physicsPeriod)
{
int minI = 0;
float minDist = 999.0f;
// Iterates through bullet pool to apply necessary changes
for (int i = 0; i < maxBullets; i++)
{
int minJ = 0;
// If active, move
if (bulletPlayerPool[i].active){
moveEntity(&bulletPlayerPool[i], bulletPlayerPool[i].position.x, bulletPlayerPool[i].position.y,
bulletPlayerPool[i].position.z + bulletPlayerPool[i].speed * timerPhysics);
if (bulletPlayerPool[i].position.z >= frontZLimit) bulletPlayerPool[i].active = false; // Deactivate bullet if outside designed limits
for (int j = 0; j < maxEnemies; j++)
{
if (enemyPool[j].active)
{
float dist = Vector3LengthSqr(Vector3Subtract(enemyPool[j].position, bulletPlayerPool[i].position));
if (dist < minDist)
{
minJ = j;
minDist = dist;
}
}
}
/// Bullet collision detection. Only if enemy is active (we don't want to collide with invisible enemies...)
if (CheckCollisionBoxes(enemyPool[minJ].bounds, bulletPlayerPool[i].bounds))
{
// If bullet collides with enemy, mutual deactivation
bulletPlayerPool[i].active = false;
enemyPool[minJ].active = false;
score += bulletPlayerPool[i].score;
TextCopy(scoreDisp, TextFormat("SCORE: %d", score));
}
}
}
// Iterates through enemy pool to apply necessary changes
for (int i = 0; i < maxEnemies; i++)
{
// If active, move
if (enemyPool[i].active){
moveEntity(&enemyPool[i], enemyPool[i].position.x, enemyPool[i].position.y,
enemyPool[i].position.z + enemyPool[i].speed * timerPhysics);
if (enemyPool[i].position.z <= backZLimit) enemyPool[i].active = false; // Deactivate enemy if outside designed limits
if (player.active){
float dist = Vector3LengthSqr(Vector3Subtract(enemyPool[i].position, player.position));
if (dist < minDist)
{
minI = i;
minDist = dist;
}
}
}
/// Bullet collision detection. Only if enemy is active (we don't want to collide with invisible enemies...)
if (enemyPool[minI].active && CheckCollisionBoxes(enemyPool[minI].bounds, player.bounds))
{
// If bullet collides with enemy, mutual deactivation
enemyPool[minI].active = false;
//player.active = false;
score += enemyPool[minI].score;
health--;
TextCopy(scoreDisp, TextFormat("SCORE: %d", score));
TextCopy(healthDisp, TextFormat("LIVES: %d", health));
}
}
// Iterates through bullet pool to apply necessary changes
for (int i = 0; i < maxBullets; i++)
{
// If active, move
if (bulletEnemyPool[i].active){
moveEntity(&bulletEnemyPool[i], bulletEnemyPool[i].position.x, bulletEnemyPool[i].position.y,
bulletEnemyPool[i].position.z + bulletEnemyPool[i].speed * timerPhysics);
if (bulletEnemyPool[i].position.z <= backZLimit) bulletEnemyPool[i].active = false; // Deactivate bullet if outside designed limits
if (player.active){
float dist = Vector3LengthSqr(Vector3Subtract(bulletEnemyPool[i].position, player.position));
if (dist < minDist)
{
minI = i;
minDist = dist;
}
}
}
}
// Bullet collision detection. Only if enemy is active (we don't want to collide with invisible enemies...)
if (bulletEnemyPool[minI].active && CheckCollisionBoxes(bulletEnemyPool[minI].bounds, player.bounds))
{
// If bullet collides with enemy, mutual deactivation
bulletEnemyPool[minI].active = false;
//player.active = false;
score += bulletEnemyPool[minI].score;
health--;
TextCopy(scoreDisp, TextFormat("SCORE: %d", score));
TextCopy(healthDisp, TextFormat("LIVES: %d", health));
}
timerPhysics = 0.0f; // Restart timer
}
if (timerEnemy >= enemySpawnPeriod) // Spawn enemy if enough time passed already
{
// Spawn Enemy
int i = getEntityFromPool(enemyPool, maxEnemies);
moveEntity(&enemyPool[i], GetRandomValue(rightXLimit, leftXLimit), yPos, enemySpawnZ);
enemyPool[i].speed = baseEnemySpeed + GetRandomValue(-speedVariability, +speedVariability);
timerEnemy = 0.0f; // Restart timer
}
if (timerFire >= firePeriod){
// Spawn enemy bullets
int i;
for (i = 0; i < maxEnemies; i++)
{
if (enemyPool[i].active){
bool shooting = GetRandomValue(0, 100) <= fireProbability;
if (shooting){
int j = getEntityFromPool(bulletEnemyPool, maxBullets);
moveEntity(&bulletEnemyPool[j], enemyPool[i].position.x, yPos, enemyPool[i].position.z - 1.0f);
bulletEnemyPool[j].speed = baseBulletEnemySpeed + GetRandomValue(0, 2 * speedVariability);
}
}
}
timerFire = 0.0f; // Restart timer
}
if (health <= 0) gameState = GO_M;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
{
ClearBackground(RAYWHITE); // Clear background
BeginMode3D(camera);
{
DrawGrid(200, 1.0f); // Static scenario
// Draw player + border + shadow
drawEntity(&player);
// Draw all active bullets
drawEntityPool(bulletPlayerPool, maxBullets);
drawEntityPool(bulletEnemyPool, maxBullets);
// Draw all active enemies
drawEntityPool(enemyPool, maxEnemies);
}
EndMode3D();
// HUD drawing
// TODO proper HUD
DrawText(title, 10, 40, 20, DARKGREEN);
DrawText(healthDisp, screenWidth - MeasureText(healthDisp, 20) - 10, 10, 20, DARKGREEN);
DrawText(scoreDisp, screenWidth - MeasureText(scoreDisp, 20) - 10, 30, 20, DARKGREEN);
DrawFPS(10, 10);
if (isPaused){
DrawRectangle(0,0,screenWidth,screenHeight,(Color) {0,0,0,100});
DrawText("PAUSE", screenWidth / 2 - MeasureText("PAUSE", 40) / 2, screenHeight / 2 - 70, 40, GREEN);
DrawText("Press ENTER to resume", screenWidth / 2 - MeasureText("Press ENTER to resume", 20) / 2, screenHeight / 2, 20, YELLOW);
DrawText("Press ESCAPE to exit", screenWidth / 2 - MeasureText("Press ESCAPE to exit", 20) / 2, screenHeight / 2 + 40, 20, YELLOW);
}
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// Unload model data
UnloadModel(playerModel);
UnloadModel(bulletModel);
UnloadModel(enemyModel);
// Unload mesh data
UnloadMesh(playerMesh);
UnloadMesh(bulletMesh);
UnloadMesh(enemyMesh);
//--------------------------------------------------------------------------------------
return gameState;
}