-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathplatformer.c
702 lines (593 loc) · 19.8 KB
/
platformer.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
/*******************************************************************************************
*
* raylib - classic game: platformer
*
* Sample game developed by Agnis "NeZvers" Aldiòð (@nezvers)
*
* This game has been created using raylib v3.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2020 Agnis "NeZvers" Aldiòð (@nezvers) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Some Defines
//----------------------------------------------------------------------------------
// Tile collision types
#define EMPTY -1
#define BLOCK 0 // Start from zero, slopes can be added
// Defined map size
#define TILE_MAP_WIDTH 20
#define TILE_MAP_HEIGHT 12
#define MAX_COINS 10
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// Object storing inputs for Entity
typedef struct {
float right;
float left;
float up;
float down;
bool jump;
} Input;
// Physics body moving around
typedef struct {
int width;
int height;
Vector2 position;
float direction;
float maxSpd;
float acc;
float dcc;
float gravity;
float jumpImpulse;
float jumpRelease;
Vector2 velocity;
// Carry stored subpixel values
float hsp;
float vsp;
bool isGrounded;
bool isJumping;
// Flags for detecting collision
bool hitOnFloor;
bool hitOnCeiling;
bool hitOnWall;
Input *control;
} Entity;
// Coin object
typedef struct {
Vector2 position;
bool visible;
} Coin;
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
const int TILE_SIZE = 16;
const int TILE_ROUND = 15; // Used in bitwise operation | TILE_SIZE - 1
float screenScale;
int screenWidth;
static int screenHeight;
static float delta;
static bool win = false;
static int score = 0;
static int tiles [TILE_MAP_WIDTH*TILE_MAP_HEIGHT];
static Entity player = { 0 };
static Input input = {false, false, false, false, false};
static Camera2D camera = {0};
// Create coin instances
static Coin coins[MAX_COINS] = {
{(Vector2){1*16+6,7*16+6}, true},
{(Vector2){3*16+6,5*16+6}, true},
{(Vector2){4*16+6,5*16+6}, true},
{(Vector2){5*16+6,5*16+6}, true},
{(Vector2){8*16+6,3*16+6}, true},
{(Vector2){9*16+6,3*16+6}, true},
{(Vector2){10*16+6,3*16+6}, true},
{(Vector2){13*16+6,4*16+6}, true},
{(Vector2){14*16+6,4*16+6}, true},
{(Vector2){15*16+6,4*16+6}, true},
};
//------------------------------------------------------------------------------------
// 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)
//------------------------------------------------------------------------------------
// Movement Functions Declaration (local)
//------------------------------------------------------------------------------------
static void EntityMoveUpdate(Entity *intance);
static void GetDirection(Entity *instance);
static void GroundCheck(Entity *instance);
static void MoveCalc(Entity *instance);
static void GravityCalc(Entity *instance);
static void CollisionCheck(Entity *instance);
static void CollisionHorizontalBlocks(Entity *instance);
static void CollisionVerticalBlocks(Entity *instance);
//------------------------------------------------------------------------------------
// Tile Functions Declaration (local)
//------------------------------------------------------------------------------------
static int MapGetTileWorld(int x, int y);
static int TileHeight(int x, int y, int tile);
static void MapInit(void);
static void MapDraw(void);
static void PlayerInit(void);
static void InputUpdate(void);
static void PlayerUpdate(void);
static void PlayerDraw(void);
static void CoinInit(void);
static void CoinUpdate(void);
static void CoinDraw(void);
//------------------------------------------------------------------------------------
// Utility Functions Declaration (local)
//------------------------------------------------------------------------------------
int ttc_sign(float x);
float ttc_abs(float x);
float ttc_clamp(float value, float min, float max);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
screenScale = 2.0;
screenWidth = TILE_SIZE*TILE_MAP_WIDTH*(int)screenScale;
screenHeight = TILE_SIZE*TILE_MAP_HEIGHT*(int)screenScale;
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "classic game: platformer");
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;
}
//------------------------------------------------------------------------------------
// Module Functions Definitions (local)
//------------------------------------------------------------------------------------
// Initialize game variables
void InitGame(void)
{
win = false;
score = 0;
camera.offset = (Vector2){0.0, 0.0};
camera.target = (Vector2){0.0, 0.0};
camera.rotation = 0.0f;
camera.zoom = screenScale;
MapInit();
PlayerInit();
CoinInit();
}
// Update game (one frame)
void UpdateGame(void)
{
// Get time since last frame
delta = GetFrameTime();
PlayerUpdate();
CoinUpdate();
// If all coins are collected
if (win)
{
if (IsKeyPressed(KEY_ENTER))
{
InitGame();
}
}
}
// Draw game (every frame)
void DrawGame(void)
{
BeginDrawing();
BeginMode2D(camera);
ClearBackground(RAYWHITE);
// Draw game
MapDraw();
CoinDraw();
PlayerDraw();
EndMode2D();
DrawText(TextFormat("SCORE: %i", score), GetScreenWidth()/2 - MeasureText(TextFormat("SCORE: %i", score), 40)/2, 50, 40, BLACK);
if (win) 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 required assets here
}
void MapInit(void)
{
// Set tiles as borders
for (int y = 0; y < TILE_MAP_HEIGHT; y++)
{
for (int x = 0; x < TILE_MAP_WIDTH; x++)
{
// Solid tiles
if (y == 0 || x == 0 || y == TILE_MAP_HEIGHT-1 || x == TILE_MAP_WIDTH-1)
{
tiles[x+y*TILE_MAP_WIDTH] = BLOCK;
}
else // Empty tiles
{
tiles[x+y*TILE_MAP_WIDTH] = EMPTY;
}
}
}
// Manual cell population for platforms
tiles[3 + 8*TILE_MAP_WIDTH] = BLOCK;
tiles[4 + 8*TILE_MAP_WIDTH] = BLOCK;
tiles[5 + 8*TILE_MAP_WIDTH] = BLOCK;
tiles[8 + 6*TILE_MAP_WIDTH] = BLOCK;
tiles[9 + 6*TILE_MAP_WIDTH] = BLOCK;
tiles[10 + 6*TILE_MAP_WIDTH] = BLOCK;
tiles[13 + 7*TILE_MAP_WIDTH] = BLOCK;
tiles[14 + 7*TILE_MAP_WIDTH] = BLOCK;
tiles[15 + 7*TILE_MAP_WIDTH] = BLOCK;
tiles[1 + 10*TILE_MAP_WIDTH] = BLOCK;
}
void MapDraw(void)
{
// Parse through tile map and draw rectangles to visualize it
for (int y = 0; y < TILE_MAP_HEIGHT; y++)
{
for (int x = 0; x < TILE_MAP_WIDTH; x++)
{
// Draw tiles
if ( tiles[x+y*TILE_MAP_WIDTH] > EMPTY)
{
DrawRectangle(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE, GRAY);
}
}
}
}
// Function to get tile index using world coordinates
int MapGetTileWorld(int x, int y)
{
if (x < 0 || y < 0) return EMPTY;
// Returns tile ID using world position
x /= TILE_SIZE;
y /= TILE_SIZE;
if (x > TILE_MAP_WIDTH || y > TILE_MAP_HEIGHT) return EMPTY;
return tiles[x+y*TILE_MAP_WIDTH];
}
// Returns one pixel above the tile in world coordinates
int TileHeight(int x, int y, int tile)
{
// Returns one pixel above solid. Extendable for slopes.
switch(tile)
{
case EMPTY: break;
case BLOCK: y = (y & ~TILE_ROUND) -1; break;
}
return y;
}
// Update player controlled Input instance
void InputUpdate(void)
{
input.right = (float)(IsKeyDown('D') || IsKeyDown(KEY_RIGHT));
input.left = (float)(IsKeyDown('A') || IsKeyDown(KEY_LEFT));
input.up = (float)(IsKeyDown('W') || IsKeyDown(KEY_UP));
input.down = (float)(IsKeyDown('S') || IsKeyDown(KEY_DOWN));
// For jumping button needs to be toggled - allows pre-jump buffered (if held, jumps as soon as lands)
if (IsKeyPressed(KEY_SPACE)) input.jump = true;
else if (IsKeyReleased(KEY_SPACE)) input.jump = false;
}
// Init player controlled instance
void PlayerInit(void)
{
player.position.x = (float)(TILE_SIZE*TILE_MAP_WIDTH)*0.5;
player.position.y = TILE_MAP_HEIGHT*TILE_SIZE - 16.0 -1;
player.direction = 1.0;
player.maxSpd = 1.5625f*60;
player.acc = 0.118164f*60*60;
player.dcc = 0.113281f*60*60;
player.gravity = 0.363281f*60*60;
player.jumpImpulse = -6.5625f*60;
player.jumpRelease = player.jumpImpulse*0.2f;
player.velocity = (Vector2){ 0.0, 0.0 };
player.hsp = 0;
player.vsp = 0;
player.width = 8;
player.height = 16;
player.isGrounded = false;
player.isJumping = false;
// Assign Input instance used by player
player.control = &input;
}
// Draw player representing rectangle so the position is at bottom middle
void PlayerDraw(void)
{
DrawRectangle(player.position.x - player.width*0.5, player.position.y-player.height +1, player.width, player.height, RED);
}
// Player's instance update
void PlayerUpdate(void)
{
InputUpdate();
EntityMoveUpdate(&player);
}
// Reset coin visibility
void CoinInit(void)
{
for (int i=0; i<MAX_COINS; i++) coins[i].visible = true;
}
// Draw each coin
void CoinDraw(void)
{
for (int i=0; i<MAX_COINS; i++)
{
if (coins[i].visible) DrawRectangle((int)coins[i].position.x, (int)coins[i].position.y, 4.0, 4.0, GOLD);
}
}
// Collision check each coin
void CoinUpdate(void)
{
Rectangle playerRect = (Rectangle){ player.position.x - player.width*0.5, player.position.y-player.height +1, player.width, player.height };
for (int i = 0; i < MAX_COINS; i++)
{
if (coins[i].visible)
{
Rectangle coinRect = (Rectangle){ coins[i].position.x, coins[i].position.y, 4.0, 4.0 };
if (CheckCollisionRecs(playerRect, coinRect))
{
coins[i].visible = false;
score += 1;
}
}
}
win = score == MAX_COINS;
}
//------------------------------------------------
// Physics functions
//------------------------------------------------
// Main Entity movement calculation
void EntityMoveUpdate(Entity *instance)
{
GroundCheck(instance);
GetDirection(instance);
MoveCalc(instance);
GravityCalc(instance);
CollisionCheck(instance);
// Horizontal velocity together including last frame sub-pixel value
float xVel = instance->velocity.x*delta + instance->hsp;
// Horizontal velocity in pixel values
int xsp = (int)ttc_abs(xVel)*ttc_sign(xVel);
// Save horizontal velocity sub-pixel value for next frame
instance->hsp = instance->velocity.x*delta - xsp;
// Vertical velocity together including last frame sub-pixel value
float yVel = instance->velocity.y*delta + instance->vsp;
// Vertical velocity in pixel values
int ysp = (int)ttc_abs(yVel)*ttc_sign(yVel);
// Save Vertical velocity sub-pixel value for next frame
instance->vsp = instance->velocity.y*delta - ysp;
// Add pixel value velocity to the position
instance->position.x += xsp;
instance->position.y += ysp;
// Prototyping Safety net - keep in view
instance->position.x = ttc_clamp(instance->position.x, 0.0, TILE_MAP_WIDTH*(float)TILE_SIZE);
instance->position.y = ttc_clamp(instance->position.y, 0.0, TILE_MAP_HEIGHT*(float)TILE_SIZE);
}
// Read Input for horizontal movement direction
void GetDirection(Entity *instance)
{
instance->direction = (instance->control->right - instance->control->left);
}
// Check pixel bellow to determine if Entity is grounded
void GroundCheck(Entity *instance)
{
int x = (int)instance->position.x;
int y = (int)instance->position.y + 1;
instance->isGrounded = false;
// Center point check
int c = MapGetTileWorld(x , y);
if (c != EMPTY)
{
int h = TileHeight(x, y, c);
instance->isGrounded = (y >= h);
}
if (!instance->isGrounded)
{
// Left bottom corner check
int xl = (x - instance->width / 2);
int l = MapGetTileWorld(xl , y);
if (l != EMPTY)
{
int h = TileHeight(xl, y, l);
instance->isGrounded = (y >= h);
}
if (!instance->isGrounded)
{
// Right bottom corner check
int xr = (x + instance->width / 2 - 1);
int r = MapGetTileWorld(xr , y);
if (r != EMPTY)
{
int h = TileHeight(xr, y, r);
instance->isGrounded = (y >= h);
}
}
}
}
// Simplified horizontal acceleration / deacceleration logic
void MoveCalc(Entity *instance)
{
// Check if direction value is above dead zone - direction is held
float deadZone = 0.0;
if (ttc_abs(instance->direction) > deadZone)
{
instance->velocity.x += instance->direction*instance->acc*delta;
instance->velocity.x = ttc_clamp(instance->velocity.x, -instance->maxSpd, instance->maxSpd);
}
else
{
// No direction means deacceleration
float xsp = instance->velocity.x;
if (ttc_abs(0 - xsp) < instance->dcc*delta) instance->velocity.x = 0;
else if (xsp > 0) instance->velocity.x -= instance->dcc*delta;
else instance->velocity.x += instance->dcc*delta;
}
}
// Set values when jump is activated
void Jump(Entity *instance)
{
instance->velocity.y = instance->jumpImpulse;
instance->isJumping = true;
instance->isGrounded = false;
}
// Gravity calculation and Jump detection
void GravityCalc(Entity *instance)
{
if (instance->isGrounded)
{
if (instance->isJumping)
{
instance->isJumping = false;
instance->control->jump = false; // Cancel input button
}
else if (!instance->isJumping && instance->control->jump)
{
Jump(instance);
}
}
else
{
if (instance->isJumping)
{
if (!instance->control->jump)
{
instance->isJumping = false;
if (instance->velocity.y < instance->jumpRelease)
{
instance->velocity.y = instance->jumpRelease;
}
}
}
}
// Add gravity
instance->velocity.y += instance->gravity*delta;
// Limit falling to negative jump value
if (instance->velocity.y > -instance->jumpImpulse)
{
instance->velocity.y = -instance->jumpImpulse;
}
}
// Main collision check function
void CollisionCheck(Entity *instance)
{
CollisionHorizontalBlocks(instance);
CollisionVerticalBlocks(instance);
}
// Detect and solve horizontal collision with block tiles
void CollisionHorizontalBlocks(Entity *instance)
{
// Get horizontal speed in pixels
float xVel = instance->velocity.x*delta + instance->hsp;
int xsp = (int)ttc_abs(xVel)*ttc_sign(xVel);
instance->hitOnWall = false;
// Get bounding box side offset
int side;
if (xsp > 0) side = instance->width / 2 - 1;
else if (xsp < 0) side = -instance->width / 2;
else return;
int x = (int)instance->position.x;
int y = (int)instance->position.y;
int mid = -instance->height / 2;
int top = -instance->height + 1;
// 3 point check
int b = MapGetTileWorld(x + side + xsp , y) > EMPTY;
int m = MapGetTileWorld(x + side + xsp , y + mid) > EMPTY;
int t = MapGetTileWorld(x + side + xsp , y + top) > EMPTY;
// If implementing slopes it's better to disable b and m, if (x,y) is in the slope tile
if (b || m || t)
{
if (xsp > 0) x = ((x + side + xsp) & ~TILE_ROUND) - 1 - side;
else x = ((x + side + xsp) & ~TILE_ROUND) + TILE_SIZE - side;
instance->position.x = (float)x;
instance->velocity.x = 0.0;
instance->hsp = 0.0;
instance->hitOnWall = true;
}
}
// Detect and solve vertical collision with block tiles
void CollisionVerticalBlocks(Entity *instance)
{
// Get vertical speed in pixels
float yVel = instance->velocity.y*delta + instance->vsp;
int ysp = (int)ttc_abs(yVel)*ttc_sign(yVel);
instance->hitOnCeiling = false;
instance->hitOnFloor = false;
// Get bounding box side offset
int side = 0;
if (ysp > 0) side = 0;
else if (ysp < 0) side = -instance->height + 1;
else return;
int x = (int)instance->position.x;
int y = (int)instance->position.y;
int xl = -instance->width/2;
int xr = instance->width/2 - 1;
int c = MapGetTileWorld(x , y + side + ysp) > EMPTY;
int l = MapGetTileWorld(x + xl , y + side + ysp) > EMPTY;
int r = MapGetTileWorld(x + xr , y + side + ysp) > EMPTY;
if (c || l || r)
{
if (ysp > 0)
{
y = ((y + side + ysp) & ~TILE_ROUND) - 1 - side;
instance->hitOnFloor = true;
}
else
{
y = ((y + side + ysp) & ~TILE_ROUND) + TILE_SIZE - side;
instance->hitOnCeiling = true;
}
instance->position.y = (float)y;
instance->velocity.y = 0.0;
instance->vsp = 0.0;
}
}
// Return sign of the floal as int (-1, 0, 1)
int ttc_sign(float x)
{
if (x < 0) return -1;
else if (x < 0.0001) return 0;
else return 1;
}
// Return absolute value of float
float ttc_abs(float x)
{
if (x < 0.0) x *= -1.0;
return x;
}
// Clamp value between min and max
float ttc_clamp(float value, float min, float max)
{
const float res = value < min ? min : value;
return res > max ? max : res;
}
// Update and Draw (one frame)
void UpdateDrawFrame(void)
{
UpdateGame();
DrawGame();
}