299 lines
9.8 KiB
C
299 lines
9.8 KiB
C
#include <rlImGui.h>
|
|
|
|
#include "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 "pathfinding.h"
|
|
|
|
ECS_COMPONENT_DECLARE(Game);
|
|
|
|
bool init(void *userData);
|
|
void deinit(void *userData);
|
|
|
|
void update(float dt, void *userData);
|
|
void render(float dt, void *userData);
|
|
void imguiRender(float dt, void *userData);
|
|
|
|
|
|
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;
|
|
|
|
appDesc->userData = NULL;
|
|
appDesc->useFlecs = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool init(void *userData) {
|
|
BZ_UNUSED(userData);
|
|
SetExitKey(-1);
|
|
ECS_COMPONENT_DEFINE(ECS, Game);
|
|
initComponentIDs(ECS);
|
|
|
|
// For ecs explorer
|
|
//ecs_singleton_set(ECS, EcsRest, {0});
|
|
//ECS_IMPORT(ECS, FlecsMonitor);
|
|
|
|
ecs_singleton_set(ECS, Game, {});
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
|
|
// init pools
|
|
game->pools.pathData = bzObjectPoolCreate(&(BzObjectPoolDesc) {
|
|
.objectSize=sizeof(PathData),
|
|
.numObjects=512
|
|
});
|
|
|
|
|
|
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"}
|
|
});
|
|
game->entityGrid = bzSpatialGridCreate(&(BzSpatialGridDesc) {
|
|
.maxWidth=game->map.width * game->map.tileWidth,
|
|
.maxHeight=game->map.height * game->map.tileHeight,
|
|
.cellWidth=game->map.tileWidth * 2,
|
|
.cellHeight=game->map.tileHeight * 2,
|
|
.userDataSize=sizeof(ecs_entity_t)
|
|
});
|
|
|
|
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(void *userData) {
|
|
BZ_UNUSED(userData);
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
|
|
bzTileMapDestroy(&game->map);
|
|
bzTilesetDestroy(&game->terrainTileset);
|
|
bzTilesetDestroy(&game->buildingsTileset);
|
|
bzTilesetDestroy(&game->entitiesTileset);
|
|
|
|
|
|
bzObjectPoolDestroy(game->pools.pathData);
|
|
bzSpatialGridDestroy(game->entityGrid);
|
|
|
|
ecs_singleton_remove(ECS, Game);
|
|
}
|
|
|
|
|
|
void update(float dt, void *userData) {
|
|
BZ_UNUSED(userData);
|
|
Game *game = ecs_singleton_get_mut(ECS, 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.state = INPUT_NONE;
|
|
}
|
|
if (game->input.state == INPUT_NONE) {
|
|
game->input.building = 0;
|
|
}
|
|
|
|
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)) {
|
|
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, void *userData) {
|
|
BZ_UNUSED(userData);
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
|
|
ClearBackground(RAYWHITE);
|
|
BeginMode2D(game->camera);
|
|
|
|
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);
|
|
}
|
|
|
|
bzSpatialGridDrawDebugGrid(game->entityGrid);
|
|
|
|
|
|
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 = bzHeapCreate(PathNode, game->map.width * game->map.height);
|
|
if (!ecs_has(ECS, game->entity, Path) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
|
Path path = {};
|
|
const Position *start = ecs_get(ECS, game->entity, Position);
|
|
findPath(&(PathfindingDesc) {
|
|
.start=(TilePosition) {
|
|
(int) (start->x / game->map.tileWidth),
|
|
(int) (start->y / game->map.tileHeight)
|
|
},
|
|
.target=(TilePosition) {tileX, tileY},
|
|
.map=&game->map,
|
|
.heap=heap,
|
|
.outPath=&path,
|
|
.pool=game->pools.pathData
|
|
});
|
|
if (path.paths) {
|
|
ecs_set_ptr(ECS, game->entity, Path, &path);
|
|
}
|
|
}
|
|
|
|
ecs_progress(ECS, dt);
|
|
|
|
EndMode2D();
|
|
}
|
|
|
|
void imguiRender(float dt, void *userData) {
|
|
BZ_UNUSED(userData);
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
|
|
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);
|
|
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 (game->input.building)
|
|
game->input.state = INPUT_PLACING;
|
|
|
|
}
|
|
if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) {
|
|
igSliderFloat("Frame duration", &game->frameDuration, 0.0f, 1.0f, NULL, 0);
|
|
}
|
|
igEnd();
|
|
igShowDemoWindow(NULL);
|
|
}
|
|
|