Can now mark json/components with std::optional and then they can be null/false to disable them.

master
Zed A. Shaw 5 days ago
parent 243b4c2663
commit 281a7f687a
  1. 4
      assets/bosses.json
  2. 6
      boss_fight_ui.cpp
  3. 26
      components.hpp
  4. 34
      json_mods.hpp

@ -3,7 +3,7 @@
"components": [ "components": [
{"_type": "BossFight", {"_type": "BossFight",
"background": "boss_fight_background", "background": "boss_fight_background",
"stage": "none", "stage": false,
"weapon_sound": "Sword_Hit_2" "weapon_sound": "Sword_Hit_2"
}, },
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false},
@ -16,7 +16,7 @@
"components": [ "components": [
{"_type": "BossFight", {"_type": "BossFight",
"background": "devils_fingers_background", "background": "devils_fingers_background",
"stage": "none", "stage": false,
"weapon_sound": "Sword_Hit_2" "weapon_sound": "Sword_Hit_2"
}, },
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false},

@ -50,14 +50,14 @@ namespace gui {
void BossFightUI::configure_background() { void BossFightUI::configure_background() {
auto& boss = $world->get<components::BossFight>($boss_id); auto& boss = $world->get<components::BossFight>($boss_id);
$boss_has_stage = boss.stage != "none";
$boss_background = textures::get(boss.background); $boss_background = textures::get(boss.background);
$boss_background.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y}); $boss_background.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y});
$status.world().set_the<Background>({$status.$parser}); $status.world().set_the<Background>({$status.$parser});
if($boss_has_stage) { if(boss.stage) {
$boss_stage = textures::get(boss.stage); $boss_has_stage = true;
$boss_stage = textures::get(*boss.stage);
$boss_stage.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y}); $boss_stage.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y});
} }
} }

@ -1,5 +1,4 @@
#pragma once #pragma once
#include "components.hpp"
#include "config.hpp" #include "config.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "dinkyecs.hpp" #include "dinkyecs.hpp"
@ -7,15 +6,10 @@
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <functional> #include <functional>
#include <nlohmann/json.hpp> #include <optional>
#include <nlohmann/json_fwd.hpp>
#include "easings.hpp" #include "easings.hpp"
#include "json_mods.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 { namespace components {
using namespace nlohmann; using namespace nlohmann;
@ -69,7 +63,7 @@ namespace components {
struct BossFight { struct BossFight {
std::string background; std::string background;
std::string stage; std::optional<std::string> stage;
std::string weapon_sound; std::string weapon_sound;
}; };
@ -126,8 +120,16 @@ namespace components {
void step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out); void step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out);
}; };
struct Player {
DinkyECS::Entity entity;
};
template <typename T> struct NameOf; template <typename T> struct NameOf;
using ReflFuncSignature = std::function<void(DinkyECS::World& world, DinkyECS::Entity ent, nlohmann::json &j)>;
using ComponentMap = std::unordered_map<std::string, ReflFuncSignature>;
ENROLL_COMPONENT(Tile, display, foreground, background); ENROLL_COMPONENT(Tile, display, foreground, background);
ENROLL_COMPONENT(BossFight, background, stage, weapon_sound); ENROLL_COMPONENT(BossFight, background, stage, weapon_sound);
ENROLL_COMPONENT(Sprite, name, width, height, scale); ENROLL_COMPONENT(Sprite, name, width, height, scale);
@ -144,12 +146,6 @@ namespace components {
speed, easing, ease_rate, stationary); speed, easing, ease_rate, stationary);
ENROLL_COMPONENT(Sound, attack, death); ENROLL_COMPONENT(Sound, attack, death);
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) { template<typename COMPONENT> COMPONENT convert(nlohmann::json &data) {
COMPONENT result; COMPONENT result;
from_json(data, result); from_json(data, result);

@ -0,0 +1,34 @@
#pragma once
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <optional>
#define ENROLL_COMPONENT(COMPONENT, ...) \
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(COMPONENT, __VA_ARGS__); \
template <> struct NameOf<COMPONENT> { \
static constexpr const char *name = #COMPONENT; \
};
// partial specialization (full specialization works too)
namespace nlohmann {
template <typename T>
struct adl_serializer<std::optional<T>> {
static void to_json(json& j, const std::optional<T>& opt) {
if (opt == std::nullopt) {
j = nullptr;
} else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
static void from_json(const json& j, std::optional<T>& opt) {
if (j.is_null() || j == false) {
opt = std::nullopt;
} else {
opt = std::make_optional<T>(j.template get<T>());
// same as above, but with adl_serializer<T>::from_json
}
}
};
}
Loading…
Cancel
Save