Add Game as singleton

This commit is contained in:
2023-11-15 08:18:56 +01:00
parent 274612b035
commit d37936ce7f
6 changed files with 55 additions and 27 deletions

View File

@@ -70,6 +70,7 @@ bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile ti
}
ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile tileY) {
Game *game = ecs_singleton_get_mut(ECS, Game);
i32 sizeX, sizeY;
getBuildingSize(type, &sizeX, &sizeY);
ECS_COMPONENT(ECS, TilePosition);
@@ -94,7 +95,7 @@ ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTi
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
buildingTile++;
GAME->entityMap[y * buildingLayer->width + x] = e;
game->entityMap[y * buildingLayer->width + x] = e;
bzTileMapUpdateCollider(map, x, y);
}

View File

@@ -31,6 +31,6 @@ typedef struct Game {
f32 elapsed;
} Game;
extern Game *GAME;
extern ECS_COMPONENT_DECLARE(Game);
#endif //PIXELDEFENSE_GAME_STATE_H

View File

@@ -10,14 +10,14 @@
#include "utils/pathfinding.h"
Game *GAME = NULL;
ECS_COMPONENT_DECLARE(Game);
bool init(Game *game);
void deinit(Game *game);
bool init(void *userData);
void deinit(void *userData);
void update(float dt, Game *game);
void render(float dt, Game *game);
void imguiRender(float dt, Game *game);
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) {
@@ -32,17 +32,28 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
appDesc->render = (BzAppRenderFunc) render;
appDesc->imguiRender = (BzAppRenderFunc) imguiRender;
GAME = bzAlloc(sizeof(*GAME));
appDesc->userData = GAME;
Game *game = bzAlloc(sizeof(*game));
bzMemSet(game, 0, sizeof(*game));
appDesc->userData = NULL;
appDesc->useFlecs = true;
return true;
}
bool init(Game *game) {
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);
int screenWidth = 1280;
int screenHeight = 720;
@@ -97,18 +108,23 @@ bool init(Game *game) {
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
return true;
}
void deinit(Game *game) {
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);
bzFree(GAME);
GAME = NULL;
ecs_singleton_remove(ECS, Game);
}
void update(float dt, Game *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);
@@ -166,7 +182,10 @@ void update(float dt, Game *game) {
}
}
void render(float dt, Game *game) {
void render(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
ClearBackground(RAYWHITE);
BeginMode2D(game->camera);
@@ -204,7 +223,10 @@ void render(float dt, Game *game) {
EndMode2D();
}
void imguiRender(float dt, Game *game) {
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("Resources", 0)) {

View File

@@ -7,24 +7,26 @@
#include "map_layers.h"
bool initGameObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
Game *game = ecs_singleton_get_mut(ECS, Game);
for (i32 i = 0; i < objectGroup->objectCount; i++) {
BzTileObject object = objectGroup->objects[i];
if (bzStringDefaultHash("camera") == object.id) {
GAME->camera.target.x = object.shape.x;
GAME->camera.target.y = object.shape.y;
game->camera.target.x = object.shape.x;
game->camera.target.y = object.shape.y;
}
}
return true;
}
bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
BzTileset *objectTileset = bzTileObjectGroupGetTileset(&GAME->map, objectGroup);
Game *game = ecs_singleton_get_mut(ECS, Game);
BzTileset *objectTileset = bzTileObjectGroupGetTileset(&game->map, objectGroup);
if (!objectTileset) return true;
for (i32 i = 0; i < objectGroup->objectCount; i++) {
BzTileObject object = objectGroup->objects[i];
ecs_entity_t e = ecs_new_id(ECS);
GAME->entity = e;
game->entity = e;
ecs_set(ECS, e, Position, {object.shape.x, object.shape.y});
ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY});
ecs_set(ECS, e, Rotation, {0.0f});
@@ -45,7 +47,8 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
}
bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
GAME->entityMap = bzCalloc(sizeof(*GAME->entityMap), layer->width * layer->height);
Game *game = ecs_singleton_get_mut(ECS, Game);
game->entityMap = bzCalloc(sizeof(*game->entityMap), layer->width * layer->height);
BzTileLayer *ownershipLayer = layer;
BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
@@ -75,7 +78,7 @@ bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
for (i32 yIdx = y; yIdx < y + size.sizeY; yIdx++) {
for (i32 xIdx = x; xIdx < x + size.sizeX; xIdx++) {
GAME->entityMap[yIdx * layer->width + xIdx] = e;
game->entityMap[yIdx * layer->width + xIdx] = e;
}
}

View File

@@ -7,13 +7,14 @@
void updateAnimations(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Animation *anim = ecs_field(it, Animation, 1);
TextureRegion *t = ecs_field(it, TextureRegion, 2);
float dt = GetFrameTime();
for (i32 i = 0; i < it->count; i++) {
anim[i].frameDuration = GAME->frameDuration;
anim[i].frameDuration = game->frameDuration;
anim[i].elapsed += dt;
if (anim[i].elapsed < anim[i].frameDuration) continue;
@@ -66,7 +67,7 @@ void updatePos(ecs_iter_t *it) {
void targetFinish(ecs_iter_t *it) {
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t e = it->entities[i];
Path *path = ecs_get(it->world, e, Path);
Path *path = ecs_get_mut(it->world, e, Path);
if (!path) continue;
path->curWaypoint++;
if (path->curWaypoint >= path->numWaypoints) {

View File

@@ -3,10 +3,11 @@
#include "../game_state.h"
void uiTask(ecs_iter_t *it) {
const Game *game = ecs_singleton_get(ECS, Game);
Vector2 mousePos = GetMousePosition();
Vector2 worldPos = GetScreenToWorld2D(mousePos, GAME->camera);
Vector2 worldPos = GetScreenToWorld2D(mousePos, game->camera);
BzTileMap *map = &GAME->map;
const BzTileMap *map = &game->map;
i32 tileX = (i32) worldPos.x / map->tileWidth;
i32 tileY = (i32) worldPos.y / map->tileHeight;