feat: made hitboxes area dependent

This commit is contained in:
2025-10-06 21:57:06 +01:00
parent c01a6be4e5
commit f18d9d2b0e
4 changed files with 26 additions and 20 deletions

View File

@@ -219,7 +219,7 @@ int main(int argc, char **argv)
world->propTypes[21].scale=1; world->propTypes[21].scale=1;
world->propCount = 0; world->propCount = 0;
world->props = M_ArenaPush(arena, World_Prop, .count=WORLD_PROP_MAX); world->props = M_ArenaPush(arena, World_Prop, .count=WORLD_PROP_MAX);
world->hitboxes = M_ArenaPush(arena, AABB, .count=WORLD_HITBOX_MAX); world->hitboxes = M_ArenaPush(arena, World_Hitbox, .count=WORLD_HITBOX_MAX);
world->portals = M_ArenaPush(arena, World_Portal, .count=64); world->portals = M_ArenaPush(arena, World_Portal, .count=64);
world->portalCount=0; world->portalCount=0;
GenerateNavMesh(arena, world); GenerateNavMesh(arena, world);
@@ -285,11 +285,12 @@ int main(int argc, char **argv)
}} else if(e.type==SDL_EVENT_MOUSE_BUTTON_UP && e.button.button == SDL_BUTTON_LEFT && game->editor.mode == G_EDITOR_MODE_HITBOX) { }} else if(e.type==SDL_EVENT_MOUSE_BUTTON_UP && e.button.button == SDL_BUTTON_LEFT && game->editor.mode == G_EDITOR_MODE_HITBOX) {
// Add hitbox // Add hitbox
V2f topLeft = V2F(Min(game->editor.cursor.x, game->editor.dragStart.x), Min(game->editor.cursor.y, game->editor.dragStart.y)); V2f topLeft = V2F(Min(game->editor.cursor.x, game->editor.dragStart.x), Min(game->editor.cursor.y, game->editor.dragStart.y));
game->world->hitboxes[game->world->hitboxCount].pos = topLeft; game->world->hitboxes[game->world->hitboxCount].box.pos = topLeft;
game->world->hitboxes[game->world->hitboxCount].size = V2F( game->world->hitboxes[game->world->hitboxCount].box.size = V2F(
Abs(game->editor.cursor.x-game->editor.dragStart.x), Abs(game->editor.cursor.x-game->editor.dragStart.x),
Abs(game->editor.cursor.y-game->editor.dragStart.y) Abs(game->editor.cursor.y-game->editor.dragStart.y)
); );
game->world->hitboxes[game->world->hitboxCount].area = game->editor.currentAsset;
game->world->hitboxCount++; game->world->hitboxCount++;
GenerateNavMesh(game->arena, game->world); GenerateNavMesh(game->arena, game->world);
} else if(e.type==SDL_EVENT_MOUSE_BUTTON_UP && e.button.button == SDL_BUTTON_LEFT && game->editor.mode == G_EDITOR_MODE_PORTAL) { } else if(e.type==SDL_EVENT_MOUSE_BUTTON_UP && e.button.button == SDL_BUTTON_LEFT && game->editor.mode == G_EDITOR_MODE_PORTAL) {
@@ -367,6 +368,7 @@ int main(int argc, char **argv)
switch(game->editor.mode) { switch(game->editor.mode) {
case G_EDITOR_MODE_PROP: {game->world->propCount--;} case G_EDITOR_MODE_PROP: {game->world->propCount--;}
case G_EDITOR_MODE_HITBOX: {game->world->hitboxCount--;} case G_EDITOR_MODE_HITBOX: {game->world->hitboxCount--;}
case G_EDITOR_MODE_PORTAL: {game->world->portalCount--;}
} }
break; break;
} }
@@ -471,16 +473,18 @@ int main(int argc, char **argv)
} }
case G_EDITOR_MODE_HITBOX: { case G_EDITOR_MODE_HITBOX: {
for(U32 i = 0; i < game->world->hitboxCount; i++) { for(U32 i = 0; i < game->world->hitboxCount; i++) {
V2f centre = AABB_Centre(game->world->hitboxes[i]); if(game->world->player.currentArea == game->world->hitboxes[i].area) {
D_Rect( V2f centre = AABB_Centre(game->world->hitboxes[i].box);
&game->draw, D_Rect(
centre.x, &game->draw,
centre.y, centre.x,
.texture=0, centre.y,
.dim=game->world->hitboxes[i].size, .texture=0,
.flags=D_RECT_IGNORE_ASPECT, .dim=game->world->hitboxes[i].box.size,
.c=V4F(100,0,0,0.7f), .flags=D_RECT_IGNORE_ASPECT,
); .c=V4F(100,0,0,0.7f),
);
}
} }
break; break;
} }

View File

@@ -189,14 +189,14 @@ void LoadWorld(M_Arena *arena, World *world) {
// //
{ {
U32 n_hitbox = *(U32 *) base; base += 4; U32 n_hitbox = *(U32 *) base; base += 4;
AABB *boxes = (AABB *) base; World_Hitbox *boxes = (World_Hitbox *) base;
world->hitboxCount = n_hitbox; world->hitboxCount = n_hitbox;
world->hitboxes = M_ArenaPush(arena, AABB, .count = WORLD_HITBOX_MAX); world->hitboxes = M_ArenaPush(arena, World_Hitbox, .count = WORLD_HITBOX_MAX);
M_CopySize(world->hitboxes, boxes, n_hitbox * sizeof(AABB)); M_CopySize(world->hitboxes, boxes, n_hitbox * sizeof(World_Hitbox));
base += (WORLD_HITBOX_MAX * sizeof(AABB)); base += (WORLD_HITBOX_MAX * sizeof(World_Hitbox));
} }
// Map // Map
@@ -242,7 +242,7 @@ void GenerateNavMesh(M_Arena *arena, World *world) {
U32 y = (i / 96); U32 y = (i / 96);
bool skip = false; bool skip = false;
for(U32 hi = 0; hi < world->hitboxCount; hi++) { for(U32 hi = 0; hi < world->hitboxCount; hi++) {
if(AABB_Point(world->hitboxes[hi], V2F((F32) x, (F32) y))) { if(AABB_Point(world->hitboxes[hi].box, V2F((F32) x, (F32) y))) {
skip = true; skip = true;
break; break;
} }
@@ -262,7 +262,7 @@ void GenerateNavMesh(M_Arena *arena, World *world) {
} }
// It's quad for loop time :D // It's quad for loop time :D
for(U32 hi = 0; hi < world->hitboxCount; hi++) { for(U32 hi = 0; hi < world->hitboxCount; hi++) {
if(AABB_Point(world->hitboxes[hi], V2F((F32) (x + nx), (F32) (y + ny)))) { if(AABB_Point(world->hitboxes[hi].box, V2F((F32) (x + nx), (F32) (y + ny)))) {
skip = true; skip = true;
break; break;
} }

View File

@@ -45,6 +45,8 @@ struct World_Portal
AABB box; AABB box;
World_Area area; World_Area area;
}; };
typedef World_Portal World_Hitbox;
typedef struct World World; typedef struct World World;
#include "player.h" #include "player.h"
@@ -62,7 +64,7 @@ struct World {
World_Tile *tileTypes; World_Tile *tileTypes;
World_PropType *propTypes; World_PropType *propTypes;
World_Prop *props; World_Prop *props;
AABB *hitboxes; World_Hitbox *hitboxes;
World_Portal *portals; World_Portal *portals;
U32 *map; U32 *map;

Binary file not shown.