Add pausing
This commit is contained in:
43
game.c
43
game.c
@@ -123,6 +123,9 @@ static float aspect;
|
|||||||
static unsigned level;
|
static unsigned level;
|
||||||
|
|
||||||
static bool dead;
|
static bool dead;
|
||||||
|
static bool clear;
|
||||||
|
static bool paused;
|
||||||
|
|
||||||
static unsigned asteroid_count;
|
static unsigned asteroid_count;
|
||||||
static uint8_t counter;
|
static uint8_t counter;
|
||||||
static unsigned score;
|
static unsigned score;
|
||||||
@@ -233,10 +236,10 @@ static void spawn_asteroid()
|
|||||||
const float rnd = rng_canon();
|
const float rnd = rng_canon();
|
||||||
if (rnd < 0.4)
|
if (rnd < 0.4)
|
||||||
r = ASTEROID_MEDIUM;
|
r = ASTEROID_MEDIUM;
|
||||||
else if (rnd < 0.8)
|
else if (level > 3 && rnd < 0.45)
|
||||||
r = ASTEROID_LARGE;
|
|
||||||
else
|
|
||||||
r = ASTEROID_HUGE;
|
r = ASTEROID_HUGE;
|
||||||
|
else
|
||||||
|
r = ASTEROID_LARGE;
|
||||||
|
|
||||||
unsigned id;
|
unsigned id;
|
||||||
entity_t *e = gen_asteroid(r, &id);
|
entity_t *e = gen_asteroid(r, &id);
|
||||||
@@ -349,7 +352,9 @@ static void bounce(collision_t c)
|
|||||||
|
|
||||||
static void cleared()
|
static void cleared()
|
||||||
{
|
{
|
||||||
|
clear = true;
|
||||||
renderer_set_wrap(false);
|
renderer_set_wrap(false);
|
||||||
|
counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
|
static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
|
||||||
@@ -401,6 +406,13 @@ static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void die()
|
||||||
|
{
|
||||||
|
dead = true;
|
||||||
|
clear = false;
|
||||||
|
counter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void handle_collisions(const collision_t *collisions, unsigned count)
|
static void handle_collisions(const collision_t *collisions, unsigned count)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < count; ++i) {
|
for (unsigned i = 0; i < count; ++i) {
|
||||||
@@ -409,7 +421,7 @@ static void handle_collisions(const collision_t *collisions, unsigned count)
|
|||||||
entity_t *b = entities + c.entities[1];
|
entity_t *b = entities + c.entities[1];
|
||||||
|
|
||||||
if (a->tag == COLLISION_SHIP || b->tag == COLLISION_SHIP) {
|
if (a->tag == COLLISION_SHIP || b->tag == COLLISION_SHIP) {
|
||||||
dead = true;
|
die();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -459,7 +471,8 @@ static void draw_arrow()
|
|||||||
static void create_field()
|
static void create_field()
|
||||||
{
|
{
|
||||||
dead = false;
|
dead = false;
|
||||||
counter = 0;
|
clear = false;
|
||||||
|
paused = false;
|
||||||
entity_count = shape_count = 0;
|
entity_count = shape_count = 0;
|
||||||
asteroid_count = 0;
|
asteroid_count = 0;
|
||||||
|
|
||||||
@@ -497,10 +510,17 @@ static void reset()
|
|||||||
create_field();
|
create_field();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pause()
|
||||||
|
{
|
||||||
|
paused = !paused;
|
||||||
|
counter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void game_init(float _aspect)
|
void game_init(float _aspect)
|
||||||
{
|
{
|
||||||
input_on_shoot(shoot);
|
input_on_shoot(shoot);
|
||||||
input_on_restart(reset);
|
input_on_restart(reset);
|
||||||
|
input_on_pause(pause);
|
||||||
aspect = _aspect;
|
aspect = _aspect;
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
@@ -508,9 +528,9 @@ void game_init(float _aspect)
|
|||||||
|
|
||||||
void game_update()
|
void game_update()
|
||||||
{
|
{
|
||||||
if (dead || asteroid_count == 0)
|
if (dead || clear || paused)
|
||||||
++counter;
|
++counter;
|
||||||
if (dead)
|
if (dead || paused)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ship_update();
|
ship_update();
|
||||||
@@ -521,7 +541,7 @@ void game_update()
|
|||||||
e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir);
|
e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir);
|
||||||
e->pos = vec2_add(e->pos, e->vel);
|
e->pos = vec2_add(e->pos, e->vel);
|
||||||
|
|
||||||
if (asteroid_count == 0 && i == ship_entity_id) {
|
if (clear && i == ship_entity_id) {
|
||||||
if (e->pos.x > aspect)
|
if (e->pos.x > aspect)
|
||||||
win();
|
win();
|
||||||
}
|
}
|
||||||
@@ -560,10 +580,15 @@ void game_draw()
|
|||||||
|
|
||||||
text_draw_score(aspect, score);
|
text_draw_score(aspect, score);
|
||||||
|
|
||||||
|
if (paused && !(counter & COUNTER_MASK)) {
|
||||||
|
text_draw_centre("PAUSED");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (dead && !(counter & COUNTER_MASK))
|
if (dead && !(counter & COUNTER_MASK))
|
||||||
text_draw_centre("GAME OVER");
|
text_draw_centre("GAME OVER");
|
||||||
|
|
||||||
if (!dead && asteroid_count == 0) {
|
if (clear) {
|
||||||
draw_arrow();
|
draw_arrow();
|
||||||
if (!(counter & COUNTER_MASK))
|
if (!(counter & COUNTER_MASK))
|
||||||
text_draw_centre("CLEAR");
|
text_draw_centre("CLEAR");
|
||||||
|
|||||||
11
input.c
11
input.c
@@ -19,6 +19,7 @@ static int input_fd;
|
|||||||
static input_callback_t restart;
|
static input_callback_t restart;
|
||||||
static input_callback_t quit;
|
static input_callback_t quit;
|
||||||
static input_callback_t shoot;
|
static input_callback_t shoot;
|
||||||
|
static input_callback_t pause_;
|
||||||
|
|
||||||
static void key_press(int key)
|
static void key_press(int key)
|
||||||
{
|
{
|
||||||
@@ -46,6 +47,9 @@ static void key_press(int key)
|
|||||||
if (shoot != nullptr)
|
if (shoot != nullptr)
|
||||||
shoot();
|
shoot();
|
||||||
break;
|
break;
|
||||||
|
case KEY_P:
|
||||||
|
if (pause_ != nullptr)
|
||||||
|
pause_();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +78,7 @@ static void key_repeat(int key)
|
|||||||
int input_init()
|
int input_init()
|
||||||
{
|
{
|
||||||
memset(&input, 0, sizeof(input));
|
memset(&input, 0, sizeof(input));
|
||||||
shoot = nullptr;
|
shoot = restart = quit = pause_ = nullptr;
|
||||||
|
|
||||||
input_fd = open("/dev/input/event0", O_RDONLY);
|
input_fd = open("/dev/input/event0", O_RDONLY);
|
||||||
assert(input_fd != -1);
|
assert(input_fd != -1);
|
||||||
@@ -105,6 +109,11 @@ void input_on_quit(input_callback_t cb)
|
|||||||
quit = cb;
|
quit = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void input_on_pause(input_callback_t cb)
|
||||||
|
{
|
||||||
|
pause_ = cb;
|
||||||
|
}
|
||||||
|
|
||||||
void input_handle()
|
void input_handle()
|
||||||
{
|
{
|
||||||
struct input_event ev;
|
struct input_event ev;
|
||||||
|
|||||||
1
input.h
1
input.h
@@ -16,6 +16,7 @@ void input_cleanup();
|
|||||||
void input_on_shoot(input_callback_t cb);
|
void input_on_shoot(input_callback_t cb);
|
||||||
void input_on_restart(input_callback_t cb);
|
void input_on_restart(input_callback_t cb);
|
||||||
void input_on_quit(input_callback_t cb);
|
void input_on_quit(input_callback_t cb);
|
||||||
|
void input_on_pause(input_callback_t cb);
|
||||||
|
|
||||||
void input_handle();
|
void input_handle();
|
||||||
|
|
||||||
|
|||||||
31
text.c
31
text.c
@@ -130,6 +130,13 @@ static const glyph_t font[] = {
|
|||||||
{ { 1, 4 }, { -1, 4 }, { -1, -4 }, { 1, -4 } },
|
{ { 1, 4 }, { -1, 4 }, { -1, -4 }, { 1, -4 } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
['D'] = {
|
||||||
|
.line_count = 1,
|
||||||
|
.line_lens = { 5 },
|
||||||
|
.lines = {
|
||||||
|
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 0, 4 }, { -1, 4 } },
|
||||||
|
},
|
||||||
|
},
|
||||||
['E'] = {
|
['E'] = {
|
||||||
.line_count = 2,
|
.line_count = 2,
|
||||||
.line_lens = { 4, 2 },
|
.line_lens = { 4, 2 },
|
||||||
@@ -166,6 +173,13 @@ static const glyph_t font[] = {
|
|||||||
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 1, 4 }, { -1, 4 } },
|
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 1, 4 }, { -1, 4 } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
['P'] = {
|
||||||
|
.line_count = 1,
|
||||||
|
.line_lens = { 5 },
|
||||||
|
.lines = {
|
||||||
|
{ { -1, -4 }, { -1, 4 }, { 1, 4 }, { 1, -2 }, { -1, -2 } },
|
||||||
|
},
|
||||||
|
},
|
||||||
['R'] = {
|
['R'] = {
|
||||||
.line_count = 1,
|
.line_count = 1,
|
||||||
.line_lens = { 6 },
|
.line_lens = { 6 },
|
||||||
@@ -176,6 +190,23 @@ static const glyph_t font[] = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
['S'] = {
|
||||||
|
.line_count = 1,
|
||||||
|
.line_lens = { 6 },
|
||||||
|
.lines = {
|
||||||
|
{
|
||||||
|
{ 1, 4 }, { -1, 4 }, { -1, -2 },
|
||||||
|
{ 1, -2 }, { 1, -4 }, { -1, -4 },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
['U'] = {
|
||||||
|
.line_count = 1,
|
||||||
|
.line_lens = { 4 },
|
||||||
|
.lines = {
|
||||||
|
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 1, 4 } },
|
||||||
|
},
|
||||||
|
},
|
||||||
['V'] = {
|
['V'] = {
|
||||||
.line_count = 1,
|
.line_count = 1,
|
||||||
.line_lens = { 3 },
|
.line_lens = { 3 },
|
||||||
|
|||||||
Reference in New Issue
Block a user