From 6bae280a0cd42288ab849b941fedaa2167396891 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 2 Apr 2024 08:56:35 +0800 Subject: [PATCH] Hazard areas are working --- app.c | 4 ++++ app_render.cpp | 16 ++++++++++++++++ mapper_misc.c | 29 +++++++++++++++++++++++++++++ mapper_misc.h | 9 +++++++++ types.h | 6 ++++++ 5 files changed, 64 insertions(+) diff --git a/app.c b/app.c index 8d34805..10af39b 100644 --- a/app.c +++ b/app.c @@ -85,6 +85,10 @@ App *app_NewApp() { misc_InstantiateHazardRespawn(app, respawn1, box2(300, 500, 400, 50), vec2(500, 550)); entity_Commit(app->entity, respawn1); + Entity *hazard1 = entity_Create(app->entity, "hazard1"); + misc_InstantiateHazard(app, hazard1, box2(800, 545, 100, 5)); + entity_Commit(app->entity, hazard1); + return app; } diff --git a/app_render.cpp b/app_render.cpp index fb7aad9..6d9c360 100644 --- a/app_render.cpp +++ b/app_render.cpp @@ -70,8 +70,24 @@ void app_Render(App *app) { line((int)round(pos.x + 12), (int)round(pos.y), (int)round(pos.x - 12), (int)round(pos.y)); line((int)round(pos.x), (int)round(pos.y + 12), (int)round(pos.x), (int)round(pos.y - 12)); } + if (e->misc->hazard) { + setlinecolor(RGB(255, 0, 0)); + Box2 box; + if (e->position) + box = camera_TransformBox2(app->camera, box2_Offset(*e->misc->hazard, e->position->position)); + else + box = camera_TransformBox2(app->camera, *e->misc->hazard); + rectangle( + (int)round(box.lefttop.x), + (int)round(box.lefttop.y), + (int)round(box.lefttop.x + box.size.x), + (int)round(box.lefttop.y + box.size.y)); + } } } + setlinecolor(RGB(255, 0, 255)); + Vec2 respawn = camera_TransformVec2(app->camera, app->player->player->hazardRespawn); + circle((int)round(respawn.x), (int)round(respawn.y), 6); setfillcolor(RGB(255, 255, 255)); diff --git a/mapper_misc.c b/mapper_misc.c index 4508140..68d1364 100644 --- a/mapper_misc.c +++ b/mapper_misc.c @@ -3,6 +3,7 @@ #include "app.h" #include "entity.h" #include "physics.h" +#include "player.h" #include "types.h" #include "util/assert.h" #include @@ -48,6 +49,26 @@ void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime) { p->hazardRespawn = worldspawn; } +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); + return; + } + if (app->player->player == NULL) // No player + return; + Component_Player *p = app->player->player; + Box2 playerbox = physics_HitboxAbsolute(p->super->hitbox); + + Box2 worldbox; // Offset if position component exists + if (e->position) + worldbox = box2_Offset(*e->misc->hazard, e->position->position); + else + worldbox = *e->misc->hazard; + + if (box2_Intersects(worldbox, playerbox, NULL)) + player_HazardHarm(app->player); +} + 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); @@ -102,3 +123,11 @@ void misc_InstantiateHazardRespawn(App *app, Entity *e, Box2 trigger_box, Vec2 r e->thinker = &misc_thinker_HazardRespawn; } + +void misc_InstantiateHazard(App *app, Entity *e, Box2 trigger_box) { + ASSERT(e->misc == NULL && "Instantiate must be called with e.misc not set"); + e->misc = zero_malloc(sizeof(Component_Misc)); + e->misc->hazard = copy_malloc_size(&trigger_box, sizeof(Box2)); + + e->thinker = &misc_thinker_Hazard; +} diff --git a/mapper_misc.h b/mapper_misc.h index 1d566fa..80c424a 100644 --- a/mapper_misc.h +++ b/mapper_misc.h @@ -39,6 +39,11 @@ typedef struct { void misc_thinker_HazardRespawn(App *app, Entity *e, Duration deltaTime); +// Thinker for hazard areas +// Tracks the player & harms on contact +void misc_thinker_Hazard(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 typedef struct { @@ -59,6 +64,10 @@ void misc_InstantiateTextbox(App *app, Entity *e, const char *text, Box2 trigger // Creates misc & sets the thinker. void misc_InstantiateHazardRespawn(App *app, Entity *e, Box2 trigger_box, Vec2 respawn_pos); +// Inserts the components for a hazard harming area. +// Creates misc & sets the thinker. +void misc_InstantiateHazard(App *app, Entity *e, Box2 trigger_box); + static inline Box2 misc_TextboxUpright(double width, double height) { Box2 b = { diff --git a/types.h b/types.h index b7f7f13..13a43f1 100644 --- a/types.h +++ b/types.h @@ -30,6 +30,12 @@ static inline char *copy_malloc(const char *src) { return p; } +static inline void *copy_malloc_size(void *src, size_t size) { + void *p = malloc(size); + memcpy(p, src, size); + return p; +} + // A 2d vector of double. typedef struct {