Add game scenario to Main menu background

This commit is contained in:
2023-12-22 08:07:22 +01:00
parent 1b801caa82
commit dbb5ac5382
4 changed files with 698 additions and 66 deletions

View File

@@ -5,9 +5,9 @@
#include <flecs.h>
typedef enum GameScreen {
SCREEN_GAME,
SCREEN_MAIN_MENU,
SCREEN_SETTINGS,
SCREEN_GAME
} GameScreen;
typedef struct Game {

View File

@@ -84,6 +84,56 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) {
}
}
void unloadMap(Game *game) {
if (game->map.isValid) {
bzTileMapDestroy(&game->map);
game->map.isValid = false;
}
if (game->entityGrid) {
bzSpatialGridDestroy(game->entityGrid);
game->entityGrid = NULL;
}
}
void loadMap(Game *game, const char *path) {
game->map = bzTileMapCreate(&(BzTileMapDesc) {
.path=path,
.generateCollisionMap=true,
.tilesets[0]=game->tileset,
.layers[LAYER_TERRAIN]=(BzTileLayerDesc) {"terrain", .renderer=terrainRender, .applyColliders=true},
.layers[LAYER_ROCKS]=(BzTileLayerDesc) {"rocks"},
.layers[LAYER_ROCKS2]=(BzTileLayerDesc) {"rocks_s"},
.layers[LAYER_TREES]=(BzTileLayerDesc) {"trees"},
.layers[LAYER_TREES2]=(BzTileLayerDesc) {"trees_s"},
.layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"buildings", .applyColliders=true},
.layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"building_ownership", 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 * 4,
.cellHeight=game->map.tileHeight * 4,
.userDataSize=sizeof(ecs_entity_t)
});
game->camera = (Camera2D){ 0 };
game->camera.target = (Vector2) {0, 0};
game->camera.rotation = 0.0f;
game->camera.zoom = 3.0f;
bzTileMapOverrideLayer(&game->map, LAYER_TREES, initTreesLayer);
bzTileMapOverrideLayer(&game->map, LAYER_TREES2, initTreesLayer);
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
}
bool init(void *userData) {
BZ_UNUSED(userData);
SetExitKey(0);
@@ -128,58 +178,20 @@ bool init(void *userData) {
});
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 = 3.0f;
game->frameDuration = 0.16f;
game->tileset = bzTilesetCreate( &(BzTilesetDesc) {
.path="assets/game.tsj",
.texturePath="assets/game.png"
});
loadMap(game, "assets/maps/main_menu_01.tmj");
game->map = bzTileMapCreate(&(BzTileMapDesc) {
.path="assets/maps/map_01.tmj",
.generateCollisionMap=true,
.tilesets[0]=game->tileset,
.layers[LAYER_TERRAIN]=(BzTileLayerDesc) {"terrain", .renderer=terrainRender, .applyColliders=true},
.layers[LAYER_ROCKS]=(BzTileLayerDesc) {"rocks"},
.layers[LAYER_ROCKS2]=(BzTileLayerDesc) {"rocks_s"},
.layers[LAYER_TREES]=(BzTileLayerDesc) {"trees"},
.layers[LAYER_TREES2]=(BzTileLayerDesc) {"trees_s"},
.layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"buildings", .applyColliders=true},
.layers[LAYER_BUILDING_OWNER]=(BzTileLayerDesc) {"building_ownership", 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 * 4,
.cellHeight=game->map.tileHeight * 4,
.userDataSize=sizeof(ecs_entity_t)
});
ECS_OBSERVER(ECS, entitySpatialRemove, EcsOnRemove, SpatialGridID);
ECS_OBSERVER(ECS, entityPathRemove, EcsOnRemove, Path);
//ECS_OBSERVER(ECS, entitySetAnimationState, EcsOnSet, Animation, AnimationType);
bzTileMapOverrideLayer(&game->map, LAYER_TREES, initTreesLayer);
bzTileMapOverrideLayer(&game->map, LAYER_TREES2, initTreesLayer);
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID);
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, Steering);
@@ -218,7 +230,7 @@ void deinit(void *userData) {
Game *game = ecs_singleton_get_mut(ECS, Game);
InputState *input = ecs_singleton_get_mut(ECS, InputState);
bzTileMapDestroy(&game->map);
unloadMap(game);
bzTilesetDestroy(&game->tileset);
Game gameCopy = *game;
@@ -233,7 +245,6 @@ void deinit(void *userData) {
bzStackAllocDestroy(&gameCopy.stackAlloc);
bzObjectPoolDestroy(gameCopy.pools.pathData);
bzObjectPoolDestroy(gameCopy.pools.actions);
bzSpatialGridDestroy(gameCopy.entityGrid);
bzUIDestroy(UI);
UI = NULL;
@@ -256,19 +267,46 @@ void update(float dt, void *userData) {
updateInputState(input, game->camera, dt);
switch (game->screen) {
case SCREEN_MAIN_MENU:
break;
case SCREEN_GAME:
updatePlayerInput();
break;
case SCREEN_MAIN_MENU:
break;
case SCREEN_SETTINGS:
break;
}
}
static void renderMainMenu(Game *game, float dt) {
static void renderGame(Game *game, float dt) {
ClearBackground(RAYWHITE);
BeginMode2D(game->camera);
bzTileMapDraw(&game->map);
drawPlayerInputUIGround();
ecs_progress(ECS, dt);
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
if (game->debugDraw.mapColliders)
bzTileMapDrawCollisions(&game->map);
if (game->debugDraw.spatialGrid)
bzSpatialGridDrawDebugGrid(game->entityGrid);
drawPlayerInputUI();
EndMode2D();
}
static void drawOverScreen(Color c) {
i32 width = GetScreenWidth();
i32 height = GetScreenHeight();
ClearBackground(RAYWHITE);
DrawRectangle(0, 0, width, height, c);
}
static void renderMainMenu(Game *game, float dt) {
i32 width = GetScreenWidth();
i32 height = GetScreenHeight();
bzUIBegin(UI, width, height);
bzUISetParentLayout(UI, (BzUILayout) {
@@ -291,6 +329,7 @@ static void renderMainMenu(Game *game, float dt) {
});
if (uiLargeTextButton("Play")) {
bzLogInfo("Play");
game->screen = SCREEN_GAME;
}
if (uiLargeTextButton("Settings")) {
bzLogInfo("Settings");
@@ -311,33 +350,21 @@ void render(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
Color shadow = BLACK;
shadow.a = 35;
switch (game->screen) {
case SCREEN_GAME:
renderGame(game, dt);
break;
case SCREEN_MAIN_MENU:
renderGame(game, dt);
drawOverScreen(shadow);
renderMainMenu(game, dt);
break;
case SCREEN_SETTINGS:
renderSettings(game, dt);
break;
case SCREEN_GAME:
ClearBackground(RAYWHITE);
BeginMode2D(game->camera);
bzTileMapDraw(&game->map);
drawPlayerInputUIGround();
ecs_progress(ECS, dt);
ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path);
ecs_enable(ECS, renderCollidersSystem, game->debugDraw.entityColliders);
if (game->debugDraw.mapColliders)
bzTileMapDrawCollisions(&game->map);
if (game->debugDraw.spatialGrid)
bzSpatialGridDrawDebugGrid(game->entityGrid);
drawPlayerInputUI();
EndMode2D();
break;
}