Add game over message

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent 7ed120899c
commit 10be5ec391

54
game.c
View File

@@ -51,6 +51,11 @@
#define MAX_SHAPES_PER_ENTITY 2 #define MAX_SHAPES_PER_ENTITY 2
#define MAX_COLLISIONS 128U #define MAX_COLLISIONS 128U
#define GAME_OVER_WIDTH 24
#define GAME_OVER_HEIGHT 8
#define GAME_OVER_SCALE 0.025
#define GAME_OVER_COUNTER_MASK (1 << 6)
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
typedef enum { typedef enum {
@@ -98,6 +103,40 @@ static const vec2_t fire_verts[] = {
}; };
static const vec2_t shot_verts[] = { { 0.0, -0.02 }, { 0.0, 0.02 } }; static const vec2_t shot_verts[] = { { 0.0, -0.02 }, { 0.0, 0.02 } };
static const vec2_t game_over_verts[][MAX_VERTS] = {
// G
{ { -10, 4 }, { -12, 4 }, { -12, -4 }, { -10, -4 }, { -10, 0 } },
// A
{ { -9, -4 }, { -8, 4 }, { -7, -4 } },
{ { -8.75, -2 }, { -7.25, -2 } },
// M
{ { -6, -4 }, { -6, 4 }, { -5, -4 }, { -4, 4 }, { -4, -4 } },
// E
{ { -1, 4 }, { -3, 4 }, { -3, -4 }, { -1, -4 } },
{ { -3, 0 }, { -2, 0 } },
// O
{ { 1, 4 }, { 1, -4 }, { 3, -4 }, { 3, 4 }, { 1, 4 } },
// V
{ { 4, 4 }, { 5, -4 }, { 6, 4 } },
// E
{ { 9, 4 }, { 7, 4 }, { 7, -4 }, { 9, -4 } },
{ { 7, 0 }, { 8, 0 } },
// R
{ { 10, -4 }, { 10, 4 }, { 12, 4 }, { 12, -2 }, { 10, -2 }, { 12, -4 } },
};
static const unsigned game_over_lens[NELEMS(game_over_verts)] = {
5, 3, 2, 5, 4, 2, 5, 3, 4, 2, 6,
};
static const mat3_t game_over_transform = {
{ GAME_OVER_SCALE, 0, 0 },
{ 0, GAME_OVER_SCALE, 0 },
{ 0, 0, 1 },
};
static const vec2_t game_over_bg = {
.x = (GAME_OVER_WIDTH + 2) * GAME_OVER_SCALE,
.y = (GAME_OVER_HEIGHT + 2) * GAME_OVER_SCALE,
};
static entity_t entities[MAX_ENTITIES]; static entity_t entities[MAX_ENTITIES];
static mat3_t transforms[MAX_ENTITIES]; static mat3_t transforms[MAX_ENTITIES];
static shape_t shapes[MAX_SHAPES]; static shape_t shapes[MAX_SHAPES];
@@ -108,6 +147,7 @@ static unsigned asteroid_count;
static bool dead; static bool dead;
static float aspect; static float aspect;
static uint8_t counter;
static unsigned ship_entity_id; static unsigned ship_entity_id;
static unsigned ship_shape_id; static unsigned ship_shape_id;
@@ -414,6 +454,7 @@ void game_init(float _aspect)
aspect = _aspect; aspect = _aspect;
dead = false; dead = false;
counter = 0;
entity_count = shape_count = 0; entity_count = shape_count = 0;
entity_t *ship = add_entity(&ship_entity_id); entity_t *ship = add_entity(&ship_entity_id);
@@ -438,8 +479,10 @@ void game_init(float _aspect)
void game_update() void game_update()
{ {
if (dead) if (dead) {
++counter;
return; return;
}
ship_update(); ship_update();
@@ -480,4 +523,13 @@ void game_draw()
shapes[i].verts, shapes[i].vert_count, transform, shapes[i].verts, shapes[i].vert_count, transform,
shapes[i].connect); shapes[i].connect);
} }
if (dead && !(counter & GAME_OVER_COUNTER_MASK)) {
renderer_clear_rect((vec2_t) { 0, 0 }, game_over_bg);
for (unsigned i = 0; i < NELEMS(game_over_verts); ++i) {
renderer_draw(
game_over_verts[i], game_over_lens[i], game_over_transform,
false);
}
}
} }