Buildings as entities instead of map layer

This commit is contained in:
2024-01-08 14:41:26 +01:00
parent 03dc3774e7
commit c08fca9670
17 changed files with 168 additions and 186 deletions

View File

@@ -11,9 +11,6 @@
#include <raymath.h>
#include <rlgl.h>
Rectangle calculateEntityBounds(Position pos, Size size);
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds);
ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag);
bool selectEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag);
@@ -29,7 +26,7 @@ void placeUnits(i32 numUnits, f32 unitSpacing, Vector2 start, Vector2 end, BzTil
void resetInputState(InputState *input) {
input->cursor = CURSOR_NONE;
input->state = INPUT_NONE;
input->building = 0;
input->building = BUILDING_NONE;
}
void inputPrimaryAction(Game *game, InputState *input) {
@@ -218,8 +215,9 @@ void updatePlayerInput() {
BzTileMap *map = &game->map;
BzTile tileX = 0, tileY = 0;
bzTileMapPosToTile(map, input->mouseWorld, &tileX, &tileY);
Vec2i tileXY = bzTileMapPosToTile(map, input->mouseWorld);
BzTile tileX = tileXY.x;
BzTile tileY = tileXY.y;
const MouseButton primaryBtn = input->mapping.primaryBtn;
const MouseButton secondaryBtn = input->mapping.secondaryBtn;
@@ -230,16 +228,19 @@ void updatePlayerInput() {
break;
}
case INPUT_BUILDING: {
BZ_ASSERT(input->building);
if (input->building <= BUILDING_NONE || input->building >= BUILDING_COUNT) {
input->state = INPUT_NONE;
return;
}
BzTile sizeX = 0, sizeY = 0;
getBuildingSize(input->building, &sizeX, &sizeY);
bool canPlace = canPlaceBuilding(&game->map, input->building, tileX, tileY);
bool canPlace = canPlaceBuilding(game, input->building, tileX, tileY);
if (canPlace && isInputBtnDown(input, primaryBtn)) {
placeBuilding(&game->map, input->building, tileX, tileY);
placeBuilding(game, input->building, tileX, tileY, (Owner) {-1});
}
input->buildingCanPlace = canPlace;
input->buildingPos = (TilePosition) {tileX, tileY};
input->buildingSize = (TileSize) {sizeX, sizeY};
input->buildingPos = (Vec2i) {tileX, tileY};
input->buildingSize = (Vec2i) {sizeX, sizeY};
break;
}
case INPUT_SELECTED_UNITS: {
@@ -278,8 +279,8 @@ void drawPlayerInputUIGround() {
const BzTile height = game->map.tileHeight;
DrawRectangleLines(input->buildingPos.x * width,
input->buildingPos.y * height,
input->buildingSize.sizeX * width,
input->buildingSize.sizeY * height, placeColor);
input->buildingSize.x * width,
input->buildingSize.y * height, placeColor);
break;
}
default: break;
@@ -323,41 +324,6 @@ void drawPlayerInputUI() {
}
Rectangle calculateEntityBounds(Position pos, Size size) {
return (Rectangle) {
pos.x - size.x * 0.5f,
pos.y - size.x * 0.5f,
size.x, size.y
};
}
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds) {
if (!ecs_is_alive(ECS, entity))
return false;
const Position *pos = ecs_get(ECS, entity, Position);
if (!pos)
return false;
const Size *size = ecs_get(ECS, entity, Size);
if (!size)
return false;
if (outPos) {
*outPos = *pos;
}
if (outSize) {
*outSize = *size;
}
if (outBounds) {
*outBounds = (Rectangle) {
pos->x - size->x * 0.5f,
pos->y - size->y * 0.5f,
size->x, size->y
};
}
return true;
}
ecs_entity_t queryEntity(BzSpatialGrid *entityGrid, Vector2 point, ecs_entity_t tag) {
BzSpatialGridIter it = bzSpatialGridIter(entityGrid, point.x, point.y, 0.0f, 0.0f);
f32 closestDst = INFINITY;

View File

@@ -2,6 +2,40 @@
#include "../game_state.h"
Rectangle calculateEntityBounds(Position pos, Size size) {
return (Rectangle) {
pos.x - size.x * 0.5f,
pos.y - size.x * 0.5f,
size.x, size.y
};
}
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds) {
if (!ecs_is_alive(ECS, entity))
return false;
const Position *pos = ecs_get(ECS, entity, Position);
if (!pos)
return false;
const Size *size = ecs_get(ECS, entity, Size);
if (!size)
return false;
if (outPos) {
*outPos = *pos;
}
if (outSize) {
*outSize = *size;
}
if (outBounds) {
*outBounds = (Rectangle) {
pos->x - size->x * 0.5f,
pos->y - size->y * 0.5f,
size->x, size->y
};
}
return true;
}
ecs_entity_t renderCollidersSystem;
ecs_entity_t renderOrientDirSystem;
ecs_entity_t renderArmPositionSystem;
@@ -37,6 +71,7 @@ ECS_DTOR(Arms, arms, {
ecs_delete(ECS, arms->secondary);
arms->secondary = 0;
}
})
ECS_MOVE(Arms, dst, src, {
*dst = *src;

View File

@@ -171,6 +171,13 @@ void drawPlayerInputUI();
* UI systems
**********************************/
/**********************************
* Utils
**********************************/
Rectangle calculateEntityBounds(Position pos, Size size);
bool getEntityBounds(ecs_entity_t entity, Position *outPos, Size *outSize, Rectangle *outBounds);
/**********************************
* MISC
**********************************/