Clean up the DinkyECSso that it's easier to understand and more obvious what a fact vs. component is.
This commit is contained in:
parent
4ed06b10b1
commit
143fe7784c
7 changed files with 82 additions and 105 deletions
|
@ -1,7 +1,7 @@
|
||||||
#include "combat.hpp"
|
#include "combat.hpp"
|
||||||
#include "rand.hpp"
|
#include "rand.hpp"
|
||||||
|
|
||||||
int Combat::fight(Combat &target) {
|
int Combat::attack(Combat &target) {
|
||||||
int attack = Random::uniform<int>(0,1);
|
int attack = Random::uniform<int>(0,1);
|
||||||
int my_dmg = 0;
|
int my_dmg = 0;
|
||||||
|
|
||||||
|
|
|
@ -7,5 +7,5 @@ struct Combat {
|
||||||
int damage;
|
int damage;
|
||||||
bool dead;
|
bool dead;
|
||||||
|
|
||||||
int fight(Combat &target);
|
int attack(Combat &target);
|
||||||
};
|
};
|
||||||
|
|
28
dinkyecs.hpp
28
dinkyecs.hpp
|
@ -33,25 +33,25 @@ namespace DinkyECS {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Comp>
|
template <typename Comp>
|
||||||
void set(Comp val) {
|
void set_the(Comp val) {
|
||||||
$facts[std::type_index(typeid(Comp))] = val;
|
$facts[std::type_index(typeid(Comp))] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Comp>
|
template <typename Comp>
|
||||||
Comp &get() {
|
Comp &get_the() {
|
||||||
// use .at to get std::out_of_range if fact not set
|
// use .at to get std::out_of_range if fact not set
|
||||||
std::any &res = $facts.at(std::type_index(typeid(Comp)));
|
std::any &res = $facts.at(std::type_index(typeid(Comp)));
|
||||||
return std::any_cast<Comp&>(res);
|
return std::any_cast<Comp&>(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Comp>
|
template <typename Comp>
|
||||||
void assign(Entity ent, Comp val) {
|
void set(Entity ent, Comp val) {
|
||||||
EntityMap &map = entity_map_for<Comp>();
|
EntityMap &map = entity_map_for<Comp>();
|
||||||
map[ent] = val;
|
map[ent] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Comp>
|
template <typename Comp>
|
||||||
Comp &component(Entity ent) {
|
Comp &get(Entity ent) {
|
||||||
EntityMap &map = entity_map_for<Comp>();
|
EntityMap &map = entity_map_for<Comp>();
|
||||||
// use .at for bounds checking
|
// use .at for bounds checking
|
||||||
std::any &res = map.at(ent);
|
std::any &res = map.at(ent);
|
||||||
|
@ -59,7 +59,7 @@ namespace DinkyECS {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Comp>
|
template<typename Comp>
|
||||||
void system(std::function<void(const Entity&, Comp&)> cb) {
|
void query(std::function<void(const Entity&, Comp&)> cb) {
|
||||||
EntityMap &map = entity_map_for<Comp>();
|
EntityMap &map = entity_map_for<Comp>();
|
||||||
for(auto& [entity, any_comp] : map) {
|
for(auto& [entity, any_comp] : map) {
|
||||||
Comp &res = std::any_cast<Comp&>(any_comp);
|
Comp &res = std::any_cast<Comp&>(any_comp);
|
||||||
|
@ -68,31 +68,17 @@ namespace DinkyECS {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename CompA, typename CompB>
|
template<typename CompA, typename CompB>
|
||||||
void system(std::function<void(const Entity&, CompA&, CompB&)> cb) {
|
void query(std::function<void(const Entity&, CompA&, CompB&)> cb) {
|
||||||
EntityMap &map_a = entity_map_for<CompA>();
|
EntityMap &map_a = entity_map_for<CompA>();
|
||||||
EntityMap &map_b = entity_map_for<CompB>();
|
EntityMap &map_b = entity_map_for<CompB>();
|
||||||
|
|
||||||
for(auto& [entity, any_a] : map_a) {
|
for(auto& [entity, any_a] : map_a) {
|
||||||
if(map_b.contains(entity)) {
|
if(map_b.contains(entity)) {
|
||||||
CompA &res_a = std::any_cast<CompA&>(any_a);
|
CompA &res_a = std::any_cast<CompA&>(any_a);
|
||||||
CompB &res_b = component<CompB>(entity);
|
CompB &res_b = get<CompB>(entity);
|
||||||
cb(entity, res_a, res_b);
|
cb(entity, res_a, res_b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename CompA, typename CompB>
|
|
||||||
std::function<void()> runner(std::function<void(const Entity&, CompA&, CompB&)> cb) {
|
|
||||||
return [&]{
|
|
||||||
system<CompA, CompB>(cb);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename CompA>
|
|
||||||
std::function<void()> runner(std::function<void(const Entity&, CompA&)> cb) {
|
|
||||||
return [&]{
|
|
||||||
system<CompA>(cb);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
56
gui.cpp
56
gui.cpp
|
@ -72,7 +72,7 @@ GUI::GUI() :
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUI::create_renderer() {
|
void GUI::create_renderer() {
|
||||||
auto player = $world.get<Player>();
|
auto player = $world.get_the<Player>();
|
||||||
|
|
||||||
$map_view = Renderer([&] {
|
$map_view = Renderer([&] {
|
||||||
System::draw_map($world, $game_map, $canvas, $view_port.x, $view_port.y);
|
System::draw_map($world, $game_map, $canvas, $view_port.x, $view_port.y);
|
||||||
|
@ -80,8 +80,8 @@ void GUI::create_renderer() {
|
||||||
});
|
});
|
||||||
|
|
||||||
$document = Renderer([&, player]{
|
$document = Renderer([&, player]{
|
||||||
const auto& player_combat = $world.component<Combat>(player.entity);
|
const auto& player_combat = $world.get<Combat>(player.entity);
|
||||||
const auto& log = $world.get<ActionLog>();
|
const auto& log = $world.get_the<ActionLog>();
|
||||||
|
|
||||||
$status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!";
|
$status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!";
|
||||||
|
|
||||||
|
@ -114,8 +114,8 @@ bool GUI::handle_events() {
|
||||||
if(event.type == sf::Event::Closed) {
|
if(event.type == sf::Event::Closed) {
|
||||||
$window.close();
|
$window.close();
|
||||||
} else if(event.type == sf::Event::KeyPressed) {
|
} else if(event.type == sf::Event::KeyPressed) {
|
||||||
auto player = $world.get<Player>();
|
auto player = $world.get_the<Player>();
|
||||||
auto& player_motion = $world.component<Motion>(player.entity);
|
auto& player_motion = $world.get<Motion>(player.entity);
|
||||||
|
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||||
player_motion.dx = -1;
|
player_motion.dx = -1;
|
||||||
|
@ -157,7 +157,7 @@ sf::Sprite &GUI::get_text_sprite(wchar_t tile) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUI::run_systems() {
|
void GUI::run_systems() {
|
||||||
auto player = $world.get<Player>();
|
auto player = $world.get_the<Player>();
|
||||||
System::enemy_pathing($world, $game_map, player);
|
System::enemy_pathing($world, $game_map, player);
|
||||||
System::motion($world, $game_map);
|
System::motion($world, $game_map);
|
||||||
System::combat($world, player);
|
System::combat($world, player);
|
||||||
|
@ -268,38 +268,42 @@ void GUI::shake() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUI::configure_world() {
|
void GUI::configure_world() {
|
||||||
|
SoundManager sounds("./assets");
|
||||||
|
sounds.load("hit", "hit.wav");
|
||||||
|
$world.set_the<SoundManager>(sounds);
|
||||||
|
|
||||||
dbc::check($game_map.room_count() > 1, "not enough rooms in map.");
|
dbc::check($game_map.room_count() > 1, "not enough rooms in map.");
|
||||||
// configure a player as a fact of the world
|
// configure a player as a fact of the world
|
||||||
Player player{$world.entity()};
|
Player player{$world.entity()};
|
||||||
$world.set<Player>(player);
|
$world.set_the<Player>(player);
|
||||||
|
|
||||||
ActionLog log{{"Welcome to the game!"}};
|
ActionLog log{{"Welcome to the game!"}};
|
||||||
$world.set<ActionLog>(log);
|
$world.set_the<ActionLog>(log);
|
||||||
|
|
||||||
spatial_map collider;
|
spatial_map collider;
|
||||||
$world.set<spatial_map>(collider);
|
$world.set_the<spatial_map>(collider);
|
||||||
|
|
||||||
$world.assign<Position>(player.entity, {$game_map.place_entity(0)});
|
$world.set<Position>(player.entity, {$game_map.place_entity(0)});
|
||||||
$world.assign<Motion>(player.entity, {0, 0});
|
$world.set<Motion>(player.entity, {0, 0});
|
||||||
$world.assign<Combat>(player.entity, {100, 10});
|
$world.set<Combat>(player.entity, {100, 10});
|
||||||
$world.assign<Tile>(player.entity, {PLAYER_TILE});
|
$world.set<Tile>(player.entity, {PLAYER_TILE});
|
||||||
|
|
||||||
auto enemy = $world.entity();
|
auto enemy = $world.entity();
|
||||||
$world.assign<Position>(enemy, {$game_map.place_entity(1)});
|
$world.set<Position>(enemy, {$game_map.place_entity(1)});
|
||||||
$world.assign<Motion>(enemy, {0,0});
|
$world.set<Motion>(enemy, {0,0});
|
||||||
$world.assign<Combat>(enemy, {20, 10});
|
$world.set<Combat>(enemy, {20, 10});
|
||||||
$world.assign<Tile>(enemy, {ENEMY_TILE});
|
$world.set<Tile>(enemy, {ENEMY_TILE});
|
||||||
|
|
||||||
auto enemy2 = $world.entity();
|
auto enemy2 = $world.entity();
|
||||||
$world.assign<Position>(enemy2, {$game_map.place_entity(2)});
|
$world.set<Position>(enemy2, {$game_map.place_entity(2)});
|
||||||
$world.assign<Motion>(enemy2, {0,0});
|
$world.set<Motion>(enemy2, {0,0});
|
||||||
$world.assign<Combat>(enemy2, {20, 10});
|
$world.set<Combat>(enemy2, {20, 10});
|
||||||
$world.assign<Tile>(enemy2, {"*"});
|
$world.set<Tile>(enemy2, {"*"});
|
||||||
|
|
||||||
auto gold = $world.entity();
|
auto gold = $world.entity();
|
||||||
$world.assign<Position>(gold, {$game_map.place_entity($game_map.room_count() - 1)});
|
$world.set<Position>(gold, {$game_map.place_entity($game_map.room_count() - 1)});
|
||||||
$world.assign<Treasure>(gold, {100});
|
$world.set<Treasure>(gold, {100});
|
||||||
$world.assign<Tile>(gold, {"$"});
|
$world.set<Tile>(gold, {"$"});
|
||||||
|
|
||||||
System::init_positions($world);
|
System::init_positions($world);
|
||||||
}
|
}
|
||||||
|
@ -314,10 +318,6 @@ void GUI::render_scene() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int GUI::main() {
|
int GUI::main() {
|
||||||
SoundManager sounds("./assets");
|
|
||||||
sounds.load("hit", "hit.wav");
|
|
||||||
$world.set<SoundManager>(sounds);
|
|
||||||
|
|
||||||
configure_world();
|
configure_world();
|
||||||
create_renderer();
|
create_renderer();
|
||||||
run_systems();
|
run_systems();
|
||||||
|
|
|
@ -31,28 +31,28 @@ void configure(DinkyECS::World &world, Entity &test) {
|
||||||
println("---Configuring the base system.");
|
println("---Configuring the base system.");
|
||||||
Entity test2 = world.entity();
|
Entity test2 = world.entity();
|
||||||
|
|
||||||
world.assign<Position>(test, {10,20});
|
world.set<Position>(test, {10,20});
|
||||||
world.assign<Velocity>(test, {1,2});
|
world.set<Velocity>(test, {1,2});
|
||||||
|
|
||||||
world.assign<Position>(test2, {1,1});
|
world.set<Position>(test2, {1,1});
|
||||||
world.assign<Velocity>(test2, {10,20});
|
world.set<Velocity>(test2, {10,20});
|
||||||
|
|
||||||
println("---- Setting up the player as a fact in the system.");
|
println("---- Setting up the player as a fact in the system.");
|
||||||
|
|
||||||
auto player_eid = world.entity();
|
auto player_eid = world.entity();
|
||||||
Player player_info{"Zed", player_eid};
|
Player player_info{"Zed", player_eid};
|
||||||
// just set some player info as a fact with the entity id
|
// just set some player info as a fact with the entity id
|
||||||
world.set<Player>(player_info);
|
world.set_the<Player>(player_info);
|
||||||
|
|
||||||
world.assign<Velocity>(player_eid, {0,0});
|
world.set<Velocity>(player_eid, {0,0});
|
||||||
world.assign<Position>(player_eid, {0,0});
|
world.set<Position>(player_eid, {0,0});
|
||||||
|
|
||||||
auto enemy = world.entity();
|
auto enemy = world.entity();
|
||||||
world.assign<Velocity>(enemy, {0,0});
|
world.set<Velocity>(enemy, {0,0});
|
||||||
world.assign<Position>(enemy, {0,0});
|
world.set<Position>(enemy, {0,0});
|
||||||
|
|
||||||
println("--- Creating facts (singletons)");
|
println("--- Creating facts (singletons)");
|
||||||
world.set<Gravity>({0.9});
|
world.set_the<Gravity>({0.9});
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -61,31 +61,31 @@ int main() {
|
||||||
|
|
||||||
configure(world, test);
|
configure(world, test);
|
||||||
|
|
||||||
Position &pos = world.component<Position>(test);
|
Position &pos = world.get<Position>(test);
|
||||||
println("GOT POS x={}, y={}", pos.x, pos.y);
|
println("GOT POS x={}, y={}", pos.x, pos.y);
|
||||||
|
|
||||||
Velocity &vel = world.component<Velocity>(test);
|
Velocity &vel = world.get<Velocity>(test);
|
||||||
println("GOT VELOCITY x={}, y={}", vel.x, vel.y);
|
println("GOT VELOCITY x={}, y={}", vel.x, vel.y);
|
||||||
|
|
||||||
println("--- Position only system:");
|
println("--- Position only system:");
|
||||||
world.system<Position>([](const auto &ent, auto &pos) {
|
world.query<Position>([](const auto &ent, auto &pos) {
|
||||||
println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y);
|
println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
println("--- Velocity only system:");
|
println("--- Velocity only system:");
|
||||||
world.system<Velocity>([](const auto &, auto &vel) {
|
world.query<Velocity>([](const auto &, auto &vel) {
|
||||||
println("vel.x={}, vel.y={}", vel.x, vel.y);
|
println("vel.x={}, vel.y={}", vel.x, vel.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
println("--- Manually get the velocity in position system:");
|
println("--- Manually get the velocity in position system:");
|
||||||
world.system<Position>([&](const auto &ent, auto &pos) {
|
world.query<Position>([&](const auto &ent, auto &pos) {
|
||||||
Velocity &vel = world.component<Velocity>(ent);
|
Velocity &vel = world.get<Velocity>(ent);
|
||||||
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
println("--- Query only entities with Position and Velocity:");
|
println("--- Query only entities with Position and Velocity:");
|
||||||
world.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||||
Gravity &grav = world.get<Gravity>();
|
Gravity &grav = world.get_the<Gravity>();
|
||||||
println("grav={}, entity={}, vel.x, vel.y, pos.x={}, pos.y={}", grav.level, ent, vel.x, vel.y, pos.x, pos.y);
|
println("grav={}, entity={}, vel.x, vel.y, pos.x={}, pos.y={}", grav.level, ent, vel.x, vel.y, pos.x, pos.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -93,25 +93,15 @@ int main() {
|
||||||
world.remove<Velocity>(test);
|
world.remove<Velocity>(test);
|
||||||
|
|
||||||
println("--- After remove test, should only result in test2:");
|
println("--- After remove test, should only result in test2:");
|
||||||
world.system<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||||
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
println("--- Create a stored system you can save for later.");
|
|
||||||
auto movementSystem = world.runner<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
|
||||||
println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y);
|
|
||||||
});
|
|
||||||
|
|
||||||
movementSystem();
|
|
||||||
|
|
||||||
// how to create an identified entity like the player
|
|
||||||
|
|
||||||
|
|
||||||
// to avoid repeatedly getting the player just make a closure with it
|
// to avoid repeatedly getting the player just make a closure with it
|
||||||
// QUESTION: could I just capture it and not have the double function wrapping?
|
// QUESTION: could I just capture it and not have the double function wrapping?
|
||||||
auto playerVsEnemies = [&]() {
|
auto playerVsEnemies = [&]() {
|
||||||
auto& player = world.get<Player>(); // grabbed it
|
auto& player = world.get_the<Player>(); // grabbed it
|
||||||
world.system<Position>([&](const auto &ent, auto &pos) {
|
world.query<Position>([&](const auto &ent, auto &pos) {
|
||||||
if(player.eid != ent) {
|
if(player.eid != ent) {
|
||||||
println("{} is enemy attacking player {}", ent, player.name);
|
println("{} is enemy attacking player {}", ent, player.name);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -24,7 +24,8 @@ void SoundManager::load(const std::string name, const std::string sound_path) {
|
||||||
|
|
||||||
// set it on the sound and keep in the sound map
|
// set it on the sound and keep in the sound map
|
||||||
pair->sound.setBuffer(pair->buffer);
|
pair->sound.setBuffer(pair->buffer);
|
||||||
pair->sound.setRelativeToListener(true);
|
pair->sound.setRelativeToListener(false);
|
||||||
|
pair->sound.setPosition(0.0f, 0.0f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoundManager::play(const std::string name) {
|
void SoundManager::play(const std::string name) {
|
||||||
|
|
44
systems.cpp
44
systems.cpp
|
@ -14,11 +14,11 @@ using namespace Components;
|
||||||
#define HEARING_DISTANCE 8
|
#define HEARING_DISTANCE 8
|
||||||
|
|
||||||
void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) {
|
void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) {
|
||||||
const auto &player_position = world.component<Position>(player.entity);
|
const auto &player_position = world.get<Position>(player.entity);
|
||||||
game_map.set_target(player_position.location);
|
game_map.set_target(player_position.location);
|
||||||
game_map.make_paths();
|
game_map.make_paths();
|
||||||
|
|
||||||
world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
|
world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
|
||||||
if(ent != player.entity) {
|
if(ent != player.entity) {
|
||||||
Point out = position.location; // copy
|
Point out = position.location; // copy
|
||||||
if(game_map.distance(out) < HEARING_DISTANCE) {
|
if(game_map.distance(out) < HEARING_DISTANCE) {
|
||||||
|
@ -31,9 +31,9 @@ void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player
|
||||||
}
|
}
|
||||||
|
|
||||||
void System::init_positions(DinkyECS::World &world) {
|
void System::init_positions(DinkyECS::World &world) {
|
||||||
auto &collider = world.get<spatial_map>();
|
auto &collider = world.get_the<spatial_map>();
|
||||||
|
|
||||||
world.system<Position>([&](const auto &ent, auto &pos) {
|
world.query<Position>([&](const auto &ent, auto &pos) {
|
||||||
collider.insert(pos.location, ent);
|
collider.insert(pos.location, ent);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -55,13 +55,13 @@ inline void move_entity(spatial_map &collider, Map &game_map, Position &position
|
||||||
}
|
}
|
||||||
|
|
||||||
void System::motion(DinkyECS::World &world, Map &game_map) {
|
void System::motion(DinkyECS::World &world, Map &game_map) {
|
||||||
auto &collider = world.get<spatial_map>();
|
auto &collider = world.get_the<spatial_map>();
|
||||||
auto &player = world.get<Player>();
|
auto &player = world.get_the<Player>();
|
||||||
auto &player_position = world.component<Position>(player.entity);
|
auto &player_position = world.get<Position>(player.entity);
|
||||||
auto &player_motion = world.component<Motion>(player.entity);
|
auto &player_motion = world.get<Motion>(player.entity);
|
||||||
move_entity(collider, game_map, player_position, player_motion, player.entity);
|
move_entity(collider, game_map, player_position, player_motion, player.entity);
|
||||||
|
|
||||||
world.system<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
|
world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
|
||||||
// don't process entities that don't move
|
// don't process entities that don't move
|
||||||
if(motion.dx != 0 || motion.dy != 0) {
|
if(motion.dx != 0 || motion.dy != 0) {
|
||||||
move_entity(collider, game_map, position, motion, ent);
|
move_entity(collider, game_map, position, motion, ent);
|
||||||
|
@ -70,9 +70,9 @@ void System::motion(DinkyECS::World &world, Map &game_map) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void System::death(DinkyECS::World &world) {
|
void System::death(DinkyECS::World &world) {
|
||||||
auto &collider = world.get<spatial_map>();
|
auto &collider = world.get_the<spatial_map>();
|
||||||
|
|
||||||
world.system<Position, Combat>([&](const auto &ent, auto &position, auto &combat) {
|
world.query<Position, Combat>([&](const auto &ent, auto &position, auto &combat) {
|
||||||
// bring out yer dead
|
// bring out yer dead
|
||||||
if(combat.hp <= 0 && !combat.dead) {
|
if(combat.hp <= 0 && !combat.dead) {
|
||||||
combat.dead = true;
|
combat.dead = true;
|
||||||
|
@ -87,19 +87,19 @@ void System::death(DinkyECS::World &world) {
|
||||||
|
|
||||||
|
|
||||||
void System::combat(DinkyECS::World &world, Player &player) {
|
void System::combat(DinkyECS::World &world, Player &player) {
|
||||||
auto& collider = world.get<spatial_map>();
|
auto& collider = world.get_the<spatial_map>();
|
||||||
const auto& player_position = world.component<Position>(player.entity);
|
const auto& player_position = world.get<Position>(player.entity);
|
||||||
auto& player_combat = world.component<Combat>(player.entity);
|
auto& player_combat = world.get<Combat>(player.entity);
|
||||||
auto& log = world.get<ActionLog>();
|
auto& log = world.get_the<ActionLog>();
|
||||||
auto& sounds = world.get<SoundManager>();
|
auto& sounds = world.get_the<SoundManager>();
|
||||||
|
|
||||||
// this is guaranteed to not return the given position
|
// this is guaranteed to not return the given position
|
||||||
auto [found, nearby] = collider.neighbors(player_position.location);
|
auto [found, nearby] = collider.neighbors(player_position.location);
|
||||||
|
|
||||||
if(found) {
|
if(found) {
|
||||||
for(auto entity : nearby) {
|
for(auto entity : nearby) {
|
||||||
auto& enemy_combat = world.component<Combat>(entity);
|
auto& enemy_combat = world.get<Combat>(entity);
|
||||||
int player_dmg = player_combat.fight(enemy_combat);
|
int player_dmg = player_combat.attack(enemy_combat);
|
||||||
|
|
||||||
if(player_dmg > 0) {
|
if(player_dmg > 0) {
|
||||||
log.log(format("You HIT for {} damage, HP left {}.", player_dmg, enemy_combat.hp));
|
log.log(format("You HIT for {} damage, HP left {}.", player_dmg, enemy_combat.hp));
|
||||||
|
@ -109,7 +109,7 @@ void System::combat(DinkyECS::World &world, Player &player) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(enemy_combat.hp > 0) {
|
if(enemy_combat.hp > 0) {
|
||||||
int enemy_dmg = enemy_combat.fight(player_combat);
|
int enemy_dmg = enemy_combat.attack(player_combat);
|
||||||
|
|
||||||
if(enemy_dmg > 0) {
|
if(enemy_dmg > 0) {
|
||||||
sounds.play("hit");
|
sounds.play("hit");
|
||||||
|
@ -125,7 +125,7 @@ void System::combat(DinkyECS::World &world, Player &player) {
|
||||||
};
|
};
|
||||||
|
|
||||||
void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) {
|
void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) {
|
||||||
world.system<Position, Tile>([&](const auto &ent, auto &pos, auto &tile) {
|
world.query<Position, Tile>([&](const auto &ent, auto &pos, auto &tile) {
|
||||||
if(pos.location.x >= cam_orig.x && pos.location.x <= cam_orig.x + view_x
|
if(pos.location.x >= cam_orig.x && pos.location.x <= cam_orig.x + view_x
|
||||||
&& pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) {
|
&& pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) {
|
||||||
Point loc = game_map.map_to_camera(pos.location, cam_orig);
|
Point loc = game_map.map_to_camera(pos.location, cam_orig);
|
||||||
|
@ -136,8 +136,8 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas
|
||||||
}
|
}
|
||||||
|
|
||||||
void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) {
|
void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) {
|
||||||
const auto& player = world.get<Player>();
|
const auto& player = world.get_the<Player>();
|
||||||
const auto& player_position = world.component<Position>(player.entity);
|
const auto& player_position = world.get<Position>(player.entity);
|
||||||
Point start = game_map.center_camera(player_position.location, view_x, view_y);
|
Point start = game_map.center_camera(player_position.location, view_x, view_y);
|
||||||
Matrix &walls = game_map.walls();
|
Matrix &walls = game_map.walls();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue