Add Game as singleton
This commit is contained in:
@@ -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) {
|
ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile tileY) {
|
||||||
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
i32 sizeX, sizeY;
|
i32 sizeX, sizeY;
|
||||||
getBuildingSize(type, &sizeX, &sizeY);
|
getBuildingSize(type, &sizeX, &sizeY);
|
||||||
ECS_COMPONENT(ECS, TilePosition);
|
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);
|
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
|
||||||
buildingTile++;
|
buildingTile++;
|
||||||
|
|
||||||
GAME->entityMap[y * buildingLayer->width + x] = e;
|
game->entityMap[y * buildingLayer->width + x] = e;
|
||||||
|
|
||||||
bzTileMapUpdateCollider(map, x, y);
|
bzTileMapUpdateCollider(map, x, y);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,6 @@ typedef struct Game {
|
|||||||
f32 elapsed;
|
f32 elapsed;
|
||||||
} Game;
|
} Game;
|
||||||
|
|
||||||
extern Game *GAME;
|
extern ECS_COMPONENT_DECLARE(Game);
|
||||||
|
|
||||||
#endif //PIXELDEFENSE_GAME_STATE_H
|
#endif //PIXELDEFENSE_GAME_STATE_H
|
||||||
|
|||||||
52
game/main.c
52
game/main.c
@@ -10,14 +10,14 @@
|
|||||||
|
|
||||||
#include "utils/pathfinding.h"
|
#include "utils/pathfinding.h"
|
||||||
|
|
||||||
Game *GAME = NULL;
|
ECS_COMPONENT_DECLARE(Game);
|
||||||
|
|
||||||
bool init(Game *game);
|
bool init(void *userData);
|
||||||
void deinit(Game *game);
|
void deinit(void *userData);
|
||||||
|
|
||||||
void update(float dt, Game *game);
|
void update(float dt, void *userData);
|
||||||
void render(float dt, Game *game);
|
void render(float dt, void *userData);
|
||||||
void imguiRender(float dt, Game *game);
|
void imguiRender(float dt, void *userData);
|
||||||
|
|
||||||
|
|
||||||
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
|
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->render = (BzAppRenderFunc) render;
|
||||||
appDesc->imguiRender = (BzAppRenderFunc) imguiRender;
|
appDesc->imguiRender = (BzAppRenderFunc) imguiRender;
|
||||||
|
|
||||||
GAME = bzAlloc(sizeof(*GAME));
|
Game *game = bzAlloc(sizeof(*game));
|
||||||
appDesc->userData = GAME;
|
bzMemSet(game, 0, sizeof(*game));
|
||||||
|
appDesc->userData = NULL;
|
||||||
appDesc->useFlecs = true;
|
appDesc->useFlecs = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool init(Game *game) {
|
bool init(void *userData) {
|
||||||
|
BZ_UNUSED(userData);
|
||||||
SetExitKey(-1);
|
SetExitKey(-1);
|
||||||
|
ECS_COMPONENT_DEFINE(ECS, Game);
|
||||||
initComponentIDs(ECS);
|
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 screenWidth = 1280;
|
||||||
int screenHeight = 720;
|
int screenHeight = 720;
|
||||||
|
|
||||||
@@ -97,18 +108,23 @@ bool init(Game *game) {
|
|||||||
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
|
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void deinit(Game *game) {
|
void deinit(void *userData) {
|
||||||
|
BZ_UNUSED(userData);
|
||||||
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
|
|
||||||
bzTileMapDestroy(&game->map);
|
bzTileMapDestroy(&game->map);
|
||||||
bzTilesetDestroy(&game->terrainTileset);
|
bzTilesetDestroy(&game->terrainTileset);
|
||||||
bzTilesetDestroy(&game->buildingsTileset);
|
bzTilesetDestroy(&game->buildingsTileset);
|
||||||
bzTilesetDestroy(&game->entitiesTileset);
|
bzTilesetDestroy(&game->entitiesTileset);
|
||||||
|
|
||||||
bzFree(GAME);
|
ecs_singleton_remove(ECS, Game);
|
||||||
GAME = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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];
|
char titleBuf[32];
|
||||||
snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000);
|
snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000);
|
||||||
SetWindowTitle(titleBuf);
|
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);
|
ClearBackground(RAYWHITE);
|
||||||
BeginMode2D(game->camera);
|
BeginMode2D(game->camera);
|
||||||
|
|
||||||
@@ -204,7 +223,10 @@ void render(float dt, Game *game) {
|
|||||||
EndMode2D();
|
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);
|
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
||||||
igBegin("Debug Menu", NULL, 0);
|
igBegin("Debug Menu", NULL, 0);
|
||||||
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {
|
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {
|
||||||
|
|||||||
@@ -7,24 +7,26 @@
|
|||||||
#include "map_layers.h"
|
#include "map_layers.h"
|
||||||
|
|
||||||
bool initGameObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
bool initGameObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||||
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
||||||
BzTileObject object = objectGroup->objects[i];
|
BzTileObject object = objectGroup->objects[i];
|
||||||
if (bzStringDefaultHash("camera") == object.id) {
|
if (bzStringDefaultHash("camera") == object.id) {
|
||||||
GAME->camera.target.x = object.shape.x;
|
game->camera.target.x = object.shape.x;
|
||||||
GAME->camera.target.y = object.shape.y;
|
game->camera.target.y = object.shape.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
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;
|
if (!objectTileset) return true;
|
||||||
|
|
||||||
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
||||||
BzTileObject object = objectGroup->objects[i];
|
BzTileObject object = objectGroup->objects[i];
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
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, Position, {object.shape.x, object.shape.y});
|
||||||
ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY});
|
ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY});
|
||||||
ecs_set(ECS, e, Rotation, {0.0f});
|
ecs_set(ECS, e, Rotation, {0.0f});
|
||||||
@@ -45,7 +47,8 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
|
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 *ownershipLayer = layer;
|
||||||
BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
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 yIdx = y; yIdx < y + size.sizeY; yIdx++) {
|
||||||
for (i32 xIdx = x; xIdx < x + size.sizeX; xIdx++) {
|
for (i32 xIdx = x; xIdx < x + size.sizeX; xIdx++) {
|
||||||
GAME->entityMap[yIdx * layer->width + xIdx] = e;
|
game->entityMap[yIdx * layer->width + xIdx] = e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,14 @@
|
|||||||
|
|
||||||
|
|
||||||
void updateAnimations(ecs_iter_t *it) {
|
void updateAnimations(ecs_iter_t *it) {
|
||||||
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
Animation *anim = ecs_field(it, Animation, 1);
|
Animation *anim = ecs_field(it, Animation, 1);
|
||||||
TextureRegion *t = ecs_field(it, TextureRegion, 2);
|
TextureRegion *t = ecs_field(it, TextureRegion, 2);
|
||||||
|
|
||||||
float dt = GetFrameTime();
|
float dt = GetFrameTime();
|
||||||
|
|
||||||
for (i32 i = 0; i < it->count; i++) {
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
anim[i].frameDuration = GAME->frameDuration;
|
anim[i].frameDuration = game->frameDuration;
|
||||||
anim[i].elapsed += dt;
|
anim[i].elapsed += dt;
|
||||||
if (anim[i].elapsed < anim[i].frameDuration) continue;
|
if (anim[i].elapsed < anim[i].frameDuration) continue;
|
||||||
|
|
||||||
@@ -66,7 +67,7 @@ void updatePos(ecs_iter_t *it) {
|
|||||||
void targetFinish(ecs_iter_t *it) {
|
void targetFinish(ecs_iter_t *it) {
|
||||||
for (i32 i = 0; i < it->count; i++) {
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
ecs_entity_t e = it->entities[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;
|
if (!path) continue;
|
||||||
path->curWaypoint++;
|
path->curWaypoint++;
|
||||||
if (path->curWaypoint >= path->numWaypoints) {
|
if (path->curWaypoint >= path->numWaypoints) {
|
||||||
|
|||||||
@@ -3,10 +3,11 @@
|
|||||||
#include "../game_state.h"
|
#include "../game_state.h"
|
||||||
|
|
||||||
void uiTask(ecs_iter_t *it) {
|
void uiTask(ecs_iter_t *it) {
|
||||||
|
const Game *game = ecs_singleton_get(ECS, Game);
|
||||||
Vector2 mousePos = GetMousePosition();
|
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 tileX = (i32) worldPos.x / map->tileWidth;
|
||||||
i32 tileY = (i32) worldPos.y / map->tileHeight;
|
i32 tileY = (i32) worldPos.y / map->tileHeight;
|
||||||
|
|||||||
Reference in New Issue
Block a user