Hazard areas are working

This commit is contained in:
Edgaru089 2024-04-02 08:56:35 +08:00
parent 20e0add5b4
commit 6bae280a0c
5 changed files with 64 additions and 0 deletions

4
app.c
View File

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

View File

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

View File

@ -3,6 +3,7 @@
#include "app.h"
#include "entity.h"
#include "physics.h"
#include "player.h"
#include "types.h"
#include "util/assert.h"
#include <stdio.h>
@ -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;
}

View File

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

View File

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