Refactored inventory some so that the UI is not so knowing of the internals.

This commit is contained in:
Zed A. Shaw 2025-02-23 23:57:46 -05:00
parent e0e7a1027c
commit 0878a9e978
8 changed files with 90 additions and 92 deletions

View file

@ -1,17 +1,23 @@
#pragma once
#include "dinkyecs.hpp"
#include "components.hpp"
#include "constants.hpp"
#include "config.hpp"
#include "dinky_components.hpp"
#include "constants.hpp"
#include "dinkyecs.hpp"
#include "point.hpp"
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>
#include <functional>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#define ENROLL_COMPONENT(COMPONENT, ...) \
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(COMPONENT, __VA_ARGS__); \
template <> struct NameOf<COMPONENT> { \
static constexpr const char *name = #COMPONENT; \
};
namespace components {
struct Player {
DinkyECS::Entity entity;
};
using namespace nlohmann;
struct Position {
Point location;
@ -104,7 +110,7 @@ namespace components {
void step(sf::Vector2f& scale_out, sf::IntRect& rect_out);
};
void configure(ComponentMap& component_map);
template <typename T> struct NameOf;
// these need to be here if you're using components::convert outside of components.cpp
ENROLL_COMPONENT(Tile, display, foreground, background);
@ -113,4 +119,42 @@ namespace components {
ENROLL_COMPONENT(LightSource, strength, radius);
ENROLL_COMPONENT(Weapon, damage);
ENROLL_COMPONENT(Loot, amount);
using ReflFuncSignature = std::function<void(DinkyECS::World& world, DinkyECS::Entity ent, nlohmann::json &j)>;
using ComponentMap = std::unordered_map<std::string, ReflFuncSignature>;
struct Player {
DinkyECS::Entity entity;
};
template<typename COMPONENT> COMPONENT convert(nlohmann::json &data) {
COMPONENT result;
from_json(data, result);
return result;
}
template<typename COMPONENT> COMPONENT get(nlohmann::json &data) {
for (auto &i : data["components"]) {
if(i["_type"] == NameOf<COMPONENT>::name) {
COMPONENT result;
from_json(i, result);
return result;
}
}
return {};
}
template <typename COMPONENT> void enroll(ComponentMap &m) {
m[NameOf<COMPONENT>::name] = [](DinkyECS::World& world, DinkyECS::Entity ent, nlohmann::json &j) {
COMPONENT c;
from_json(j, c);
world.set<COMPONENT>(ent, c);
};
}
void configure(ComponentMap& component_map);
void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data);
}