#include #include "systems/systems.h" #include "utils/building_types.h" #include "components.h" #include "game_state.h" #include "map_init.h" #include "map_layers.h" #include "buildings.h" #include "utils/pathfinding.h" Game *GAME = NULL; bool init(Game *game); void deinit(Game *game); void update(float dt, Game *game); void render(float dt, Game *game); void imguiRender(float dt, Game *game); bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) { appDesc->width = 1280; appDesc->height = 720; appDesc->title = "PixelDefense"; appDesc->fps = 60; appDesc->init = (BzAppInitFunc) init; appDesc->deinit = (BzAppDeinitFunc) deinit; appDesc->update = (BzAppUpdateFunc) update; appDesc->render = (BzAppRenderFunc) render; appDesc->imguiRender = (BzAppRenderFunc) imguiRender; GAME = bzAlloc(sizeof(*GAME)); appDesc->userData = GAME; appDesc->useFlecs = true; return true; } bool init(Game *game) { SetExitKey(-1); initComponentIDs(ECS); int screenWidth = 1280; int screenHeight = 720; game->camera = (Camera2D){ 0 }; game->camera.target = (Vector2) {0, 0}; game->camera.offset = (Vector2) {screenWidth / 2.0f, screenHeight / 2.0f}; game->camera.rotation = 0.0f; game->camera.zoom = 1.0f; game->frameDuration = 0.16f; game->terrainTileset = bzTilesetCreate( &(BzTilesetDesc) { .path="assets/terrain.tsj", .texturePath="assets/terrain.png" }); game->buildingsTileset = bzTilesetCreate(&(BzTilesetDesc) { .path="assets/buildings.tsj", .texturePath="assets/buildings.png" }); game->entitiesTileset = bzTilesetCreate(&(BzTilesetDesc) { .path="assets/entities.tsj", .texturePath="assets/entities.png" }); game->map = bzTileMapCreate(&(BzTileMapDesc) { .path="assets/maps/test.tmj", .tilesets[0]=game->terrainTileset, .tilesets[1]=game->buildingsTileset, .tilesets[2]=game->entitiesTileset, .layers[LAYER_TERRAIN]=(BzTileLayerDesc) {"Terrain"}, .layers[LAYER_FOLIAGE]=(BzTileLayerDesc) {"Foliage"}, .layers[LAYER_TREES]=(BzTileLayerDesc) {"Trees"}, .layers[LAYER_TREES2]=(BzTileLayerDesc) {"TreesS"}, .layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"Buildings"}, .layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"BuildingOwnership", BZ_TILE_LAYER_SKIP_RENDER}, .objectGroups[OBJECTS_GAME]=(BzTileObjectsDesc) {"Game"}, .objectGroups[OBJECTS_ENTITIES]=(BzTileObjectsDesc ) {"Entities"} }); bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer); 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, 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; } void deinit(Game *game) { bzTileMapDestroy(&game->map); bzTilesetDestroy(&game->terrainTileset); bzTilesetDestroy(&game->buildingsTileset); bzTilesetDestroy(&game->entitiesTileset); bzFree(GAME); GAME = NULL; } void update(float dt, Game *game) { char titleBuf[32]; snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000); SetWindowTitle(titleBuf); ImGuiIO *io = igGetIO(); if (io->WantCaptureMouse || io->WantCaptureKeyboard) return; if (IsKeyDown(KEY_W)) game->camera.target.y -= 20; if (IsKeyDown(KEY_S)) game->camera.target.y += 20; if (IsKeyDown(KEY_A)) game->camera.target.x -= 20; if (IsKeyDown(KEY_D)) game->camera.target.x += 20; 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 / map->tileWidth; int tileY = (int) worldPos.y / map->tileHeight; if (IsKeyPressed(KEY_ESCAPE) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) 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; } 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"); } } void render(float dt, Game *game) { ClearBackground(RAYWHITE); BeginMode2D(game->camera); bzTileMapDraw(&game->map); bzTileMapDrawColliders(&game->map); Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera); int tileX = (int) worldPos.x / 16; int tileY = (int) worldPos.y / 16; static PathNode *heap = NULL; if (!heap) heap = bzHeapNew(PathNode, game->map.width * game->map.height); game->path.waypoints = game->waypoints; game->path.maxWaypoints = 128; findPath(&(PathfindingDesc) { .start=(TilePosition){57, 24}, .target=(TilePosition){tileX, tileY}, .map=&game->map, .heap=heap, .outPath=&game->path }); if (game->path.numWaypoints > 0 && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { ecs_entity_t e = game->entity; bzLogInfo("%d", ecs_is_alive(ECS, e)); Position pos = worldPos; //ecs_set_ptr(ECS, e, Position, &pos); ecs_set_ptr(ECS, e, Path, &game->path); } ecs_progress(ECS, dt); EndMode2D(); } 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->input.building == i, 0, (ImVec2){0, 0})) game->input.building = i; } } if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) { igSliderFloat("Frame duration", &game->frameDuration, 0.0f, 1.0f, NULL, 0); } igEnd(); igShowDemoWindow(NULL); }