diff --git a/assets/config.json b/assets/config.json index 655aba2..055ba27 100644 --- a/assets/config.json +++ b/assets/config.json @@ -8,6 +8,6 @@ "worldgen": { "enemy_probability": 20, "empty_room_probability": 20, - "device_probability": 20 + "device_probability": 100 } } diff --git a/assets/devices.json b/assets/devices.json index 57de267..aa01b7d 100644 --- a/assets/devices.json +++ b/assets/devices.json @@ -9,7 +9,7 @@ "components": [ {"type": "Tile", "config": {"chr": "\u2ac5"}}, {"type": "Device", - "config": {}, "actions": ["StairsDown"] + "config": {"test": true}, "events": ["Events::GUI::STAIRS_DOWN"] } ] }, @@ -23,7 +23,7 @@ "components": [ {"type": "Tile", "config": {"chr": "\u2259"}}, {"type": "Device", - "config": {}, "actions": ["StairsUp"] + "config": {"test": true}, "events": ["Events::GUI::STAIRS_UP"] } ] } diff --git a/components.cpp b/components.cpp index 8514ac4..c5e3893 100644 --- a/components.cpp +++ b/components.cpp @@ -4,38 +4,31 @@ namespace components { void configure(DinkyECS::World &world, DinkyECS::Entity entity, json& entity_data) { for(auto &comp : entity_data["components"]) { json& config = comp["config"]; + const string comp_type = comp["type"]; - if(comp["type"] == "Weapon") { + if(comp_type == "Weapon") { world.set(entity, {config["damage"]}); - } else if(comp["type"] == "LightSource") { + } else if(comp_type == "LightSource") { world.set(entity, {config["strength"], config["radius"]}); - } else if(comp["type"] == "Loot") { + } else if(comp_type == "Loot") { world.set(entity, {config["amount"]}); - } else if(comp["type"] == "Tile") { + } else if(comp_type == "Tile") { world.set(entity, {config["chr"]}); - } else if(comp["type"] == "EnemyConfig") { + } else if(comp_type == "EnemyConfig") { world.set(entity, {config["hearing_distance"]}); - } else if(comp["type"] == "Combat") { + } else if(comp_type == "Combat") { world.set(entity, {config["hp"], config["damage"]}); - } else if(comp["type"] == "Curative") { + } else if(comp_type == "Curative") { world.set(entity, {config["hp"]}); - } else if(comp["type"] == "Motion") { + } else if(comp_type == "Motion") { world.set(entity, {config["dx"], config["dy"], config["random"]}); - } else if(comp["type"] == "Device") { - Device device{.config=config, .actions={}}; - - for(auto name : comp["actions"]) { - if(name == "StairsUp") { - device.actions.push_back(StairsUp); - } else if(name == "StairsDown") { - device.actions.push_back(StairsUp); - } - } - + } else if(comp_type == "Device") { + Device device{.config=config, .events={}}; + device.configure_events(comp["events"]); world.set(entity, device); } else { dbc::sentinel(fmt::format("ITEM COMPONENT TYPE MISSING: {}", - std::string(comp["type"]))); + std::string(comp_type))); } // json config variable dies diff --git a/devices.cpp b/devices.cpp index ea4442f..796781e 100644 --- a/devices.cpp +++ b/devices.cpp @@ -1,19 +1,22 @@ #include "devices.hpp" #include "events.hpp" +#include "dbc.hpp" namespace components { - void StairsDown(DinkyECS::Entity player_ent, json &, DinkyECS::World &world) { - world.send(Events::GUI::STAIRS, player_ent, {}); - } - - void StairsUp(DinkyECS::Entity player_ent, json &, DinkyECS::World &world) { - - world.send(Events::GUI::STAIRS, player_ent, {}); - } - void Device::hit(DinkyECS::Entity ent, DinkyECS::World &world) { - for(auto& action : actions) { - action(ent, config, world); + /* + * Note: This should go away or at least the event names to + * numbers should probably be automatically created. + */ + void Device::configure_events(json &event_names) { + for(string name : event_names) { + if(name == "Events::GUI::STAIRS_DOWN") { + events.push_back(Events::GUI::STAIRS_DOWN); + } else if(name == "Events::GUI::STAIRS_UP") { + events.push_back(Events::GUI::STAIRS_UP); + } else { + dbc::sentinel(fmt::format("Unknown device event {}", name)); + } } } } diff --git a/devices.hpp b/devices.hpp index 8ab6088..4df344f 100644 --- a/devices.hpp +++ b/devices.hpp @@ -6,14 +6,10 @@ namespace components { using namespace nlohmann; - typedef std::function Action; - - void StairsDown(DinkyECS::Entity player_ent, json &data, DinkyECS::World &world); - void StairsUp(DinkyECS::Entity player_ent, json &data, DinkyECS::World &world); - struct Device { json config; - std::vector actions; - void hit(DinkyECS::Entity ent, DinkyECS::World &world); + std::vector events; + + void configure_events(json &event_names); }; } diff --git a/events.hpp b/events.hpp index dccd7eb..f15dfff 100644 --- a/events.hpp +++ b/events.hpp @@ -2,7 +2,7 @@ namespace Events { enum GUI { - START, COMBAT, LOOT, DEATH, STAIRS + START, COMBAT, LOOT, DEATH, STAIRS_UP, STAIRS_DOWN }; struct Combat { diff --git a/gui.cpp b/gui.cpp index c995f8a..a459029 100644 --- a/gui.cpp +++ b/gui.cpp @@ -262,8 +262,13 @@ void GUI::handle_world_events() { } break; - case eGUI::STAIRS: { - $status_ui.log("You can go down stairs!"); + case eGUI::STAIRS_UP: { + $status_ui.log("You can't go back, only forward."); + } break; + case eGUI::STAIRS_DOWN: { + auto& device = std::any_cast(data); + $status_ui.log(format("Up stairs has test {}.", + (bool)device.config["test"])); } break; default: $status_ui.log(format("INVALID EVENT! {},{}", evt, entity)); diff --git a/systems.cpp b/systems.cpp index c98435b..c91a825 100644 --- a/systems.cpp +++ b/systems.cpp @@ -217,6 +217,10 @@ void System::pickup(DinkyECS::World &world, DinkyECS::Entity actor, DinkyECS::En void System::device(DinkyECS::World &world, DinkyECS::Entity actor, DinkyECS::Entity item) { auto& device = world.get(item); + + for(int event : device.events) { + world.send((Events::GUI)event, actor, device); + } + println("entity {} INTERACTED WITH DEVICE {}", actor, item); - device.hit(actor, world); }