Add building preview

This commit is contained in:
2023-11-16 09:40:41 +01:00
parent c232fc9afd
commit 6974a80611
2 changed files with 56 additions and 18 deletions

View File

@@ -6,6 +6,14 @@
#include "entity_map.h"
typedef enum InputState {
INPUT_NONE,
INPUT_PLACING,
INPUT_DRAGGING,
INPUT_SELECTED_UNITS,
INPUT_SELECTED_OBJECT,
} InputState;
typedef struct Game {
Camera2D camera;
BzTileset terrainTileset;
@@ -14,10 +22,13 @@ typedef struct Game {
BzTileMap map;
EntityMap entityMap;
f32 frameDuration;
Vector2 targetPos;
ecs_entity_t entity;
struct {
InputState state;
int building;
bool buildingCanPlace;
TilePosition buildingPos;
TileSize buildingSize;
Vector2 mouseDown;
f32 mouseDownElapsed;
} input;

View File

@@ -161,25 +161,34 @@ void update(float dt, void *userData) {
int tileX = (int) worldPos.x / map->tileWidth;
int tileY = (int) worldPos.y / map->tileHeight;
if (IsKeyPressed(KEY_ESCAPE) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
if (IsKeyPressed(KEY_ESCAPE) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
game->input.state = INPUT_NONE;
}
if (game->input.state == INPUT_NONE) {
game->input.building = 0;
if (game->input.building) {
BzTile sizeX = 0, sizeY = 0;
getBuildingSize(game->input.building, &sizeX, &sizeY);
}
bool canPlace = canPlaceBuilding(&game->map, game->input.building, tileX, tileY);
/*
Color placeColor = canPlace ?
(Color) {0, 255, 0, 200} :
(Color) {255, 0, 0, 200};
DrawRectangleLines(tileX * 16, tileY * 16, sizeX * 16, sizeY * 16, placeColor);
*/
if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
placeBuilding(&game->map, game->input.building, tileX, tileY);
}
return;
switch (game->input.state) {
case INPUT_NONE:
break;
case INPUT_PLACING:
BZ_ASSERT(game->input.building);
BzTile sizeX = 0, sizeY = 0;
getBuildingSize(game->input.building, &sizeX, &sizeY);
bool canPlace = canPlaceBuilding(&game->map, game->input.building, tileX, tileY);
if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
placeBuilding(&game->map, game->input.building, tileX, tileY);
}
game->input.buildingCanPlace = canPlace;
game->input.buildingPos = (TilePosition) {tileX, tileY};
game->input.buildingSize = (TileSize) {sizeX, sizeY};
break;
case INPUT_DRAGGING:
break;
case INPUT_SELECTED_UNITS:
break;
case INPUT_SELECTED_OBJECT:
break;
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
@@ -204,6 +213,19 @@ void render(float dt, void *userData) {
bzTileMapDraw(&game->map);
bzTileMapDrawColliders(&game->map);
if (game->input.building) {
Color placeColor = game->input.buildingCanPlace ?
(Color) {0, 255, 0, 200} :
(Color) {255, 0, 0, 200};
BzTile width = game->map.tileWidth;
BzTile height = game->map.tileHeight;
DrawRectangleLines(game->input.buildingPos.x * width,
game->input.buildingPos.y * height,
game->input.buildingSize.sizeX * width,
game->input.buildingSize.sizeY * height, placeColor);
}
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
int tileX = (int) worldPos.x / 16;
@@ -243,6 +265,9 @@ void imguiRender(float dt, void *userData) {
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
igBegin("Debug Menu", NULL, 0);
if (igCollapsingHeader_TreeNodeFlags("Selection", 0)) {
}
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {
igText("Wood: %lld", game->resources.wood);
igText("Iron: %lld", game->resources.iron);
@@ -255,6 +280,8 @@ void imguiRender(float dt, void *userData) {
if (igSelectable_Bool(getBuildingStr(i), game->input.building == i, 0, (ImVec2){0, 0}))
game->input.building = i;
}
if (game->input.building)
game->input.state = INPUT_PLACING;
}
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {