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

@@ -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;
}
}