Pausing the game

I love this
This commit is contained in:
Edgaru089 2024-04-22 05:28:50 +08:00
parent 787f94a8b2
commit bef08f583f
8 changed files with 40 additions and 24 deletions

16
app.c
View File

@ -31,6 +31,7 @@ App *app_NewApp() {
app->switch_level = NULL; app->switch_level = NULL;
app->clear_color = 0; app->clear_color = 0;
app->wantQuit = false; app->wantQuit = false;
app->paused = false;
Entity *player = entity_Create(app->entity, "player"); Entity *player = entity_Create(app->entity, "player");
@ -68,11 +69,14 @@ void app_Advance(App *app, Duration deltaTime) {
if (app->switch_level != NULL) if (app->switch_level != NULL)
_app_SwitchLevel(app); _app_SwitchLevel(app);
gametime_Advance(app->time, deltaTime);
particle_Advance(app->particle, deltaTime);
input_Advance(app->input); input_Advance(app->input);
player_Advance(app->player, deltaTime);
physics_Advance(app->physics, deltaTime); if (!app->paused) {
entity_Advance(app->entity, deltaTime); gametime_Advance(app->time, deltaTime);
camera_Advance(app->camera, 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);
}
} }

1
app.h
View File

@ -29,6 +29,7 @@ typedef struct _App {
char *switch_level; char *switch_level;
uint32_t clear_color; uint32_t clear_color;
bool wantQuit, debugOn; bool wantQuit, debugOn;
bool paused;
} App; } App;
App *app_NewApp(); App *app_NewApp();

View File

@ -233,6 +233,7 @@ void _app_SwitchLevel(App *app) {
// Clear the current level // Clear the current level
entity_Clear(app->entity); entity_Clear(app->entity);
particle_Clear(app->particle); particle_Clear(app->particle);
app->camera->target = NULL;
// Read every line // Read every line
while (!feof(f) && fgets(linebuf, sizeof(linebuf), f)) { while (!feof(f) && fgets(linebuf, sizeof(linebuf), f)) {

View File

@ -172,5 +172,11 @@ void app_Render(App *app) {
settextcolor(RGB(255, 255, 255)); 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);
}
} }
} }

View File

@ -65,7 +65,10 @@ void input_Advance(System_Input *sys) {
} }
if (sys->keys[input_Key_Escape] == JustPressed) { if (sys->keys[input_Key_Escape] == JustPressed) {
fprintf(stderr, "[input_Advance] Let's quit now!\n"); if (!sys->super->paused)
sys->super->wantQuit = true; fprintf(stderr, "[input_Advance] Pausing\n");
else
fprintf(stderr, "[input_Advance] Unpausing\n");
sys->super->paused = !sys->super->paused;
} }
} }

View File

