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
44
systems.cpp
44
systems.cpp
|
@ -14,11 +14,11 @@ using namespace Components;
|
|||
#define HEARING_DISTANCE 8
|
||||
|
||||
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.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) {
|
||||
Point out = position.location; // copy
|
||||
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) {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
@ -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) {
|
||||
auto &collider = world.get<spatial_map>();
|
||||
auto &player = world.get<Player>();
|
||||
auto &player_position = world.component<Position>(player.entity);
|
||||
auto &player_motion = world.component<Motion>(player.entity);
|
||||
auto &collider = world.get_the<spatial_map>();
|
||||
auto &player = world.get_the<Player>();
|
||||
auto &player_position = world.get<Position>(player.entity);
|
||||
auto &player_motion = world.get<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
|
||||
if(motion.dx != 0 || motion.dy != 0) {
|
||||
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) {
|
||||
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
|
||||
if(combat.hp <= 0 && !combat.dead) {
|
||||
combat.dead = true;
|
||||
|
@ -87,19 +87,19 @@ void System::death(DinkyECS::World &world) {
|
|||
|
||||
|
||||
void System::combat(DinkyECS::World &world, Player &player) {
|
||||
auto& collider = world.get<spatial_map>();
|
||||
const auto& player_position = world.component<Position>(player.entity);
|
||||
auto& player_combat = world.component<Combat>(player.entity);
|
||||
auto& log = world.get<ActionLog>();
|
||||
auto& sounds = world.get<SoundManager>();
|
||||
auto& collider = world.get_the<spatial_map>();
|
||||
const auto& player_position = world.get<Position>(player.entity);
|
||||
auto& player_combat = world.get<Combat>(player.entity);
|
||||
auto& log = world.get_the<ActionLog>();
|
||||
auto& sounds = world.get_the<SoundManager>();
|
||||
|
||||
// this is guaranteed to not return the given position
|
||||
auto [found, nearby] = collider.neighbors(player_position.location);
|
||||
|
||||
if(found) {
|
||||
for(auto entity : nearby) {
|
||||
auto& enemy_combat = world.component<Combat>(entity);
|
||||
int player_dmg = player_combat.fight(enemy_combat);
|
||||
auto& enemy_combat = world.get<Combat>(entity);
|
||||
int player_dmg = player_combat.attack(enemy_combat);
|
||||
|
||||
if(player_dmg > 0) {
|
||||
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) {
|
||||
int enemy_dmg = enemy_combat.fight(player_combat);
|
||||
int enemy_dmg = enemy_combat.attack(player_combat);
|
||||
|
||||
if(enemy_dmg > 0) {
|
||||
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) {
|
||||
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
|
||||
&& 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);
|
||||
|
@ -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) {
|
||||
const auto& player = world.get<Player>();
|
||||
const auto& player_position = world.component<Position>(player.entity);
|
||||
const auto& player = world.get_the<Player>();
|
||||
const auto& player_position = world.get<Position>(player.entity);
|
||||
Point start = game_map.center_camera(player_position.location, view_x, view_y);
|
||||
Matrix &walls = game_map.walls();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue