Skip to content
Snippets Groups Projects
Commit 5b549d1e authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

slightly more intresting gridworld

parent 21a762b4
No related branches found
No related tags found
No related merge requests found
---
floors:
ground:
visible: true
walls:
none:
visible: false
solid:
visible: true
\ No newline at end of file
...@@ -17,47 +17,100 @@ ...@@ -17,47 +17,100 @@
// //
#include "grid.hpp" #include "grid.hpp"
#include "fggl/assets/loader.hpp"
#include "fggl/entity/gridworld/zone.hpp" #include "fggl/entity/gridworld/zone.hpp"
namespace demo { using namespace fggl::gfx::colours;
GridScene::GridScene(fggl::App &app) : Game(app), m_tiles(), m_grid(nullptr) { namespace demo {
}
void GridScene::activate() { using namespace fggl::entity::grid;
Game::activate();
fggl::debug::log(fggl::debug::Level::info, "GridScene::activate()");
// create the grid world static void build_tileset(TileSet& tiles) {
fggl::entity::grid::FloorTile empty{fggl::entity::grid::FloorTile::IMPOSSIBLE, fggl::gfx::colours::BLACK}; fggl::entity::grid::FloorTile empty{fggl::entity::grid::FloorTile::IMPOSSIBLE, BLACK};
fggl::entity::grid::FloorTile ground{1, fggl::gfx::colours::GREEN}; fggl::entity::grid::FloorTile ground{1, GREEN};
m_tiles.m_floors.push_back(empty); tiles.m_floors.push_back(empty);
m_tiles.m_floors.push_back(ground); tiles.m_floors.push_back(ground);
fggl::entity::grid::WallTile noWall{}; fggl::entity::grid::WallTile noWall{};
m_tiles.m_walls.push_back(noWall); tiles.m_walls.push_back(noWall);
fggl::entity::grid::WallTile solidWall{ fggl::entity::grid::WallTile solidWall{
.render = true, .render = true,
.colour = fggl::gfx::colours::DARK_SLATE_GRAY .colour = DARK_SLATE_GRAY
}; };
m_tiles.m_walls.push_back(solidWall); tiles.m_walls.push_back(solidWall);
}
m_grid = std::make_unique<fggl::entity::grid::Area2D<255,255>>(m_tiles); static void build_room(DemoGrid* area, fggl::math::vec2i center, fggl::math::vec2i size) {
m_grid->clear();
// create a small area to use for (int yOffset = -size.y; yOffset <= size.y; ++yOffset) {
for (int i=3; i<20; ++i) { auto yPos = yOffset + center.y;
m_grid->setWallAt(i, 3, true, 1); area->setWallAt(center.x - size.x, yPos, false, 1);
m_grid->setWallAt(i, 20, true, 1); area->setWallAt(center.x + size.x + 1, yPos, false, 1);
m_grid->setWallAt(20, i, false, 1); }
m_grid->setWallAt(3, i, false, 1);
for (int xOffset = -size.x; xOffset <= size.x; ++xOffset) {
auto xPos = center.x + xOffset;
area->setWallAt(xPos, center.y - size.y, true, 1);
area->setWallAt(xPos, center.y + size.y + 1, true, 1);
for (int yOffset = -size.y; yOffset <= size.y; ++yOffset) {
auto yPos = yOffset + center.y;
area->setFloorAt(xPos, yPos, 1);
}
}
}
for (int j=3; j<20; ++j) { void build_doorway(DemoGrid* area, fggl::math::vec2i position, bool north, int size = 1) {
m_grid->setFloorAt(i, j, 1); for ( auto offset = 0; offset < size; offset++) {
if ( north ) {
area->setWallAt(position.x + offset, position.y, north, 0);
} else {
area->setWallAt(position.x, position.y + offset, north, 0);
} }
} }
}
static void build_test_env(DemoGrid* area) {
area->clear();
build_room(area, {5, 5}, {4,4});
build_room(area, {11, 5}, {1,1});
build_room(area, {17, 5}, {4,4});
build_doorway(area, {10, 5}, false, 1);
build_doorway(area, {13, 5}, false, 1);
build_room(area, {25, 5}, {3,3});
// player
auto& manager = area->entities();
{
auto player = manager.create();
auto& cellPos = manager.add<CellPos>(player);
cellPos.pos = {5,5};
}
}
GridScene::GridScene(fggl::App &app) : Game(app), m_tiles(), m_grid(nullptr) {
}
void GridScene::activate() {
Game::activate();
fggl::debug::log(fggl::debug::Level::info, "GridScene::activate()");
// fake loading the tileset
if ( m_tiles.m_floors.empty() ) {
build_tileset(m_tiles);
//auto* assetLoader = m_owner.service<fggl::assets::Loader>();
//assetLoader->load("tileset_base.yml", ASSET_TILESET);
}
// create the grid world
m_grid = std::make_unique<DemoGrid>(m_tiles);
build_test_env(m_grid.get());
} }
void GridScene::deactivate() { void GridScene::deactivate() {
...@@ -99,12 +152,31 @@ namespace demo { ...@@ -99,12 +152,31 @@ namespace demo {
} }
} }
float progress = 0.0f;
static void render_objects(fggl::gfx::Paint& paint, DemoGrid& grid) {
auto& manager = grid.entities();
auto entities = manager.find<CellPos>();
for (const auto& entity : entities ) {
//auto& sprite = manager.get<Sprite>(entity);
auto& cellPos = manager.get<CellPos>(entity);
// convert grid pos to world pos
fggl::math::vec2f drawPos = cellPos.pos;
drawPos *= DRAW_SIZE;
auto shape = fggl::gfx::make_shape(drawPos, DRAW_HALF, 3);
paint.fill(shape);
}
}
//float progress = 0.0f;
void GridScene::render(fggl::gfx::Graphics &gfx) { void GridScene::render(fggl::gfx::Graphics &gfx) {
Game::render(gfx); Game::render(gfx);
fggl::gfx::Paint paint; fggl::gfx::Paint paint;
render_grid(paint, *m_grid); render_grid(paint, *m_grid);
render_objects(paint, *m_grid);
/* /*
// draw test shapes to check grid alignment // draw test shapes to check grid alignment
......
...@@ -27,6 +27,15 @@ ...@@ -27,6 +27,15 @@
namespace demo { namespace demo {
constexpr int GRID_SIZE = 255; constexpr int GRID_SIZE = 255;
using DemoGrid = fggl::entity::grid::Area2D<GRID_SIZE, GRID_SIZE>;
struct Sprite {
};
struct CellPos {
fggl::math::vec2i pos;
};
class GridScene : public fggl::scenes::Game { class GridScene : public fggl::scenes::Game {
public: public:
...@@ -38,7 +47,7 @@ namespace demo { ...@@ -38,7 +47,7 @@ namespace demo {
void render(fggl::gfx::Graphics& gfx) override; void render(fggl::gfx::Graphics& gfx) override;
private: private:
fggl::entity::grid::TileSet m_tiles; fggl::entity::grid::TileSet m_tiles;
std::unique_ptr<fggl::entity::grid::Area2D<GRID_SIZE,GRID_SIZE>> m_grid; std::unique_ptr<DemoGrid> m_grid;
}; };
......
...@@ -23,10 +23,13 @@ ...@@ -23,10 +23,13 @@
#include <vector> #include <vector>
#include "fggl/math/types.hpp" #include "fggl/math/types.hpp"
#include "fggl/assets/types.hpp"
#include "fggl/entity/entity.hpp"
namespace fggl::entity::grid { namespace fggl::entity::grid {
using GridPos = math::vec2i; using GridPos = math::vec2i;
constexpr auto ASSET_TILESET = assets::AssetType::make("tileset");
template<typename T, uint32_t width, uint32_t height> template<typename T, uint32_t width, uint32_t height>
struct Grid { struct Grid {
...@@ -147,11 +150,16 @@ namespace fggl::entity::grid { ...@@ -147,11 +150,16 @@ namespace fggl::entity::grid {
} }
} }
EntityManager& entities() {
return m_entities;
}
void neighbours(math::vec2i pos, std::vector<math::vec2i> &neighbours) const; void neighbours(math::vec2i pos, std::vector<math::vec2i> &neighbours) const;
private: private:
TileSet& m_tiles; TileSet& m_tiles;
Grid<uint32_t, width, height> m_floors; Grid<uint32_t, width, height> m_floors;
Grid<WallState, width + 1, height + 1> m_walls; Grid<WallState, width + 1, height + 1> m_walls;
EntityManager m_entities;
}; };
} // namespace fggl::entity::gridworld } // namespace fggl::entity::gridworld
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment