Reworked the way that entities are loaded so they're more dynamic and can be configured without modifying C++code.
This commit is contained in:
parent
1f7214fcd4
commit
9ce4fbd552
6 changed files with 94 additions and 42 deletions
|
@ -168,30 +168,43 @@ void WorldBuilder::generate_map() {
|
|||
}
|
||||
}
|
||||
|
||||
void configure_components(DinkyECS::World &world, DinkyECS::Entity entity, json& entity_data) {
|
||||
for(auto &comp : entity_data["components"]) {
|
||||
json& config = comp["config"];
|
||||
|
||||
if(comp["type"] == "Weapon") {
|
||||
world.set<Weapon>(entity, {config["damage"]});
|
||||
} else if(comp["type"] == "LightSource") {
|
||||
world.set<LightSource>(entity, {config["strength"], config["radius"]});
|
||||
} else if(comp["type"] == "Loot") {
|
||||
world.set<Loot>(entity, {config["amount"]});
|
||||
} else if(comp["type"] == "Tile") {
|
||||
world.set<Tile>(entity, {config["chr"]});
|
||||
} else if(comp["type"] == "EnemyConfig") {
|
||||
world.set<EnemyConfig>(entity, {config["hearing_distance"]});
|
||||
} else if(comp["type"] == "Combat") {
|
||||
world.set<Combat>(entity, {config["hp"], config["damage"]});
|
||||
} else {
|
||||
dbc::sentinel(format("ITEM COMPONENT TYPE MISSING: {}",
|
||||
std::string(comp["type"])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DinkyECS::Entity place_item(DinkyECS::World &world, Map &game_map, std::string name, int in_room) {
|
||||
auto &config = world.get_the<GameConfig>();
|
||||
auto item = world.entity();
|
||||
auto pos = game_map.place_entity(in_room);
|
||||
json item_data = config.items[name];
|
||||
|
||||
world.set<Position>(item, {pos.x+1, pos.y+1});
|
||||
world.set<Tile>(item, {item_data["display"]});
|
||||
|
||||
if(item_data["type"] == "WEAPON") {
|
||||
world.set<InventoryItem>(item, {1, item_data});
|
||||
world.set<Weapon>(item, {20});
|
||||
} else if(item_data["type"] == "LIGHT") {
|
||||
world.set<InventoryItem>(item, {1, item_data});
|
||||
world.set<LightSource>(item, {70,2.0f});
|
||||
} else if(item_data["type"] == "LOOT") {
|
||||
world.set<InventoryItem>(item, {1, item_data});
|
||||
world.set<Loot>(item, {100});
|
||||
} else if(item_data["type"] == "FIXED_LIGHT") {
|
||||
world.set<LightSource>(item, {90,3.0f});
|
||||
} else {
|
||||
dbc::sentinel(format("ITEM MISSING TYPE: {}", name));
|
||||
if(item_data["inventory_count"] > 0) {
|
||||
world.set<InventoryItem>(item, {item_data["inventory_count"], item_data});
|
||||
}
|
||||
|
||||
if(item_data.contains("components")) {
|
||||
configure_components(world, item, item_data);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
|
@ -202,8 +215,10 @@ DinkyECS::Entity place_combatant(DinkyECS::World &world, Map &game_map, std::str
|
|||
auto enemy_data = config.enemies[name];
|
||||
world.set<Position>(enemy, {game_map.place_entity(in_room)});
|
||||
world.set<Motion>(enemy, {0,0});
|
||||
world.set<Tile>(enemy, {enemy_data["display"]});
|
||||
world.set<Combat>(enemy, {enemy_data["hp"], enemy_data["damage"]});
|
||||
|
||||
if(enemy_data.contains("components")) {
|
||||
configure_components(world, enemy, enemy_data);
|
||||
}
|
||||
return enemy;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue