Cereal works pretty well so I may use it, but there's one more library I want to try first called tser.

main
Zed A. Shaw 4 weeks ago
parent ddf1ba955c
commit 713d400d17
  1. 4
      assets/config.json
  2. 12
      components.hpp
  3. 19
      main.cpp
  4. 1
      map.cpp
  5. 5
      map.hpp
  6. 8
      meson.build
  7. 11
      systems.cpp
  8. 44
      tests/config.cpp
  9. 12
      wraps/cereal.wrap

@ -6,8 +6,8 @@
"ENEMY_TILE": "Ω",
"BG_TILE": "█"
},
"enemies": {
"enemy": {
"HEARING_DISTANCE": 8
},
"player": {

@ -25,4 +25,16 @@ namespace Components {
struct Tile {
std::string chr = "!";
};
struct MapConfig {
std::string WALL_TILE;
std::string FLOOR_TILE;
std::string PLAYER_TILE;
std::string ENEMY_TILE;
std::string BG_TILE;
};
struct EnemyConfig {
int HEARING_DISTANCE;
};
}

@ -17,6 +17,7 @@ using namespace ftxui;
* system.
*/
void configure_world(DinkyECS::World &world, Map &game_map) {
const auto &config = world.get_the<MapConfig>();
// this sets up the gui event system
world.set_the<Events::GUI>(Events::GUI::START);
@ -30,13 +31,13 @@ void configure_world(DinkyECS::World &world, Map &game_map) {
world.set<Position>(player.entity, {game_map.place_entity(0)});
world.set<Motion>(player.entity, {0, 0});
world.set<Combat>(player.entity, {100, 10});
world.set<Tile>(player.entity, {PLAYER_TILE});
world.set<Tile>(player.entity, {config.PLAYER_TILE});
auto enemy = world.entity();
world.set<Position>(enemy, {game_map.place_entity(1)});
world.set<Motion>(enemy, {0,0});
world.set<Combat>(enemy, {20, 10});
world.set<Tile>(enemy, {ENEMY_TILE});
world.set<Tile>(enemy, {config.ENEMY_TILE});
auto enemy2 = world.entity();
world.set<Position>(enemy2, {game_map.place_entity(2)});
@ -57,6 +58,20 @@ int main() {
Config config("./assets/config.json");
world.set_the<Config>(config);
auto map = config["map"];
world.set_the<MapConfig>({
map["WALL_TILE"],
map["FLOOR_TILE"],
map["PLAYER_TILE"],
map["ENEMY_TILE"],
map["BG_TILE"]
});
auto enemy = config["enemy"];
world.set_the<EnemyConfig>({
enemy["HEARING_DISTANCE"]
});
Map game_map(GAME_MAP_X, GAME_MAP_Y);
game_map.generate();

@ -6,7 +6,6 @@
#include "rand.hpp"
#include <utility>
using std::vector, std::pair;
using namespace fmt;

@ -11,11 +11,6 @@
#define INV_SPACE 1
#define WALL_VALUE 1
#define SPACE_VALUE 0
#define WALL_TILE "█"
#define FLOOR_TILE "\u2849"
#define PLAYER_TILE "♣"
#define ENEMY_TILE "Ω"
#define BG_TILE L'█'
struct Room {
size_t x = 0;

@ -8,10 +8,11 @@ ftxui_screen = dependency('ftxui-screen')
ftxui_dom = dependency('ftxui-dom')
ftxui_component = dependency('ftxui-component')
sfml = dependency('sfml')
cereal = dependency('cereal')
dependencies = [catch2, fmt,
ftxui_screen, ftxui_dom, ftxui_component,
json, sfml]
json, sfml, cereal]
runtests = executable('runtests', [
'dbc.cpp',
@ -48,9 +49,4 @@ roguish = executable('roguish', [
],
dependencies: dependencies)
collider = executable('collider', [
'./scratchpad/collider.cpp'
],
dependencies: dependencies)
test('tests', runtests)

@ -13,9 +13,9 @@ using namespace fmt;
using namespace Components;
using ftxui::Color;
#define HEARING_DISTANCE 8
void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) {
// TODO: this will be on each enemy not a global thing
const auto &config = world.get_the<EnemyConfig>();
const auto &player_position = world.get<Position>(player.entity);
game_map.set_target(player_position.location);
game_map.make_paths();
@ -23,7 +23,7 @@ void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player
world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
if(ent != player.entity) {
Point out = position.location; // copy
if(game_map.distance(out) < HEARING_DISTANCE) {
if(game_map.distance(out) < config.HEARING_DISTANCE) {
game_map.neighbors(out, false);
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
}
@ -133,6 +133,7 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas
}
void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) {
const auto& config = world.get_the<MapConfig>();
const auto& player = world.get_the<Player>();
const auto& player_position = world.get<Position>(player.entity);
Point start = game_map.center_camera(player_position.location, view_x, view_y);
@ -143,9 +144,9 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv
for(size_t x = 0; x < end_x; ++x) {
for(size_t y = 0; y < end_y; ++y) {
string tile = walls[start.y+y][start.x+x] == 1 ? WALL_TILE : FLOOR_TILE;
string tile = walls[start.y+y][start.x+x] == 1 ? config.WALL_TILE : config.FLOOR_TILE;
// the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing
if(tile == WALL_TILE) {
if(tile == config.WALL_TILE) {
canvas.DrawText(x * 2, y * 4, tile, Color::RGB(70, 70, 70));
} else {
canvas.DrawText(x * 2, y * 4, tile, Color::RGB(20, 20, 20));

@ -4,6 +4,7 @@
#include "config.hpp"
#include <any>
#include "dinkyecs.hpp"
#include "components.hpp"
using namespace fmt;
using std::string;
@ -60,3 +61,46 @@ TEST_CASE("can go into a world", "[config]") {
Config &cfg = world.get_the<Config>();
REQUIRE(cfg["types"]["NUMBER"] == 1234);
}
#include <cereal/types/unordered_map.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/archives/portable_binary.hpp>
#include <fstream>
struct MyData
{
int x, y, z;
std::string tiles;
template<class Archive>
serialize(Archive &ar) { ar(x, y, z, tiles); }
};
TEST_CASE("test using serialization", "[config]") {
{
std::ofstream os("cereal.json", std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
MyData m1{1,2,3, "\u2849█Ω♣"};
MyData m2{5,6,7, "\u2849█Ω♣"};
MyData m3{8,9,10, "\u2849█Ω♣"};
oarchive(m1, m2, m3);
}
{
std::ifstream is("cereal.json", std::ios::binary);
cereal::JSONInputArchive iarchive(is);
MyData m1, m2, m3;
iarchive(m1, m2, m3);
REQUIRE(m1.tiles == "\u2849█Ω♣");
REQUIRE(m2.tiles == "\u2849█Ω♣");
REQUIRE(m3.tiles == "\u2849█Ω♣");
}
}

@ -0,0 +1,12 @@
[wrap-file]
directory = cereal-1.3.2
source_url = https://github.com/USCiLab/cereal/archive/v1.3.2.tar.gz
source_filename = cereal-1.3.2.tar.gz
source_hash = 16a7ad9b31ba5880dac55d62b5d6f243c3ebc8d46a3514149e56b5e7ea81f85f
patch_filename = cereal_1.3.2-1_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/cereal_1.3.2-1/get_patch
patch_hash = fd2f047a40a0d291c643fdafe4ce743f0eadbef667b6afe43a332e1ba0862603
[provide]
cereal = cereal_dep
Loading…
Cancel
Save