diff --git a/app.c b/app.c index 9f575ec..a19aba5 100644 --- a/app.c +++ b/app.c @@ -31,6 +31,7 @@ App *app_NewApp() { app->switch_level = NULL; app->clear_color = 0; app->wantQuit = false; + app->paused = false; Entity *player = entity_Create(app->entity, "player"); @@ -68,11 +69,14 @@ void app_Advance(App *app, Duration deltaTime) { if (app->switch_level != NULL) _app_SwitchLevel(app); - gametime_Advance(app->time, deltaTime); - particle_Advance(app->particle, deltaTime); input_Advance(app->input); - player_Advance(app->player, deltaTime); - physics_Advance(app->physics, deltaTime); - entity_Advance(app->entity, deltaTime); - camera_Advance(app->camera, deltaTime); + + if (!app->paused) { + gametime_Advance(app->time, deltaTime); + particle_Advance(app->particle, deltaTime); + player_Advance(app->player, deltaTime); + physics_Advance(app->physics, deltaTime); + entity_Advance(app->entity, deltaTime); + camera_Advance(app->camera, deltaTime); + } } diff --git a/app.h b/app.h index a0ee901..5c768f5 100644 --- a/app.h +++ b/app.h @@ -29,6 +29,7 @@ typedef struct _App { char *switch_level; uint32_t clear_color; bool wantQuit, debugOn; + bool paused; } App; App *app_NewApp(); diff --git a/app_file.c b/app_file.c index 3585639..c6e985c 100644 --- a/app_file.c +++ b/app_file.c @@ -233,6 +233,7 @@ void _app_SwitchLevel(App *app) { // Clear the current level entity_Clear(app->entity); particle_Clear(app->particle); + app->camera->target = NULL; // Read every line while (!feof(f) && fgets(linebuf, sizeof(linebuf), f)) { diff --git a/app_render.cpp b/app_render.cpp index fc9907b..ca0b586 100644 --- a/app_render.cpp +++ b/app_render.cpp @@ -172,5 +172,11 @@ void app_Render(App *app) { settextcolor(RGB(255, 255, 255)); + + // If paused, display a text + if (app->paused) { + RECT rect = {.left = SCREEN_WIDTH / 2 - 10, .top = 100, .right = SCREEN_WIDTH / 2 + 10, .bottom = 200}; + drawtext("Game Paused", &rect, DT_CENTER | DT_NOCLIP); + } } } diff --git a/input.c b/input.c index 6f597ed..0f44195 100644 --- a/input.c +++ b/input.c @@ -65,7 +65,10 @@ void input_Advance(System_Input *sys) { } if (sys->keys[input_Key_Escape] == JustPressed) { - fprintf(stderr, "[input_Advance] Let's quit now!\n"); - sys->super->wantQuit = true; + if (!sys->super->paused) + fprintf(stderr, "[input_Advance] Pausing\n"); + else + fprintf(stderr, "[input_Advance] Unpausing\n"); + sys->super->paused = !sys->super->paused; } } diff --git a/mapper_misc.c b/mapper_misc.c index 3380d5e..77c2c45 100644 --- a/mapper_misc.c +++ b/mapper_misc.c @@ -68,8 +68,8 @@ void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime) { .fg = 0}; uint64_t emitCooldown = 400.0 * 40000.0 / e->misc->trigger.size.x; // 40 msec across 400px TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData}; - if (time_Since(lastEmit).microseconds > emitCooldown) { - lastEmit = time_Now(); + if (gametime_Since(app->time, lastEmit).microseconds > emitCooldown) { + lastEmit = gametime_Now(app->time); lastEmit.microseconds -= 10000 * rand_Double01(); Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger); particle_Emit( @@ -128,7 +128,7 @@ void misc_thinker_ToLive(App *app, Entity *e, Duration deltaTime) { return; } - if (e->misc->tolive.microseconds < time_Now().microseconds) { + if (e->misc->tolive.microseconds < gametime_Now(app->time).microseconds) { // After its allocated time entity_Delete(app->entity, e->id); } @@ -146,8 +146,8 @@ void misc_thinker_ChangeLevel(App *app, Entity *e, Duration deltaTime) { // Copied from Hazard thinker uint64_t emitCooldown = 400.0 * 60000.0 / e->misc->trigger.size.x; // 60 msec across 400px TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData}; - if (time_Since(lastEmit).microseconds > emitCooldown) { - lastEmit = time_Now(); + if (gametime_Since(app->time, lastEmit).microseconds > emitCooldown) { + lastEmit = gametime_Now(app->time); lastEmit.microseconds -= 10000 * rand_Double01(); Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger); particle_Emit( diff --git a/particle.c b/particle.c index acbecc1..443da7a 100644 --- a/particle.c +++ b/particle.c @@ -4,6 +4,7 @@ #include "util/tree.h" #include "util/vector.h" #include "util/assert.h" +#include "app.h" #include @@ -38,7 +39,7 @@ void particle_DeleteSystem(System_Particle *sys) { static void _particle_AdvancePart(System_Particle *sys, int layer, Particle *p, Duration deltaTime) { p->size -= p->sizedec * duration_Seconds(deltaTime); if ((p->tolive.microseconds != 0 && - time_Since(p->addtime).microseconds > p->tolive.microseconds) || + gametime_Since(sys->super->time, p->addtime).microseconds > p->tolive.microseconds) || p->size < EPS) { // vanished particle_Delete(sys, layer, p->id); return; @@ -82,7 +83,7 @@ void particle_Emit(System_Particle *sys, int layer, Vec2 pos, Vec2 vec, double v Particle *p = tree_Insert(sys->parts[layer], id, NULL); memset(p, 0, sizeof(Particle)); p->id = id; - p->addtime = time_Now(); + p->addtime = gametime_Now(sys->super->time); p->tolive = tolive; p->pos = pos; p->vec = vec; diff --git a/player.c b/player.c index f48efd7..c8fdfe1 100644 --- a/player.c +++ b/player.c @@ -74,12 +74,12 @@ void player_Advance(System_Player *sys, Duration deltaTime) { // Particles static Duration emitCooldown = {.microseconds = 80000}; static TimePoint lastEmit = {.microseconds = 0}; - if (time_Since(p->lastDash).microseconds < dashLength.microseconds && dabs(p->super->position->velocity.x) > EPS) { + if (gametime_Since(sys->super->time, p->lastDash).microseconds < dashLength.microseconds && dabs(p->super->position->velocity.x) > EPS) { // When dashing static TimePoint lastDashEmit = {.microseconds = 0}; static Duration cooldown = {.microseconds = 4000}; - if (time_Since(lastDashEmit).microseconds > cooldown.microseconds) { - lastDashEmit = time_Now(); + if (gametime_Since(sys->super->time, lastDashEmit).microseconds > cooldown.microseconds) { + lastDashEmit = gametime_Now(sys->super->time); Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y / 2.0)); particle_Emit( sys->super->particle, @@ -89,12 +89,12 @@ void player_Advance(System_Player *sys, Duration deltaTime) { 7, rand_DoubleRange(14, 20), rand_DoubleRange(32, 40), duration_FromSeconds(0), &render_ModeRotate); } - lastEmit = time_Now(); + lastEmit = gametime_Now(sys->super->time); lastEmit.microseconds -= 74000; } else { // When not dashing - if (time_Since(lastEmit).microseconds > emitCooldown.microseconds) { - lastEmit = time_Now(); + if (gametime_Since(sys->super->time, lastEmit).microseconds > emitCooldown.microseconds) { + lastEmit = gametime_Now(sys->super->time); lastEmit.microseconds += duration_FromSeconds(0.05 * rand_Double01()).microseconds; Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y)); particle_Emit( @@ -148,13 +148,13 @@ void player_Advance(System_Player *sys, Duration deltaTime) { p->dashCount = 0; if (input_IsPressed(input->keys[input_Key_Dash]) && (p->onGround || p->dashCount < airdashCount) && - time_Since(p->lastDash).microseconds > dashCooldown.microseconds) { - p->lastDash = time_Now(); + gametime_Since(sys->super->time, p->lastDash).microseconds > dashCooldown.microseconds) { + p->lastDash = gametime_Now(sys->super->time); if (!p->onGround) p->dashCount++; } // Am I dashing right now? - if (time_Since(p->lastDash).microseconds < dashLength.microseconds) { + if (gametime_Since(sys->super->time, p->lastDash).microseconds < dashLength.microseconds) { p->super->position->velocity.x += p->faceDirection * dashSpeed; p->super->position->velocity.y = 0; } else { // Release the stored Y speed