Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Added score
Browse files Browse the repository at this point in the history
  • Loading branch information
xfnty committed Jun 28, 2023
1 parent 9da3d90 commit 28c1ce0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/engine/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int engine_run(engine_t* engine, int argc, const char** argv) {
if (!_engine_init(engine, argc, argv))
return engine->exit_error;

while (_engine_tick(engine));
while (_engine_tick(engine) && !WindowShouldClose());

_engine_shutdown(engine);

Expand Down
2 changes: 0 additions & 2 deletions src/game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ void game_tick(game_t* game, update_context_t ctx) {

ClearBackground(BLACK);
game_state_update(&game->state, game, ctx);

game->is_running = !WindowShouldClose();
}

void game_debug_draw(game_t* game, update_context_t ctx) {
Expand Down
95 changes: 88 additions & 7 deletions src/game/states/gameplay.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "game/states/gameplay.h"

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <assert.h>

#include <kvec.h>
#include <raylib.h>
Expand All @@ -27,12 +28,16 @@ static struct {
Texture2D base;
Texture2D pipe;
Texture2D bird;
Texture2D digits[10];
} sprites = {0};

typedef struct gameplay_state_s {
bool is_paused;
float bird_y;
float bird_y_speed;
bool is_bird_between_pipes;
bool is_debug;
int score;
kvec_t(Vector2) pipes;
} gameplay_state_t;

Expand All @@ -41,6 +46,8 @@ static void draw_pipe(Vector2 center, gameplay_state_t* gameplay);
static void draw_pipes(gameplay_state_t* gameplay, game_t* game, update_context_t ctx);
static void draw_base(gameplay_state_t* gameplay, game_t* game, update_context_t ctx);
static void draw_bird(gameplay_state_t* gameplay, game_t* game);
static void draw_score(gameplay_state_t* gameplay, game_t* game);
static void debug_draw(gameplay_state_t* gameplay, game_t* game);
static void update_bird(gameplay_state_t* gameplay, game_t* game);
static void update_pipes(gameplay_state_t* gameplay, game_t* game);

Expand All @@ -55,6 +62,8 @@ static void _gameplay_state_enter(game_state_t* state, game_t* game) {
if (sprites.base.id == 0) sprites.base = LoadTexture("assets/base.png");
if (sprites.pipe.id == 0) sprites.pipe = LoadTexture("assets/pipe.png");
if (sprites.bird.id == 0) sprites.bird = LoadTexture("assets/midflap.png");
for (int i = 0; i < STACKARRAY_SIZE(sprites.digits); i++)
if (sprites.digits[i].id == 0) sprites.digits[i] = LoadTexture(TextFormat("assets/%d.png", i));

gameplay->bird_y = ((float)game->canvas.texture.height - sprites.base.height) / 2;

Expand Down Expand Up @@ -82,9 +91,15 @@ static void _gameplay_state_update(game_state_t* state, game_t* game, update_con
draw_pipes(gameplay, game, ctx);
draw_base(gameplay, game, ctx);
draw_bird(gameplay, game);
draw_score(gameplay, game);

if (gameplay->is_debug)
debug_draw(gameplay, game);

if (IsKeyPressed(KEY_ENTER))
gameplay->is_paused = !gameplay->is_paused;
if (IsKeyPressed(KEY_D))
gameplay->is_debug = !gameplay->is_debug;
if (IsKeyPressed(KEY_ESCAPE))
game->is_running = false;
}
Expand Down Expand Up @@ -143,8 +158,6 @@ void draw_pipes(gameplay_state_t* gameplay, game_t* game, update_context_t ctx)
}

void draw_base(gameplay_state_t* gameplay, game_t* game, update_context_t ctx) {
// FIXME: flickering every second

float shift = (gameplay->is_paused) ? 0 : scroll_speed * (GetTime() - (int)GetTime());
for (int i = 0; sprites.base.width * i - shift <= game->canvas.texture.width; i++) {
DrawTexture(
Expand Down Expand Up @@ -172,6 +185,65 @@ void draw_bird(gameplay_state_t* gameplay, game_t* game) {
);
}

void draw_score(gameplay_state_t* gameplay, game_t* game) {
const char* s = TextFormat("%d", gameplay->score);
float l = 0;

for (int i = 0; i < TextLength(s); i++)
l += sprites.digits[s[i] - '0'].width;

float t = 0;
for (int i = 0; i < TextLength(s); i++) {
DrawTexture(
sprites.digits[s[i] - '0'],
game->canvas.texture.width / 2.0f - l / 2 + t,
50,
WHITE
);
t += sprites.digits[s[i] - '0'].width;
}
}

void debug_draw(gameplay_state_t* gameplay, game_t* game) {
DrawRectangleLinesEx(
(Rectangle) {
game->canvas.texture.width / 2.0f - sprites.bird.width / 2.0f,
gameplay->bird_y - (float)sprites.bird.height / 2,
sprites.bird.width,
sprites.bird.height
},
1,
RED
);

for (int i = 0; i < kv_size(gameplay->pipes); i++) {
Vector2 center = kv_A(gameplay->pipes, i);
Vector2 pipe_size = {(float)sprites.pipe.width, (float)sprites.pipe.height};
Rectangle top_pipe_rect = {
.x = center.x - pipe_size.x / 2,
.y = center.y - pipe_window_height / 2 - pipe_size.y,
.width = pipe_size.x,
.height = pipe_size.y
};
Rectangle bottom_pipe_rect = {
.x = center.x - pipe_size.x / 2,
.y = center.y + pipe_window_height / 2,
.width = pipe_size.x,
.height = pipe_size.y
};
Rectangle score_trigger_rect = {
.x = center.x - pipe_size.x / 2,
.y = center.y - pipe_window_height / 2,
.width = pipe_size.x,
.height = pipe_window_height
};

DrawRectangleLinesEx(top_pipe_rect, 1, ORANGE);
DrawRectangleLinesEx(bottom_pipe_rect, 1, ORANGE);
DrawRectangleLinesEx(score_trigger_rect, 3, GREEN);
}
}

void update_bird(gameplay_state_t* gameplay, game_t* game) {
gameplay->bird_y_speed += bird_gravity_force * GetFrameTime();
if (IsKeyPressed(KEY_SPACE))
Expand All @@ -188,8 +260,8 @@ void update_bird(gameplay_state_t* gameplay, game_t* game) {
sprites.bird.width,
sprites.bird.height
};
DrawRectangleLinesEx(bird_rect, 1, RED);

bool bird_between_pipes = false;
for (int i = 0; i < kv_size(gameplay->pipes); i++) {
Vector2 center = kv_A(gameplay->pipes, i);
Vector2 pipe_size = {(float)sprites.pipe.width, (float)sprites.pipe.height};
Expand All @@ -205,13 +277,22 @@ void update_bird(gameplay_state_t* gameplay, game_t* game) {
.width = pipe_size.x,
.height = pipe_size.y
};

DrawRectangleLinesEx(top_pipe_rect, 1, ORANGE);
DrawRectangleLinesEx(bottom_pipe_rect, 1, ORANGE);
Rectangle score_trigger_rect = {
.x = center.x - pipe_size.x / 2,
.y = center.y - pipe_window_height / 2,
.width = pipe_size.x,
.height = pipe_window_height
};

if (CheckCollisionRecs(bird_rect, top_pipe_rect) || CheckCollisionRecs(bird_rect, bottom_pipe_rect))
game_switch_state(game, gameplay_state_create());

bird_between_pipes |= CheckCollisionRecs(bird_rect, score_trigger_rect);
}

if (!bird_between_pipes && gameplay->is_bird_between_pipes)
gameplay->score++;
gameplay->is_bird_between_pipes = bird_between_pipes;
}

void update_pipes(gameplay_state_t* gameplay, game_t* game) {
Expand Down
3 changes: 2 additions & 1 deletion src/system/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define ID_INVALID (~(id_t)0)

#define CONSTRAIN(v, a, b) ((v < a) ? a : ((v > b) ? b : v))

#define MIN(a, b) ((a < b) ? (a) : (b))
#define MAX(a, b) ((a > b) ? (a) : (b))

#endif

0 comments on commit 28c1ce0

Please sign in to comment.