Add path debug drawing, resources fields
This commit is contained in:
@@ -11,6 +11,7 @@ add_subdirectory(engine/)
|
||||
add_executable(PixelDefense
|
||||
game/systems/entity_systems.c
|
||||
game/systems/systems.h
|
||||
game/systems/ui_systems.c
|
||||
|
||||
game/utils/building_types.h
|
||||
game/utils/pathfinding.c
|
||||
|
||||
@@ -10,13 +10,25 @@ typedef struct Game {
|
||||
BzTileset buildingsTileset;
|
||||
BzTileset entitiesTileset;
|
||||
BzTileMap map;
|
||||
int selectedBuilding;
|
||||
ecs_entity_t *entityMap;
|
||||
f32 frameDuration;
|
||||
Vector2 targetPos;
|
||||
ecs_entity_t entity;
|
||||
Path path;
|
||||
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;
|
||||
|
||||
extern Game *GAME;
|
||||
|
||||
47
game/main.c
47
game/main.c
@@ -40,6 +40,7 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
|
||||
}
|
||||
|
||||
bool init(Game *game) {
|
||||
SetExitKey(-1);
|
||||
initComponentIDs(ECS);
|
||||
|
||||
int screenWidth = 1280;
|
||||
@@ -87,9 +88,11 @@ bool init(Game *game) {
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
|
||||
|
||||
ECS_SYSTEM(ECS, uiTask, EcsOnUpdate, 0);
|
||||
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, drawDebugPath, EcsOnUpdate, Path);
|
||||
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion);
|
||||
ECS_OBSERVER(ECS, targetFinish, EcsOnRemove, TargetPosition);
|
||||
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
|
||||
return true;
|
||||
@@ -122,17 +125,21 @@ void update(float dt, Game *game) {
|
||||
if (IsKeyDown(KEY_Q)) game->camera.rotation--;
|
||||
if (IsKeyDown(KEY_E)) game->camera.rotation++;
|
||||
|
||||
|
||||
game->camera.zoom += ((float) GetMouseWheelMove() * 0.05f);
|
||||
BzTileMap *map = &game->map;
|
||||
|
||||
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
||||
int tileX = (int) worldPos.x / 16;
|
||||
int tileY = (int) worldPos.y / 16;
|
||||
int tileX = (int) worldPos.x / map->tileWidth;
|
||||
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;
|
||||
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) {0, 255, 0, 200} :
|
||||
@@ -142,9 +149,20 @@ void update(float dt, Game *game) {
|
||||
*/
|
||||
|
||||
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,
|
||||
.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)) {
|
||||
ecs_entity_t e = game->entity;
|
||||
bzLogInfo("%d", ecs_is_alive(ECS, e));
|
||||
@@ -193,10 +207,17 @@ void render(float dt, Game *game) {
|
||||
void imguiRender(float dt, Game *game) {
|
||||
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
||||
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)) {
|
||||
for (int i = 0; i < BUILDINGS_COUNT; i++) {
|
||||
if (igSelectable_Bool(getBuildingStr(i), game->selectedBuilding == i, 0, (ImVec2){0,0}))
|
||||
game->selectedBuilding = i;
|
||||
if (igSelectable_Bool(getBuildingStr(i), game->input.building == i, 0, (ImVec2){0, 0}))
|
||||
game->input.building = i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -93,3 +93,20 @@ void startPath(ecs_iter_t *it) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,5 +10,8 @@ void updateAnimations(ecs_iter_t *it);
|
||||
void updatePos(ecs_iter_t *it);
|
||||
void targetFinish(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
|
||||
|
||||
14
game/systems/ui_systems.c
Normal file
14
game/systems/ui_systems.c
Normal 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;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user