Commit 9341e972 authored by Georg Schäfer's avatar Georg Schäfer
Browse files

Add basic level loading.

parent c6255c31
Pipeline #3221 passed with stage
in 2 minutes and 23 seconds
tiles: = tilesets/*.json
level: = level/*.lvl
dummy
##############################
###---------##################
###| |##################
###| p |##################
###| |##################
###| x |##################
###---- ----##################
######| |#####################
######| |#####################
######| |#####################
###---- -----------###########
###| |###########
###| |###########
###| y |###########
###----------------###########
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
{
"tile_size": 1,
"tiles": {
"|": {
"blueprint": "dummy_wall_vertical",
"solid": true
},
"-": {
"blueprint": "dummy_wall_horizontal",
"solid": true
},
" ": {
"blueprint": "dummy_floor",
"solid": false
},
"#": {
"blueprint": "dummy_ceiling",
"solid": true
}
}
}
#include "game_screen.hpp"
#include "game_engine.hpp"
#include "level/level_system.hpp"
#include "meta_system.hpp"
#include <mirrage/renderer/animation_comp.hpp>
......@@ -43,6 +44,8 @@ namespace phase_shifter {
{
_meta_system.shrink_to_fit();
_mailbox.enable();
_meta_system.level_system().load("dummy");
}
void Game_screen::_on_leave(mirrage::util::maybe<Screen&>) { _mailbox.disable(); }
......
#include "level.hpp"
#include <sstream>
namespace phase_shifter::level {
Level::Level(Tileset_ptr tileset, vector_2d<char> tiles) : _tileset(tileset), _tiles(tiles) {}
} // namespace phase_shifter::level
namespace mirrage::asset {
auto Loader<phase_shifter::level::Level>::load(istream in) -> async::task<phase_shifter::level::Level>
{
std::string tileset_name;
in >> tileset_name;
in.ignore();
auto tileset = in.manager().load<phase_shifter::level::Tileset>(AID("tiles:" + tileset_name));
auto all_loaded = async::when_all(tileset.internal_task());
using Task_type = decltype(all_loaded)::result_type;
auto lines = in.lines();
phase_shifter::level::Level::vector_2d<char> tiles;
for(auto&& line : lines) {
std::vector<char> row;
for(auto&& key : line) {
row.emplace_back(key);
}
tiles.emplace_back(row);
}
return all_loaded.then(
[=](const Task_type&) mutable { return phase_shifter::level::Level(tileset, tiles); });
}
} // namespace mirrage::asset
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <mirrage/asset/asset_manager.hpp>
namespace phase_shifter::level {
struct Tile {
std::string blueprint;
bool solid;
};
sf2_structDef(Tile, blueprint, solid);
struct Tileset {
float tile_size;
std::unordered_map<std::string, Tile> tiles;
};
sf2_structDef(Tileset, tile_size, tiles);
using Tileset_ptr = mirrage::asset::Ptr<Tileset>;
class Level {
friend class Level_system;
public:
template <typename T>
using vector_2d = std::vector<std::vector<T>>;
Level(Tileset_ptr tileset, vector_2d<char> tiles);
private:
Tileset_ptr _tileset;
vector_2d<char> _tiles;
};
using Level_ptr = mirrage::asset::Ptr<Level>;
} // namespace phase_shifter::level
namespace mirrage::asset {
template <>
struct Loader<phase_shifter::level::Level> {
public:
auto load(istream in) -> async::task<phase_shifter::level::Level>;
void save(ostream, const phase_shifter::level::Level&)
{
MIRRAGE_FAIL("Save of levels is not supported!");
}
};
} // namespace mirrage::asset
#include "level_system.hpp"
#include <mirrage/ecs/ecs.hpp>
namespace phase_shifter::level {
Level_system::Level_system(mirrage::ecs::Entity_manager& entities, mirrage::asset::Asset_manager& assets)
: _entities(entities), _assets(assets)
{
}
auto Level_system::load(const std::string& name) -> void
{
_current_level = _assets.load<Level>(mirrage::asset::AID("level:" + name));
auto& level = _current_level.get_blocking();
auto& tiles = level._tileset->tiles;
auto offset = level._tileset->tile_size / 2.f;
glm::vec3 position;
for(auto&& row : level._tiles) {
for(auto&& tile_key : row) {
if(auto tile_it = tiles.find(std::string(1, tile_key)); tile_it != tiles.end()) {
auto& tile = tile_it->second;
_entities.entity_builder(tile.blueprint).position(position).create();
position.x += offset;
}
}
position.z += offset;
}
}
} // namespace phase_shifter::level
#pragma once
#include "level.hpp"
namespace mirrage::ecs {
class Entity_manager;
}
namespace mirrage::asset {
class Asset_manager;
}
namespace phase_shifter::level {
class Level_system {
public:
Level_system(mirrage::ecs::Entity_manager& entities, mirrage::asset::Asset_manager& assets);
auto load(const std::string& name) -> void;
private:
mirrage::ecs::Entity_manager& _entities;
mirrage::asset::Asset_manager& _assets;
Level_ptr _current_level;
};
} // namespace phase_shifter::level
......@@ -4,6 +4,7 @@
#include "game_engine.hpp"
#include "gameplay/beat_system.hpp"
#include "helper/attachment_system.hpp"
#include "level/level_system.hpp"
#include "ui/hud_system.hpp"
#include <context.hpp>
......@@ -27,6 +28,7 @@ namespace phase_shifter {
, _beat_system(std::make_unique<gameplay::Beat_system>())
, _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities))
, _level_system(std::make_unique<level::Level_system>(_entities, engine.assets()))
{
_entities.register_component_type<ecs::components::Transform_comp>();
......
......@@ -28,6 +28,9 @@ namespace phase_shifter {
namespace ui {
class Hud_system;
}
namespace level {
class Level_system;
}
class Meta_system {
public:
......@@ -44,6 +47,7 @@ namespace phase_shifter {
auto entities() noexcept -> auto& { return _entities; }
auto renderer() noexcept -> auto& { return *_renderer; }
auto level_system() noexcept -> auto& { return *_level_system; }
private:
mirrage::ecs::Entity_manager _entities;
......@@ -54,6 +58,7 @@ namespace phase_shifter {
std::unique_ptr<gameplay::Beat_system> _beat_system;
std::unique_ptr<helper::Attachment_system> _attachment_system;
std::unique_ptr<ui::Hud_system> _hud_system;
std::unique_ptr<level::Level_system> _level_system;
// TODO: add systems here
mirrage::util::Console_command_container _commands;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment