Add path debug drawing, resources fields

This commit is contained in:
2023-11-14 16:21:14 +01:00
parent 8825b9e01f
commit 274612b035
6 changed files with 82 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ add_subdirectory(engine/)
add_executable(PixelDefense add_executable(PixelDefense
game/systems/entity_systems.c game/systems/entity_systems.c
game/systems/systems.h game/systems/systems.h
game/systems/ui_systems.c
game/utils/building_types.h game/utils/building_types.h
game/utils/pathfinding.c game/utils/pathfinding.c

View File

@@ -10,13 +10,25 @@ typedef struct Game {
BzTileset buildingsTileset; BzTileset buildingsTileset;
BzTileset entitiesTileset; BzTileset entitiesTileset;
BzTileMap map; BzTileMap map;
int selectedBuilding;
ecs_entity_t *entityMap; ecs_entity_t *entityMap;
f32 frameDuration; f32 frameDuration;
Vector2 targetPos; Vector2 targetPos;
ecs_entity_t entity; ecs_entity_t entity;
Path path; Path path;
Position waypoints[128]; Position waypoints[128];
struct {
int building;
Vector2 mouseDown;
f32 mouseDownElapsed;
} input;
struct {
i64 wood;
i64 iron;
i64 food;
i64 gold;
i64 pop;
} resources;
f32 elapsed;
} Game; } Game;
extern Game *GAME; extern Game *GAME;

View File

@@ -40,6 +40,7 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
} }
bool init(Game *game) { bool init(Game *game) {
SetExitKey(-1);
initComponentIDs(ECS); initComponentIDs(ECS);
int screenWidth = 1280; int screenWidth = 1280;
@@ -87,9 +88,11 @@ bool init(Game *game) {
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer); bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer); bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
ECS_SYSTEM(ECS, uiTask, EcsOnUpdate, 0);
ECS_SYSTEM(ECS, updateAnimations, EcsOnUpdate, Animation, TextureRegion); ECS_SYSTEM(ECS, updateAnimations, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion);
ECS_SYSTEM(ECS, updatePos, EcsOnUpdate, Position, TargetPosition, TextureRegion); ECS_SYSTEM(ECS, updatePos, EcsOnUpdate, Position, TargetPosition, TextureRegion);
ECS_SYSTEM(ECS, drawDebugPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion);
ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition); ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition);
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path); ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
return true; return true;
@@ -122,17 +125,21 @@ void update(float dt, Game *game) {
if (IsKeyDown(KEY_Q)) game->camera.rotation--; if (IsKeyDown(KEY_Q)) game->camera.rotation--;
if (IsKeyDown(KEY_E)) game->camera.rotation++; if (IsKeyDown(KEY_E)) game->camera.rotation++;
game->camera.zoom += ((float) GetMouseWheelMove() * 0.05f); game->camera.zoom += ((float) GetMouseWheelMove() * 0.05f);
BzTileMap *map = &game->map;
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera); Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
int tileX = (int) worldPos.x / 16; int tileX = (int) worldPos.x / map->tileWidth;
int tileY = (int) worldPos.y / 16; int tileY = (int) worldPos.y / map->tileHeight;
if (game->selectedBuilding) { if (IsKeyPressed(KEY_ESCAPE) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
game->input.building = 0;
if (game->input.building) {
BzTile sizeX = 0, sizeY = 0; BzTile sizeX = 0, sizeY = 0;
getBuildingSize(game->selectedBuilding, &sizeX, &sizeY); getBuildingSize(game->input.building, &sizeX, &sizeY);
bool canPlace = canPlaceBuilding(&game->map, game->selectedBuilding, tileX, tileY); bool canPlace = canPlaceBuilding(&game->map, game->input.building, tileX, tileY);
/* /*
Color placeColor = canPlace ? Color placeColor = canPlace ?
(Color) {0, 255, 0, 200} : (Color) {0, 255, 0, 200} :
@@ -142,9 +149,20 @@ void update(float dt, Game *game) {
*/ */
if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
placeBuilding(&game->map, game->selectedBuilding, tileX, tileY); placeBuilding(&game->map, game->input.building, tileX, tileY);
} }
return;
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
game->input.mouseDown = GetMousePosition();
game->input.mouseDownElapsed = 0;
bzLogInfo("Pressed");
} else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
game->input.mouseDownElapsed += dt;
bzLogInfo("Down: %.2f", game->input.mouseDownElapsed);
} else if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
bzLogInfo("Released");
} }
} }
@@ -173,10 +191,6 @@ void render(float dt, Game *game) {
.heap=heap, .heap=heap,
.outPath=&game->path .outPath=&game->path
}); });
for (i32 i = 0; i < game->path.numWaypoints; i++) {
Position pos = game->path.waypoints[i];
DrawCircle(pos.x, pos.y, 3.0f, RED);
}
if (game->path.numWaypoints > 0 && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { if (game->path.numWaypoints > 0 && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
ecs_entity_t e = game->entity; ecs_entity_t e = game->entity;
bzLogInfo("%d", ecs_is_alive(ECS, e)); bzLogInfo("%d", ecs_is_alive(ECS, e));
@@ -193,10 +207,17 @@ void render(float dt, Game *game) {
void imguiRender(float dt, Game *game) { void imguiRender(float dt, Game *game) {
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("Resources", 0)) {
igText("Wood: %lld", game->resources.wood);
igText("Iron: %lld", game->resources.iron);
igText("Food: %lld", game->resources.food);
igText("Gold: %lld", game->resources.gold);
igText("Population: %lld", game->resources.pop);
}
if (igCollapsingHeader_TreeNodeFlags("BuildMenu", 0)) { if (igCollapsingHeader_TreeNodeFlags("BuildMenu", 0)) {
for (int i = 0; i < BUILDINGS_COUNT; i++) { for (int i = 0; i < BUILDINGS_COUNT; i++) {
if (igSelectable_Bool(getBuildingStr(i), game->selectedBuilding == i, 0, (ImVec2){0,0})) if (igSelectable_Bool(getBuildingStr(i), game->input.building == i, 0, (ImVec2){0, 0}))
game->selectedBuilding = i; game->input.building = i;
} }
} }

View File

@@ -93,3 +93,20 @@ void startPath(ecs_iter_t *it) {
path[i].curWaypoint++; path[i].curWaypoint++;
} }
} }
void drawDebugPath(ecs_iter_t *it) {
Path *path = ecs_field(it, Path, 1);
for (i32 i = 0; i < it->count; i++) {
for (i32 iPath = 0; iPath < path[i].numWaypoints; iPath++) {
Color color = RED;
if (iPath < path[i].curWaypoint - 1)
color = GREEN;
else if (iPath == path[i].curWaypoint - 1)
color = ORANGE;
color.a = 180;
Position pos = path[i].waypoints[iPath];
DrawCircle(pos.x, pos.y, 3, color);
}
}
}

View File

@@ -10,5 +10,8 @@ void updateAnimations(ecs_iter_t *it);
void updatePos(ecs_iter_t *it); void updatePos(ecs_iter_t *it);
void targetFinish(ecs_iter_t *it); void targetFinish(ecs_iter_t *it);
void startPath(ecs_iter_t *it); void startPath(ecs_iter_t *it);
void drawDebugPath(ecs_iter_t *it);
void uiTask(ecs_iter_t *it);
#endif //PIXELDEFENSE_SYSTEMS_H #endif //PIXELDEFENSE_SYSTEMS_H

14
game/systems/ui_systems.c Normal file
View File

@@ -0,0 +1,14 @@
#include "systems.h"
#include "../game_state.h"
void uiTask(ecs_iter_t *it) {
Vector2 mousePos = GetMousePosition();
Vector2 worldPos = GetScreenToWorld2D(mousePos, GAME->camera);
BzTileMap *map = &GAME->map;
i32 tileX = (i32) worldPos.x / map->tileWidth;
i32 tileY = (i32) worldPos.y / map->tileHeight;
}