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

6
app.c
View File

@ -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);
input_Advance(app->input);
if (!app->paused) {
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);
}
}

1
app.h
View File

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

View File

@ -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)) {

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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(

View File

@ -4,6 +4,7 @@
#include "util/tree.h"
#include "util/vector.h"
#include "util/assert.h"
#include "app.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) {
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;

View File

@ -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