ToLive misc component

This commit is contained in:
Edgaru089 2024-04-02 09:29:46 +08:00
parent bba3e9d15b
commit f70bf85ec2
4 changed files with 43 additions and 0 deletions

View File

@ -28,6 +28,7 @@ void misc_DeleteComponent(Component_Misc *misc) {
void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime) { void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime) {
if (!e->misc || !e->misc->respawn) { if (!e->misc || !e->misc->respawn) {
WARN("called on an entity without misc or misc.respawn", 0); WARN("called on an entity without misc or misc.respawn", 0);
e->thinker = NULL;
return; return;
} }
if (app->player->player == NULL) // No player 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) { void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime) {
if (!e->misc || !e->misc->hazard) { if (!e->misc || !e->misc->hazard) {
WARN("called on an entity without misc or misc.textbox", 0); WARN("called on an entity without misc or misc.textbox", 0);
e->thinker = NULL;
return; return;
} }
if (app->player->player == NULL) // No player 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) { void misc_thinker_Textbox(App *app, Entity *e, Duration deltaTime) {
if (!e->misc || !e->misc->textbox) { if (!e->misc || !e->misc->textbox) {
WARN("called on an entity without misc or misc.textbox", 0); WARN("called on an entity without misc or misc.textbox", 0);
e->thinker = NULL;
return; return;
} }
misc_Textbox *t = e->misc->textbox; 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); 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 // Utility functions for creating misc entities
void misc_InstantiateTextbox(App *app, Entity *e, const char *text, Box2 trigger_box, float offset) { 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; 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;
}

View File

@ -43,6 +43,10 @@ void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime);
// Tracks the player & harms on contact // Tracks the player & harms on contact
void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime); 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. // Misc data an entity in the map might want.
// Used as patches for quick logic like hazard respawn & textbox // Used as patches for quick logic like hazard respawn & textbox
@ -50,6 +54,7 @@ typedef struct {
misc_Textbox *textbox; misc_Textbox *textbox;
misc_HazardRespawn *respawn; misc_HazardRespawn *respawn;
Box2 *hazard; // Harms the player on contact if not null, relative 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; } Component_Misc;
// Deletes everything nested in misc, and then itself. // 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. // Creates misc & sets the thinker.
void misc_InstantiateHazard(App *app, Entity *e, Box2 trigger_box); 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) { static inline Box2 misc_TextboxUpright(double width, double height) {
Box2 b = { Box2 b = {

View File

@ -170,6 +170,12 @@ TimePoint time_Now() {
return t; return t;
} }
TimePoint time_After(TimePoint now, Duration after) {
TimePoint p = {
.microseconds = now.microseconds + after.microseconds};
return p;
}
Duration time_Since(TimePoint prev) { Duration time_Since(TimePoint prev) {
return time_Difference(time_Now(), prev); return time_Difference(time_Now(), prev);
} }

View File

@ -107,6 +107,7 @@ typedef struct {
} TimePoint; } TimePoint;
TimePoint time_Now(); TimePoint time_Now();
TimePoint time_After(TimePoint now, Duration after);
Duration time_Since(TimePoint prev); Duration time_Since(TimePoint prev);
Duration time_Difference(TimePoint now, TimePoint prev); Duration time_Difference(TimePoint now, TimePoint prev);
Duration time_Reset(TimePoint *prev); Duration time_Reset(TimePoint *prev);