Add building preview
This commit is contained in:
@@ -6,6 +6,14 @@
|
|||||||
|
|
||||||
#include "entity_map.h"
|
#include "entity_map.h"
|
||||||
|
|
||||||
|
typedef enum InputState {
|
||||||
|
INPUT_NONE,
|
||||||
|
INPUT_PLACING,
|
||||||
|
INPUT_DRAGGING,
|
||||||
|
INPUT_SELECTED_UNITS,
|
||||||
|
INPUT_SELECTED_OBJECT,
|
||||||
|
} InputState;
|
||||||
|
|
||||||
typedef struct Game {
|
typedef struct Game {
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
BzTileset terrainTileset;
|
BzTileset terrainTileset;
|
||||||
@@ -14,10 +22,13 @@ typedef struct Game {
|
|||||||
BzTileMap map;
|
BzTileMap map;
|
||||||
EntityMap entityMap;
|
EntityMap entityMap;
|
||||||
f32 frameDuration;
|
f32 frameDuration;
|
||||||
Vector2 targetPos;
|
|
||||||
ecs_entity_t entity;
|
ecs_entity_t entity;
|
||||||
struct {
|
struct {
|
||||||
|
InputState state;
|
||||||
int building;
|
int building;
|
||||||
|
bool buildingCanPlace;
|
||||||
|
TilePosition buildingPos;
|
||||||
|
TileSize buildingSize;
|
||||||
Vector2 mouseDown;
|
Vector2 mouseDown;
|
||||||
f32 mouseDownElapsed;
|
f32 mouseDownElapsed;
|
||||||
} input;
|
} input;
|
||||||
|
|||||||
51
game/main.c
51
game/main.c
@@ -161,25 +161,34 @@ void update(float dt, void *userData) {
|
|||||||
int tileX = (int) worldPos.x / map->tileWidth;
|
int tileX = (int) worldPos.x / map->tileWidth;
|
||||||
int tileY = (int) worldPos.y / map->tileHeight;
|
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;
|
game->input.building = 0;
|
||||||
if (game->input.building) {
|
}
|
||||||
|
|
||||||
|
switch (game->input.state) {
|
||||||
|
case INPUT_NONE:
|
||||||
|
break;
|
||||||
|
case INPUT_PLACING:
|
||||||
|
BZ_ASSERT(game->input.building);
|
||||||
BzTile sizeX = 0, sizeY = 0;
|
BzTile sizeX = 0, sizeY = 0;
|
||||||
getBuildingSize(game->input.building, &sizeX, &sizeY);
|
getBuildingSize(game->input.building, &sizeX, &sizeY);
|
||||||
|
|
||||||
bool canPlace = canPlaceBuilding(&game->map, game->input.building, tileX, tileY);
|
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)) {
|
if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||||
placeBuilding(&game->map, game->input.building, tileX, tileY);
|
placeBuilding(&game->map, game->input.building, tileX, tileY);
|
||||||
}
|
}
|
||||||
return;
|
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)) {
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||||
@@ -204,6 +213,19 @@ void render(float dt, void *userData) {
|
|||||||
bzTileMapDraw(&game->map);
|
bzTileMapDraw(&game->map);
|
||||||
bzTileMapDrawColliders(&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);
|
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
||||||
int tileX = (int) worldPos.x / 16;
|
int tileX = (int) worldPos.x / 16;
|
||||||
@@ -243,6 +265,9 @@ void imguiRender(float dt, void *userData) {
|
|||||||
|
|
||||||
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
||||||
igBegin("Debug Menu", NULL, 0);
|
igBegin("Debug Menu", NULL, 0);
|
||||||
|
if (igCollapsingHeader_TreeNodeFlags("Selection", 0)) {
|
||||||
|
|
||||||
|
}
|
||||||
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {
|
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {
|
||||||
igText("Wood: %lld", game->resources.wood);
|
igText("Wood: %lld", game->resources.wood);
|
||||||
igText("Iron: %lld", game->resources.iron);
|
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}))
|
if (igSelectable_Bool(getBuildingStr(i), game->input.building == i, 0, (ImVec2){0, 0}))
|
||||||
game->input.building = i;
|
game->input.building = i;
|
||||||
}
|
}
|
||||||
|
if (game->input.building)
|
||||||
|
game->input.state = INPUT_PLACING;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {
|
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user