Using an event for the device interaction is better. If I get to where there's tons of devices then I'll rethink it but right now this is less convoluted.
This commit is contained in:
parent
8defc0bedf
commit
e63a8dd920
8 changed files with 46 additions and 45 deletions
|
@ -8,6 +8,6 @@
|
|||
"worldgen": {
|
||||
"enemy_probability": 20,
|
||||
"empty_room_probability": 20,
|
||||
"device_probability": 20
|
||||
"device_probability": 100
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -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<Weapon>(entity, {config["damage"]});
|
||||
} else if(comp["type"] == "LightSource") {
|
||||
} else if(comp_type == "LightSource") {
|
||||
world.set<LightSource>(entity, {config["strength"], config["radius"]});
|
||||
} else if(comp["type"] == "Loot") {
|
||||
} else if(comp_type == "Loot") {
|
||||
world.set<Loot>(entity, {config["amount"]});
|
||||
} else if(comp["type"] == "Tile") {
|
||||
} else if(comp_type == "Tile") {
|
||||
world.set<Tile>(entity, {config["chr"]});
|
||||
} else if(comp["type"] == "EnemyConfig") {
|
||||
} else if(comp_type == "EnemyConfig") {
|
||||
world.set<EnemyConfig>(entity, {config["hearing_distance"]});
|
||||
} else if(comp["type"] == "Combat") {
|
||||
} else if(comp_type == "Combat") {
|
||||
world.set<Combat>(entity, {config["hp"], config["damage"]});
|
||||
} else if(comp["type"] == "Curative") {
|
||||
} else if(comp_type == "Curative") {
|
||||
world.set<Curative>(entity, {config["hp"]});
|
||||
} else if(comp["type"] == "Motion") {
|
||||
} else if(comp_type == "Motion") {
|
||||
world.set<Motion>(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<Device>(entity, device);
|
||||
} else {
|
||||
dbc::sentinel(fmt::format("ITEM COMPONENT TYPE MISSING: {}",
|
||||
std::string(comp["type"])));
|
||||
std::string(comp_type)));
|
||||
}
|
||||
|
||||
// json config variable dies
|
||||
|
|
25
devices.cpp
25
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>(Events::GUI::STAIRS, player_ent, {});
|
||||
}
|
||||
|
||||
void StairsUp(DinkyECS::Entity player_ent, json &, DinkyECS::World &world) {
|
||||
|
||||
world.send<Events::GUI>(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
10
devices.hpp
10
devices.hpp
|
@ -6,14 +6,10 @@
|
|||
namespace components {
|
||||
using namespace nlohmann;
|
||||
|
||||
typedef std::function<void(DinkyECS::Entity ent, json &config, DinkyECS::World &world)> 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<Action> actions;
|
||||
void hit(DinkyECS::Entity ent, DinkyECS::World &world);
|
||||
std::vector<int> events;
|
||||
|
||||
void configure_events(json &event_names);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Events {
|
||||
enum GUI {
|
||||
START, COMBAT, LOOT, DEATH, STAIRS
|
||||
START, COMBAT, LOOT, DEATH, STAIRS_UP, STAIRS_DOWN
|
||||
};
|
||||
|
||||
struct Combat {
|
||||
|
|
9
gui.cpp
9
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<Device&>(data);
|
||||
$status_ui.log(format("Up stairs has test {}.",
|
||||
(bool)device.config["test"]));
|
||||
} break;
|
||||
default:
|
||||
$status_ui.log(format("INVALID EVENT! {},{}", evt, entity));
|
||||
|
|
|
@ -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<Device>(item);
|
||||
|
||||
for(int event : device.events) {
|
||||
world.send<Events::GUI>((Events::GUI)event, actor, device);
|
||||
}
|
||||
|
||||
println("entity {} INTERACTED WITH DEVICE {}", actor, item);
|
||||
device.hit(actor, world);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue