Move UI code into s_ui
This commit is contained in:
324
game/main.c
324
game/main.c
@@ -51,90 +51,6 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void terrainRender(BzTileMap *map, BzTileLayer *layer) {
|
|
||||||
BzTileset *tileset = bzTileLayerGetTileset(map, layer);
|
|
||||||
|
|
||||||
|
|
||||||
Vector2 drawPos = {layer->offsetX, layer->offsetY};
|
|
||||||
|
|
||||||
static f32 elapsed = 0.0f;
|
|
||||||
elapsed += GetFrameTime();
|
|
||||||
|
|
||||||
for (i32 y = 0; y < layer->height; y++) {
|
|
||||||
for (i32 x = 0; x < layer->width; x++) {
|
|
||||||
BzTile tile = bzTileLayerGetTile(layer, x, y);
|
|
||||||
tile = bzTilesetGetTileID(tileset, tile);
|
|
||||||
if (tile != -1) {
|
|
||||||
if (terrainHasAnimation(tile)) {
|
|
||||||
f32 frameDuration = terrainGetAnimationFrame(tile, 0).duration;
|
|
||||||
i32 numFrames = terrainGetAnimationSequence(tile).frameCount;
|
|
||||||
i32 frameIdx = (i32) (elapsed / frameDuration) % numFrames;
|
|
||||||
tile = terrainGetAnimationFrame(tile, frameIdx).frame;
|
|
||||||
}
|
|
||||||
Rectangle rec = bzTilesetGetTileRegion(tileset, tile);
|
|
||||||
DrawTextureRec(tileset->tiles, rec, drawPos, WHITE);
|
|
||||||
}
|
|
||||||
drawPos.x += (float) tileset->tileWidth;
|
|
||||||
}
|
|
||||||
drawPos.x = layer->offsetX;
|
|
||||||
drawPos.y += (float) tileset->tileHeight;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void unloadMap(Game *game) {
|
|
||||||
ecs_delete_with(ECS, GameEntity);
|
|
||||||
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,
|
|
||||||
.collisionMap=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", BZ_TILE_LAYER_SKIP_RENDER},
|
|
||||||
.layers[LAYER_TREES2]=(BzTileLayerDesc) {"trees_s", BZ_TILE_LAYER_SKIP_RENDER},
|
|
||||||
.layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"buildings", BZ_TILE_LAYER_SKIP_RENDER},
|
|
||||||
.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.offset = (Vector2) { GetScreenWidth() * 0.5f, GetScreenHeight() * 0.5f };
|
|
||||||
game->camera.rotation = 0.0f;
|
|
||||||
game->camera.zoom = 3.0f;
|
|
||||||
|
|
||||||
bzTileMapAddLayerCollisions(&game->map, LAYER_TERRAIN, COLL_LAYER_TERRAIN);
|
|
||||||
|
|
||||||
bzTileMapOverrideLayer(&game->map, LAYER_TREES, initTreesLayer);
|
|
||||||
bzTileMapOverrideLayer(&game->map, LAYER_TREES2, initTreesLayer);
|
|
||||||
|
|
||||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
|
||||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDINGS, BZ_TILE_LAYER_CLEAR);
|
|
||||||
|
|
||||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
|
||||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
|
|
||||||
|
|
||||||
}
|
|
||||||
int cmpDrawData(const void *a, const void *b) {
|
int cmpDrawData(const void *a, const void *b) {
|
||||||
const DrawData *lhs = (DrawData *) a;
|
const DrawData *lhs = (DrawData *) a;
|
||||||
const DrawData *rhs = (DrawData *) b;
|
const DrawData *rhs = (DrawData *) b;
|
||||||
@@ -520,240 +436,9 @@ static void renderGame(Game *game, float dt) {
|
|||||||
EndMode2D();
|
EndMode2D();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void renderGameMenu(Game *game, float dt) {
|
|
||||||
// UI
|
|
||||||
i32 width = GetScreenWidth();
|
|
||||||
i32 height = GetScreenHeight();
|
|
||||||
bzUIBegin(UI, width, height);
|
|
||||||
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN |
|
|
||||||
BZ_UI_FLEX_JUSTIFY_SPACE_BETWEEN |
|
|
||||||
BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
// top bar
|
|
||||||
f32 topBarHeight = 0.05f;
|
|
||||||
BzUINode *topBar = uiPushDivParentPercentage(1.0f, topBarHeight);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_ROW |
|
|
||||||
BZ_UI_FLEX_ALIGN_CENTER |
|
|
||||||
BZ_UI_FLEX_JUSTIFY_START
|
|
||||||
});
|
|
||||||
Color topBarBG = {0, 0, 0, 50};
|
|
||||||
bzUISetBackgroundStyle(UI, topBar, (BzUIBackgroundStyle) {
|
|
||||||
.normal = topBarBG,
|
|
||||||
.hover = topBarBG,
|
|
||||||
.active = topBarBG,
|
|
||||||
});
|
|
||||||
BzTileset *tileset = &game->tileset;
|
|
||||||
Rectangle woodRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_WOOD));
|
|
||||||
Rectangle stoneRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_STONE));
|
|
||||||
Rectangle foodRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_APPLE));
|
|
||||||
Rectangle goldRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_GOLD));
|
|
||||||
Rectangle popRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_POP));
|
|
||||||
uiGameResCount(100, -1, woodRec, tileset->tiles);
|
|
||||||
uiGameResCount(100, -1, stoneRec, tileset->tiles);
|
|
||||||
uiGameResCount(100, -1, foodRec, tileset->tiles);
|
|
||||||
uiGameResCount(250, -1, goldRec, tileset->tiles);
|
|
||||||
uiGameResCount(1, 10, popRec, tileset->tiles);
|
|
||||||
bzUIPopParent(UI);
|
|
||||||
|
|
||||||
uiPushDivParentPercentage(1.0f, 0.20f);
|
|
||||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
|
||||||
|
|
||||||
const BuildingType buildingOrder[] = {
|
|
||||||
BUILDING_HOUSE_01,
|
|
||||||
BUILDING_GRANARY,
|
|
||||||
BUILDING_WAREHOUSE,
|
|
||||||
BUILDING_MARKET,
|
|
||||||
BUILDING_MILL,
|
|
||||||
BUILDING_HOUSE_02, // placeholder for farm
|
|
||||||
BUILDING_BARRACS,
|
|
||||||
BUILDING_ARCHERY_RANGE
|
|
||||||
};
|
|
||||||
const char *buildingNames[] = {
|
|
||||||
"House",
|
|
||||||
"Granary",
|
|
||||||
"Warehouse",
|
|
||||||
"Market",
|
|
||||||
"Mill",
|
|
||||||
"Farm",
|
|
||||||
"Barracks",
|
|
||||||
"Archery Range"
|
|
||||||
};
|
|
||||||
i32 numBuildings = sizeof(buildingOrder) / sizeof(*buildingOrder);
|
|
||||||
|
|
||||||
switch (input->state) {
|
|
||||||
case INPUT_NONE:
|
|
||||||
case INPUT_BUILDING:
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_ROW | BZ_UI_FLEX_ALIGN_CENTER | BZ_UI_FLEX_JUSTIFY_START
|
|
||||||
});
|
|
||||||
for (i32 i = 0; i < numBuildings; i++) {
|
|
||||||
BuildingType buildingType = buildingOrder[i];
|
|
||||||
Rectangle rec = bzTilesetGetTileRegion(tileset, getBuildingTile(buildingType));
|
|
||||||
// Adjust for size
|
|
||||||
i32 sizeX = 1, sizeY = 1;
|
|
||||||
getBuildingSize(buildingType, &sizeX, &sizeY);
|
|
||||||
rec.width *= sizeX;
|
|
||||||
rec.height *= sizeY;
|
|
||||||
Texture2D tex = tileset->tiles;
|
|
||||||
bool selected = input->building == buildingOrder[i];
|
|
||||||
uiGameBuild(buildingNames[i], rec, tex, &selected);
|
|
||||||
if (selected) {
|
|
||||||
input->building = buildingOrder[i];
|
|
||||||
input->state = INPUT_BUILDING;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case INPUT_SELECTED_UNITS:
|
|
||||||
break;
|
|
||||||
case INPUT_SELECTED_OBJECT:
|
|
||||||
break;
|
|
||||||
case INPUT_SELECTED_BUILDING:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bzUISetDebugMode(UI, true);
|
|
||||||
bzUIEnd(UI);
|
|
||||||
bzUISetDebugMode(UI, false);
|
|
||||||
}
|
|
||||||
static void renderPauseMenu(Game *game, float dt) {
|
|
||||||
i32 width = GetScreenWidth();
|
|
||||||
i32 height = GetScreenHeight();
|
|
||||||
|
|
||||||
bzUIBegin(UI, width, height);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN
|
|
||||||
});
|
|
||||||
|
|
||||||
uiPushDivParentPercentage(1.0f, 0.4f);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
uiMainMenuLabel("Paused");
|
|
||||||
bzUIPopParent(UI);
|
|
||||||
|
|
||||||
uiPushDivParentPercentage(1.0f, 0.6f);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
if (uiMainMenuButton("Resume")) {
|
|
||||||
setScreen(game, SCREEN_GAME);
|
|
||||||
}
|
|
||||||
if (uiMainMenuButton("Exit")) {
|
|
||||||
setScreen(game, SCREEN_MAIN_MENU);
|
|
||||||
unloadMap(game);
|
|
||||||
loadMap(game, "assets/maps/main_menu_01.tmj");
|
|
||||||
}
|
|
||||||
bzUIPopParent(UI);
|
|
||||||
bzUIEnd(UI);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void renderMainMenu(Game *game, float dt) {
|
|
||||||
i32 width = GetScreenWidth();
|
|
||||||
i32 height = GetScreenHeight();
|
|
||||||
|
|
||||||
game->camera.zoom = 3 * uiGetScale();
|
|
||||||
|
|
||||||
bzUIBegin(UI, width, height);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN
|
|
||||||
});
|
|
||||||
|
|
||||||
uiPushDivParentPercentage(1.0f, 0.4f);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
uiMainMenuLabel("Pixel Defense");
|
|
||||||
bzUIPopParent(UI);
|
|
||||||
|
|
||||||
uiPushDivParentPercentage(1.0f, 0.6f);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
if (uiMainMenuButton("Play")) {
|
|
||||||
setScreen(game, SCREEN_GAME);
|
|
||||||
unloadMap(game);
|
|
||||||
loadMap(game, "assets/maps/tree_test.tmj");
|
|
||||||
}
|
|
||||||
if (uiMainMenuButton("Settings")) {
|
|
||||||
setScreen(game, SCREEN_SETTINGS);
|
|
||||||
}
|
|
||||||
if (uiMainMenuButton("Exit")) {
|
|
||||||
bzGameExit();
|
|
||||||
}
|
|
||||||
bzUIPopParent(UI);
|
|
||||||
bzUIEnd(UI);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void renderSettings(Game *game, float dt) {
|
|
||||||
i32 width = GetScreenWidth();
|
|
||||||
i32 height = GetScreenHeight();
|
|
||||||
|
|
||||||
game->camera.zoom = 3 * uiGetScale();
|
|
||||||
|
|
||||||
bzUIBegin(UI, width, height);
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
|
|
||||||
bzUIPushDiv(UI, (BzUISize) { BZ_UI_SIZE_REL_PARENT, 0.8f},
|
|
||||||
(BzUISize) { BZ_UI_SIZE_REL_PARENT, 0.8f});
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
static Options opts;
|
|
||||||
if (game->screenPrevFrame != SCREEN_SETTINGS)
|
|
||||||
opts = game->options;
|
|
||||||
uiSettingsLabel("Video");
|
|
||||||
uiSettingsCheckbox("Fullscreen", &opts.fullscreen);
|
|
||||||
uiSettingsCheckbox("V-Sync", &opts.vsync);
|
|
||||||
|
|
||||||
uiSettingsLabel("Audio");
|
|
||||||
uiSettingsSlider("Master: ", &opts.master);
|
|
||||||
uiSettingsSlider("Music: ", &opts.music);
|
|
||||||
uiSettingsSlider("Sound: ", &opts.sound);
|
|
||||||
|
|
||||||
bzUIPopParent(UI);
|
|
||||||
bzUIPushDiv(UI, (BzUISize) {BZ_UI_SIZE_REL_PARENT, 0.8f},
|
|
||||||
(BzUISize) {BZ_UI_SIZE_REL_PARENT, 0.2f});
|
|
||||||
bzUISetParentLayout(UI, (BzUILayout) {
|
|
||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
|
||||||
.flags = BZ_UI_FLEX_DIR_ROW | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
|
||||||
});
|
|
||||||
|
|
||||||
if (uiSettingsButton("Back")) {
|
|
||||||
setScreen(game, SCREEN_MAIN_MENU);
|
|
||||||
}
|
|
||||||
if (uiSettingsButton("Reset")) {
|
|
||||||
opts = game->options;
|
|
||||||
}
|
|
||||||
if (uiSettingsButton("Apply")) {
|
|
||||||
game->options = opts;
|
|
||||||
setScreen(game, SCREEN_MAIN_MENU);
|
|
||||||
}
|
|
||||||
|
|
||||||
bzUIEnd(UI);
|
|
||||||
}
|
|
||||||
|
|
||||||
void render(float dt, void *userData) {
|
void render(float dt, void *userData) {
|
||||||
BZ_UNUSED(userData);
|
BZ_UNUSED(userData);
|
||||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
const InputState *input = ecs_singleton_get(ECS, InputState);;
|
|
||||||
|
|
||||||
Color shadow = BLACK;
|
Color shadow = BLACK;
|
||||||
shadow.a = 35;
|
shadow.a = 35;
|
||||||
@@ -761,23 +446,22 @@ void render(float dt, void *userData) {
|
|||||||
switch (game->screen) {
|
switch (game->screen) {
|
||||||
case SCREEN_GAME:
|
case SCREEN_GAME:
|
||||||
renderGame(game, dt);
|
renderGame(game, dt);
|
||||||
renderGameMenu(game, dt);
|
drawGameUI(game, dt);
|
||||||
break;
|
break;
|
||||||
case SCREEN_PAUSE_MENU:
|
case SCREEN_PAUSE_MENU:
|
||||||
renderGame(game, dt);
|
renderGame(game, dt);
|
||||||
drawOverScreen(shadow);
|
drawOverScreen(shadow);
|
||||||
renderPauseMenu(game, dt);
|
drawPauseUI(game, dt);
|
||||||
break;
|
break;
|
||||||
case SCREEN_MAIN_MENU:
|
case SCREEN_MAIN_MENU:
|
||||||
renderGame(game, dt);
|
renderGame(game, dt);
|
||||||
drawOverScreen(shadow);
|
drawOverScreen(shadow);
|
||||||
renderMainMenu(game, dt);
|
drawMainMenuUI(game, dt);
|
||||||
break;
|
break;
|
||||||
case SCREEN_SETTINGS:
|
case SCREEN_SETTINGS:
|
||||||
renderGame(game, dt);
|
renderGame(game, dt);
|
||||||
drawOverScreen(shadow);
|
drawOverScreen(shadow);
|
||||||
renderSettings(game, dt);
|
drawSettingsUI(game, dt);
|
||||||
game->screenPrevFrame = SCREEN_SETTINGS;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,3 +116,88 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void terrainRender(BzTileMap *map, BzTileLayer *layer) {
|
||||||
|
BzTileset *tileset = bzTileLayerGetTileset(map, layer);
|
||||||
|
|
||||||
|
|
||||||
|
Vector2 drawPos = {layer->offsetX, layer->offsetY};
|
||||||
|
|
||||||
|
static f32 elapsed = 0.0f;
|
||||||
|
elapsed += GetFrameTime();
|
||||||
|
|
||||||
|
for (i32 y = 0; y < layer->height; y++) {
|
||||||
|
for (i32 x = 0; x < layer->width; x++) {
|
||||||
|
BzTile tile = bzTileLayerGetTile(layer, x, y);
|
||||||
|
tile = bzTilesetGetTileID(tileset, tile);
|
||||||
|
if (tile != -1) {
|
||||||
|
if (terrainHasAnimation(tile)) {
|
||||||
|
f32 frameDuration = terrainGetAnimationFrame(tile, 0).duration;
|
||||||
|
i32 numFrames = terrainGetAnimationSequence(tile).frameCount;
|
||||||
|
i32 frameIdx = (i32) (elapsed / frameDuration) % numFrames;
|
||||||
|
tile = terrainGetAnimationFrame(tile, frameIdx).frame;
|
||||||
|
}
|
||||||
|
Rectangle rec = bzTilesetGetTileRegion(tileset, tile);
|
||||||
|
DrawTextureRec(tileset->tiles, rec, drawPos, WHITE);
|
||||||
|
}
|
||||||
|
drawPos.x += (float) tileset->tileWidth;
|
||||||
|
}
|
||||||
|
drawPos.x = layer->offsetX;
|
||||||
|
drawPos.y += (float) tileset->tileHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void loadMap(Game *game, const char *path) {
|
||||||
|
|
||||||
|
game->map = bzTileMapCreate(&(BzTileMapDesc) {
|
||||||
|
.path=path,
|
||||||
|
.collisionMap=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", BZ_TILE_LAYER_SKIP_RENDER},
|
||||||
|
.layers[LAYER_TREES2]=(BzTileLayerDesc) {"trees_s", BZ_TILE_LAYER_SKIP_RENDER},
|
||||||
|
.layers[LAYER_BUILDINGS]=(BzTileLayerDesc) {"buildings", BZ_TILE_LAYER_SKIP_RENDER},
|
||||||
|
.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.offset = (Vector2) { GetScreenWidth() * 0.5f, GetScreenHeight() * 0.5f };
|
||||||
|
game->camera.rotation = 0.0f;
|
||||||
|
game->camera.zoom = 3.0f;
|
||||||
|
|
||||||
|
bzTileMapAddLayerCollisions(&game->map, LAYER_TERRAIN, COLL_LAYER_TERRAIN);
|
||||||
|
|
||||||
|
bzTileMapOverrideLayer(&game->map, LAYER_TREES, initTreesLayer);
|
||||||
|
bzTileMapOverrideLayer(&game->map, LAYER_TREES2, initTreesLayer);
|
||||||
|
|
||||||
|
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
||||||
|
bzTileMapOverrideLayer(&game->map, LAYER_BUILDINGS, BZ_TILE_LAYER_CLEAR);
|
||||||
|
|
||||||
|
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
||||||
|
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
|
||||||
|
|
||||||
|
}
|
||||||
|
void unloadMap(Game *game) {
|
||||||
|
ecs_delete_with(ECS, GameEntity);
|
||||||
|
if (game->map.isValid) {
|
||||||
|
bzTileMapDestroy(&game->map);
|
||||||
|
game->map.isValid = false;
|
||||||
|
}
|
||||||
|
if (game->entityGrid) {
|
||||||
|
bzSpatialGridDestroy(game->entityGrid);
|
||||||
|
game->entityGrid = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,4 +14,9 @@ bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer);
|
|||||||
|
|
||||||
bool initTreesLayer(BzTileMap *map, BzTileLayer *layer);
|
bool initTreesLayer(BzTileMap *map, BzTileLayer *layer);
|
||||||
|
|
||||||
|
typedef struct Game Game;
|
||||||
|
|
||||||
|
void loadMap(Game *game, const char *path);
|
||||||
|
void unloadMap(Game *game);
|
||||||
|
|
||||||
#endif //PIXELDEFENSE_MAP_INITIALIZATION_H
|
#endif //PIXELDEFENSE_MAP_INITIALIZATION_H
|
||||||
|
|||||||
@@ -1,15 +1,236 @@
|
|||||||
#include "systems.h"
|
#include "systems.h"
|
||||||
|
|
||||||
#include "../game_state.h"
|
#include "../game_state.h"
|
||||||
|
#include "../input.h"
|
||||||
|
#include "../map_init.h"
|
||||||
|
#include "../ui_widgets.h"
|
||||||
|
|
||||||
void uiTask(ecs_iter_t *it) {
|
void drawGameUI(Game *game, f32 dt) {
|
||||||
const Game *game = ecs_singleton_get(ECS, Game);
|
// UI
|
||||||
Vector2 mousePos = GetMousePosition();
|
i32 width = GetScreenWidth();
|
||||||
Vector2 worldPos = GetScreenToWorld2D(mousePos, game->camera);
|
i32 height = GetScreenHeight();
|
||||||
|
bzUIBegin(UI, width, height);
|
||||||
|
|
||||||
const BzTileMap *map = &game->map;
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN |
|
||||||
|
BZ_UI_FLEX_JUSTIFY_SPACE_BETWEEN |
|
||||||
|
BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
// top bar
|
||||||
|
f32 topBarHeight = 0.05f;
|
||||||
|
BzUINode *topBar = uiPushDivParentPercentage(1.0f, topBarHeight);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_ROW |
|
||||||
|
BZ_UI_FLEX_ALIGN_CENTER |
|
||||||
|
BZ_UI_FLEX_JUSTIFY_START
|
||||||
|
});
|
||||||
|
Color topBarBG = {0, 0, 0, 50};
|
||||||
|
bzUISetBackgroundStyle(UI, topBar, (BzUIBackgroundStyle) {
|
||||||
|
.normal = topBarBG,
|
||||||
|
.hover = topBarBG,
|
||||||
|
.active = topBarBG,
|
||||||
|
});
|
||||||
|
BzTileset *tileset = &game->tileset;
|
||||||
|
Rectangle woodRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_WOOD));
|
||||||
|
Rectangle stoneRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_STONE));
|
||||||
|
Rectangle foodRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_APPLE));
|
||||||
|
Rectangle goldRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_GOLD));
|
||||||
|
Rectangle popRec = bzTilesetGetTileRegion(tileset, getEntityTile(ENTITY_POP));
|
||||||
|
uiGameResCount(100, -1, woodRec, tileset->tiles);
|
||||||
|
uiGameResCount(100, -1, stoneRec, tileset->tiles);
|
||||||
|
uiGameResCount(100, -1, foodRec, tileset->tiles);
|
||||||
|
uiGameResCount(250, -1, goldRec, tileset->tiles);
|
||||||
|
uiGameResCount(1, 10, popRec, tileset->tiles);
|
||||||
|
bzUIPopParent(UI);
|
||||||
|
|
||||||
i32 tileX = (i32) worldPos.x / map->tileWidth;
|
uiPushDivParentPercentage(1.0f, 0.20f);
|
||||||
i32 tileY = (i32) worldPos.y / map->tileHeight;
|
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||||
|
|
||||||
|
const BuildingType buildingOrder[] = {
|
||||||
|
BUILDING_HOUSE_01,
|
||||||
|
BUILDING_GRANARY,
|
||||||
|
BUILDING_WAREHOUSE,
|
||||||
|
BUILDING_MARKET,
|
||||||
|
BUILDING_MILL,
|
||||||
|
BUILDING_HOUSE_02, // placeholder for farm
|
||||||
|
BUILDING_BARRACS,
|
||||||
|
BUILDING_ARCHERY_RANGE
|
||||||
|
};
|
||||||
|
const char *buildingNames[] = {
|
||||||
|
"House",
|
||||||
|
"Granary",
|
||||||
|
"Warehouse",
|
||||||
|
"Market",
|
||||||
|
"Mill",
|
||||||
|
"Farm",
|
||||||
|
"Barracks",
|
||||||
|
"Archery Range"
|
||||||
|
};
|
||||||
|
i32 numBuildings = sizeof(buildingOrder) / sizeof(*buildingOrder);
|
||||||
|
|
||||||
|
switch (input->state) {
|
||||||
|
case INPUT_NONE:
|
||||||
|
case INPUT_BUILDING:
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_ROW | BZ_UI_FLEX_ALIGN_CENTER | BZ_UI_FLEX_JUSTIFY_START
|
||||||
|
});
|
||||||
|
for (i32 i = 0; i < numBuildings; i++) {
|
||||||
|
BuildingType buildingType = buildingOrder[i];
|
||||||
|
Rectangle rec = bzTilesetGetTileRegion(tileset, getBuildingTile(buildingType));
|
||||||
|
// Adjust for size
|
||||||
|
i32 sizeX = 1, sizeY = 1;
|
||||||
|
getBuildingSize(buildingType, &sizeX, &sizeY);
|
||||||
|
rec.width *= sizeX;
|
||||||
|
rec.height *= sizeY;
|
||||||
|
Texture2D tex = tileset->tiles;
|
||||||
|
bool selected = input->building == buildingOrder[i];
|
||||||
|
uiGameBuild(buildingNames[i], rec, tex, &selected);
|
||||||
|
if (selected) {
|
||||||
|
input->building = buildingOrder[i];
|
||||||
|
input->state = INPUT_BUILDING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case INPUT_SELECTED_UNITS:
|
||||||
|
break;
|
||||||
|
case INPUT_SELECTED_OBJECT:
|
||||||
|
break;
|
||||||
|
case INPUT_SELECTED_BUILDING:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bzUISetDebugMode(UI, true);
|
||||||
|
bzUIEnd(UI);
|
||||||
|
bzUISetDebugMode(UI, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawPauseUI(Game *game, f32 dt) {
|
||||||
|
i32 width = GetScreenWidth();
|
||||||
|
i32 height = GetScreenHeight();
|
||||||
|
|
||||||
|
bzUIBegin(UI, width, height);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN
|
||||||
|
});
|
||||||
|
|
||||||
|
uiPushDivParentPercentage(1.0f, 0.4f);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
uiMainMenuLabel("Paused");
|
||||||
|
bzUIPopParent(UI);
|
||||||
|
|
||||||
|
uiPushDivParentPercentage(1.0f, 0.6f);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
if (uiMainMenuButton("Resume")) {
|
||||||
|
setScreen(game, SCREEN_GAME);
|
||||||
|
}
|
||||||
|
if (uiMainMenuButton("Exit")) {
|
||||||
|
setScreen(game, SCREEN_MAIN_MENU);
|
||||||
|
unloadMap(game);
|
||||||
|
loadMap(game, "assets/maps/main_menu_01.tmj");
|
||||||
|
}
|
||||||
|
bzUIPopParent(UI);
|
||||||
|
bzUIEnd(UI);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawMainMenuUI(Game *game, f32 dt) {
|
||||||
|
i32 width = GetScreenWidth();
|
||||||
|
i32 height = GetScreenHeight();
|
||||||
|
|
||||||
|
game->camera.zoom = 3 * uiGetScale();
|
||||||
|
|
||||||
|
bzUIBegin(UI, width, height);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN
|
||||||
|
});
|
||||||
|
|
||||||
|
uiPushDivParentPercentage(1.0f, 0.4f);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
uiMainMenuLabel("Pixel Defense");
|
||||||
|
bzUIPopParent(UI);
|
||||||
|
|
||||||
|
uiPushDivParentPercentage(1.0f, 0.6f);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
if (uiMainMenuButton("Play")) {
|
||||||
|
setScreen(game, SCREEN_GAME);
|
||||||
|
unloadMap(game);
|
||||||
|
loadMap(game, "assets/maps/tree_test.tmj");
|
||||||
|
}
|
||||||
|
if (uiMainMenuButton("Settings")) {
|
||||||
|
setScreen(game, SCREEN_SETTINGS);
|
||||||
|
}
|
||||||
|
if (uiMainMenuButton("Exit")) {
|
||||||
|
bzGameExit();
|
||||||
|
}
|
||||||
|
bzUIPopParent(UI);
|
||||||
|
bzUIEnd(UI);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawSettingsUI(Game *game, f32 dt) {
|
||||||
|
i32 width = GetScreenWidth();
|
||||||
|
i32 height = GetScreenHeight();
|
||||||
|
|
||||||
|
game->camera.zoom = 3 * uiGetScale();
|
||||||
|
|
||||||
|
bzUIBegin(UI, width, height);
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
|
||||||
|
bzUIPushDiv(UI, (BzUISize) { BZ_UI_SIZE_REL_PARENT, 0.8f},
|
||||||
|
(BzUISize) { BZ_UI_SIZE_REL_PARENT, 0.8f});
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
static Options opts;
|
||||||
|
if (game->screenPrevFrame != SCREEN_SETTINGS)
|
||||||
|
opts = game->options;
|
||||||
|
uiSettingsLabel("Video");
|
||||||
|
uiSettingsCheckbox("Fullscreen", &opts.fullscreen);
|
||||||
|
uiSettingsCheckbox("V-Sync", &opts.vsync);
|
||||||
|
|
||||||
|
uiSettingsLabel("Audio");
|
||||||
|
uiSettingsSlider("Master: ", &opts.master);
|
||||||
|
uiSettingsSlider("Music: ", &opts.music);
|
||||||
|
uiSettingsSlider("Sound: ", &opts.sound);
|
||||||
|
|
||||||
|
bzUIPopParent(UI);
|
||||||
|
bzUIPushDiv(UI, (BzUISize) {BZ_UI_SIZE_REL_PARENT, 0.8f},
|
||||||
|
(BzUISize) {BZ_UI_SIZE_REL_PARENT, 0.2f});
|
||||||
|
bzUISetParentLayout(UI, (BzUILayout) {
|
||||||
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
|
.flags = BZ_UI_FLEX_DIR_ROW | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
|
});
|
||||||
|
|
||||||
|
if (uiSettingsButton("Back")) {
|
||||||
|
setScreen(game, SCREEN_MAIN_MENU);
|
||||||
|
}
|
||||||
|
if (uiSettingsButton("Reset")) {
|
||||||
|
opts = game->options;
|
||||||
|
}
|
||||||
|
if (uiSettingsButton("Apply")) {
|
||||||
|
game->options = opts;
|
||||||
|
setScreen(game, SCREEN_MAIN_MENU);
|
||||||
|
}
|
||||||
|
|
||||||
|
bzUIEnd(UI);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,6 +162,11 @@ void drawPlayerInputUI();
|
|||||||
* UI systems
|
* UI systems
|
||||||
**********************************/
|
**********************************/
|
||||||
|
|
||||||
|
void drawGameUI(Game *game, f32 dt);
|
||||||
|
void drawPauseUI(Game *game, f32 dt);
|
||||||
|
void drawMainMenuUI(Game *game, f32 dt);
|
||||||
|
void drawSettingsUI(Game *game, f32 dt);
|
||||||
|
|
||||||
/**********************************
|
/**********************************
|
||||||
* Utils
|
* Utils
|
||||||
**********************************/
|
**********************************/
|
||||||
|
|||||||
Reference in New Issue
Block a user