From f70bf85ec282441fed148ebd1e72468e000e72a3 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 2 Apr 2024 09:29:46 +0800 Subject: [PATCH] ToLive misc component --- mapper_misc.c | 24 ++++++++++++++++++++++++ mapper_misc.h | 12 ++++++++++++ types.c | 6 ++++++ types.h | 1 + 4 files changed, 43 insertions(+) diff --git a/mapper_misc.c b/mapper_misc.c index 68d1364..65d46ca 100644 --- a/mapper_misc.c +++ b/mapper_misc.c @@ -28,6 +28,7 @@ void misc_DeleteComponent(Component_Misc *misc) { void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime) { if (!e->misc || !e->misc->respawn) { WARN("called on an entity without misc or misc.respawn", 0); + e->thinker = NULL; return; } if (app->player->player == NULL) // No player @@ -52,6 +53,7 @@ void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime) { void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime) { if (!e->misc || !e->misc->hazard) { WARN("called on an entity without misc or misc.textbox", 0); + e->thinker = NULL; return; } if (app->player->player == NULL) // No player @@ -72,6 +74,7 @@ void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime) { void misc_thinker_Textbox(App *app, Entity *e, Duration deltaTime) { if (!e->misc || !e->misc->textbox) { WARN("called on an entity without misc or misc.textbox", 0); + e->thinker = NULL; return; } misc_Textbox *t = e->misc->textbox; @@ -94,6 +97,19 @@ void misc_thinker_Textbox(App *app, Entity *e, Duration deltaTime) { t->progress = fmaxf(0.0f, t->progress - duration_Seconds(deltaTime) * MISC_TEXTBOX_FADEIN_SPEED); } +void misc_thinker_ToLive(App *app, Entity *e, Duration deltaTime) { + if (!e->misc || e->misc->tolive.microseconds == 0) { + WARN("called on an entity without misc or misc.tolive", 0); + e->thinker = NULL; + return; + } + + if (e->misc->tolive.microseconds < time_Now().microseconds) { + // After its allocated time + entity_Delete(app->entity, e->id); + } +} + // Utility functions for creating misc entities void misc_InstantiateTextbox(App *app, Entity *e, const char *text, Box2 trigger_box, float offset) { @@ -131,3 +147,11 @@ void misc_InstantiateHazard(App *app, Entity *e, Box2 trigger_box) { e->thinker = &misc_thinker_Hazard; } + +void misc_InstantiateToLive(App *app, Entity *e, Duration duration, TimePoint since) { + ASSERT(e->misc == NULL && "Instantiate must be called with e.misc not set"); + e->misc = zero_malloc(sizeof(Component_Misc)); + e->misc->tolive = time_After(since, duration); + + e->thinker = &misc_thinker_ToLive; +} diff --git a/mapper_misc.h b/mapper_misc.h index 80c424a..c42aac2 100644 --- a/mapper_misc.h +++ b/mapper_misc.h @@ -43,6 +43,10 @@ void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime); // Tracks the player & harms on contact void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime); +// Thinker for tolive +// Kills itself after some time +void misc_thinker_ToLive(App *app, Entity *e, Duration deltaTime); + // Misc data an entity in the map might want. // Used as patches for quick logic like hazard respawn & textbox @@ -50,6 +54,7 @@ typedef struct { misc_Textbox *textbox; misc_HazardRespawn *respawn; Box2 *hazard; // Harms the player on contact if not null, relative + TimePoint tolive; // Deletes itself after this time if it is not 0 } Component_Misc; // Deletes everything nested in misc, and then itself. @@ -68,6 +73,13 @@ void misc_InstantiateHazardRespawn(App *app, Entity *e, Box2 trigger_box, Vec2 r // Creates misc & sets the thinker. void misc_InstantiateHazard(App *app, Entity *e, Box2 trigger_box); +// Inserts the components for the entity to kill itself after some time. +// Creates misc & sets the thinker. +// +// You would normally want to set duration to a fixed amount, +// and since to time_Now(). +void misc_InstantiateToLive(App *app, Entity *e, Duration duration, TimePoint since); + static inline Box2 misc_TextboxUpright(double width, double height) { Box2 b = { diff --git a/types.c b/types.c index 67ee331..fa294f1 100644 --- a/types.c +++ b/types.c @@ -170,6 +170,12 @@ TimePoint time_Now() { return t; } +TimePoint time_After(TimePoint now, Duration after) { + TimePoint p = { + .microseconds = now.microseconds + after.microseconds}; + return p; +} + Duration time_Since(TimePoint prev) { return time_Difference(time_Now(), prev); } diff --git a/types.h b/types.h index 16148a0..c040baa 100644 --- a/types.h +++ b/types.h @@ -107,6 +107,7 @@ typedef struct { } TimePoint; TimePoint time_Now(); +TimePoint time_After(TimePoint now, Duration after); Duration time_Since(TimePoint prev); Duration time_Difference(TimePoint now, TimePoint prev); Duration time_Reset(TimePoint *prev);