Move UI code into s_ui
This commit is contained in:
@@ -116,3 +116,88 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user