@ -68,8 +68,8 @@ void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime) {
.fg = 0}; .fg = 0};
uint64_t emitCooldown = 400.0 * 40000.0 / e->misc->trigger.size.x; // 40 msec across 400px uint64_t emitCooldown = 400.0 * 40000.0 / e->misc->trigger.size.x; // 40 msec across 400px
TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData}; TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData};
if (time_Since(lastEmit).microseconds > emitCooldown) { if (gametime_Since(app->time, lastEmit).microseconds > emitCooldown) {
lastEmit = time_Now(); lastEmit = gametime_Now(app->time);
lastEmit.microseconds -= 10000 * rand_Double01(); lastEmit.microseconds -= 10000 * rand_Double01();
Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger); Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger);
particle_Emit( particle_Emit(
@ -128,7 +128,7 @@ void misc_thinker_ToLive(App *app, Entity *e, Duration deltaTime) {
return; return;
} }
if (e->misc->tolive.microseconds < time_Now().microseconds) { if (e->misc->tolive.microseconds < gametime_Now(app->time).microseconds) {
// After its allocated time // After its allocated time
entity_Delete(app->entity, e->id); entity_Delete(app->entity, e->id);
} }
@ -146,8 +146,8 @@ void misc_thinker_ChangeLevel(App *app, Entity *e, Duration deltaTime) {
// Copied from Hazard thinker // Copied from Hazard thinker
uint64_t emitCooldown = 400.0 * 60000.0 / e->misc->trigger.size.x; // 60 msec across 400px uint64_t emitCooldown = 400.0 * 60000.0 / e->misc->trigger.size.x; // 60 msec across 400px
TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData}; TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData};
if (time_Since(lastEmit).microseconds > emitCooldown) { if (gametime_Since(app->time, lastEmit).microseconds > emitCooldown) {
lastEmit = time_Now(); lastEmit = gametime_Now(app->time);
lastEmit.microseconds -= 10000 * rand_Double01(); lastEmit.microseconds -= 10000 * rand_Double01();
Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger); Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger);
particle_Emit( particle_Emit(

View File

@ -4,6 +4,7 @@
#include "util/tree.h" #include "util/tree.h"
#include "util/vector.h" #include "util/vector.h"
#include "util/assert.h" #include "util/assert.h"
#include "app.h"
#include <stdio.h> #include <stdio.h>
@ -38,7 +39,7 @@ void particle_DeleteSystem(System_Particle *sys) {
static void _particle_AdvancePart(System_Particle *sys, int layer, Particle *p, Duration deltaTime) { static void _particle_AdvancePart(System_Particle *sys, int layer, Particle *p, Duration deltaTime) {
p->size -= p->sizedec * duration_Seconds(deltaTime); p->size -= p->sizedec * duration_Seconds(deltaTime);
if ((p->tolive.microseconds != 0 && 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 p->size < EPS) { // vanished
particle_Delete(sys, layer, p->id); particle_Delete(sys, layer, p->id);
return; 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); Particle *p = tree_Insert(sys->parts[layer], id, NULL);
memset(p, 0, sizeof(Particle)); memset(p, 0, sizeof(Particle));
p->id = id; p->id = id;
p->addtime = time_Now(); p->addtime = gametime_Now(sys->super->time);
p->tolive = tolive; p->tolive = tolive;
p->pos = pos; p->pos = pos;
p->vec = vec; p->vec = vec;

View File

@ -74,12 +74,12 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
// Particles // Particles
static Duration emitCooldown = {.microseconds = 80000}; static Duration emitCooldown = {.microseconds = 80000};
static TimePoint lastEmit = {.microseconds = 0}; 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 // When dashing
static TimePoint lastDashEmit = {.microseconds = 0}; static TimePoint lastDashEmit = {.microseconds = 0};
static Duration cooldown = {.microseconds = 4000}; static Duration cooldown = {.microseconds = 4000};
if (time_Since(lastDashEmit).microseconds > cooldown.microseconds) { if (gametime_Since(sys->super->time, lastDashEmit).microseconds > cooldown.microseconds) {
lastDashEmit = time_Now(); 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)); Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y / 2.0));
particle_Emit( particle_Emit(
sys->super->particle, sys->super->particle,
@ -89,12 +89,12 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
7, rand_DoubleRange(14, 20), rand_DoubleRange(32, 40), 7, rand_DoubleRange(14, 20), rand_DoubleRange(32, 40),
duration_FromSeconds(0), &render_ModeRotate); duration_FromSeconds(0), &render_ModeRotate);
} }
lastEmit = time_Now(); lastEmit = gametime_Now(sys->super->time);
lastEmit.microseconds -= 74000; lastEmit.microseconds -= 74000;
} else { } else {
// When not dashing // When not dashing
if (time_Since(lastEmit).microseconds > emitCooldown.microseconds) { if (gametime_Since(sys->super->time, lastEmit).microseconds > emitCooldown.microseconds) {
lastEmit = time_Now(); lastEmit = gametime_Now(sys->super->time);
lastEmit.microseconds += duration_FromSeconds(0.05 * rand_Double01()).microseconds; 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)); Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y));
particle_Emit( particle_Emit(
@ -148,13 +148,13 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
p->dashCount = 0; p->dashCount = 0;
if (input_IsPressed(input->keys[input_Key_Dash]) && if (input_IsPressed(input->keys[input_Key_Dash]) &&
(p->onGround || p->dashCount < airdashCount) && (p->onGround || p->dashCount < airdashCount) &&
time_Since(p->lastDash).microseconds > dashCooldown.microseconds) { gametime_Since(sys->super->time, p->lastDash).microseconds > dashCooldown.microseconds) {
p->lastDash = time_Now(); p->lastDash = gametime_Now(sys->super->time);
if (!p->onGround) if (!p->onGround)
p->dashCount++; p->dashCount++;
} }
// Am I dashing right now? // 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.x += p->faceDirection * dashSpeed;
p->super->position->velocity.y = 0; p->super->position->velocity.y = 0;
} else { // Release the stored Y speed } else { // Release the stored Y speed