Game now loads random enemies and items into rooms but in rudimentary way. Need to now randomize more of it and make it more robust so only changing the .json is needed to get new effects and enemies.
This commit is contained in:
parent
31e5eb7fce
commit
f2864a62ee
13 changed files with 119 additions and 76 deletions
|
@ -2,8 +2,10 @@
|
|||
#include "rand.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
#include "components.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
using namespace components;
|
||||
|
||||
inline int make_split(Room &cur, bool horiz) {
|
||||
size_t dimension = horiz ? cur.height : cur.width;
|
||||
|
@ -119,7 +121,7 @@ void WorldBuilder::stylize_room(int room, string tile_name, float size) {
|
|||
}
|
||||
}
|
||||
|
||||
void WorldBuilder::generate() {
|
||||
void WorldBuilder::generate_map() {
|
||||
PointList holes;
|
||||
Room root{
|
||||
.x = 0,
|
||||
|
@ -166,6 +168,89 @@ void WorldBuilder::generate() {
|
|||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
DinkyECS::Entity place_combatant(DinkyECS::World &world, Map &game_map, std::string name, int in_room) {
|
||||
auto &config = world.get_the<GameConfig>();
|
||||
auto enemy = world.entity();
|
||||
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"]});
|
||||
return enemy;
|
||||
}
|
||||
|
||||
void WorldBuilder::place_entities(DinkyECS::World &world) {
|
||||
auto &config = world.get_the<GameConfig>();
|
||||
// configure a player as a fact of the world
|
||||
|
||||
auto player_ent = place_combatant(world, $map, "PLAYER_TILE", 0);
|
||||
// configure player in the world
|
||||
Player player{player_ent};
|
||||
world.set_the<Player>(player);
|
||||
world.set<Combat>(player.entity, {100, 10});
|
||||
world.set<LightSource>(player.entity, {50,1.0});
|
||||
world.set<Inventory>(player.entity, {5});
|
||||
|
||||
{
|
||||
std::vector<std::string> keys;
|
||||
for(auto &el : config.items.json().items()) {
|
||||
keys.push_back(el.key());
|
||||
}
|
||||
|
||||
for(size_t room_num = 1; room_num < $map.room_count(); room_num++) {
|
||||
std::string key = keys[room_num % keys.size()];
|
||||
place_item(world, $map, key, room_num);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<std::string> keys;
|
||||
for(auto &el : config.enemies.json().items()) {
|
||||
keys.push_back(el.key());
|
||||
}
|
||||
|
||||
for(size_t room_num = $map.room_count() - 1; room_num > 0; room_num--) {
|
||||
if(room_num % 2 == 0) {
|
||||
std::string key = keys[room_num % keys.size()];
|
||||
place_combatant(world, $map, key, room_num);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldBuilder::generate(DinkyECS::World &world) {
|
||||
generate_map();
|
||||
place_entities(world);
|
||||
}
|
||||
|
||||
void WorldBuilder::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
|
||||
$map.INVARIANT();
|
||||
dbc::pre("y out of bounds", origin_y + h < $map.$height);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue