Started working on the loot system which will eventually become the inventory/improved collision system.
This commit is contained in:
parent
c1d43694b0
commit
0a268591c2
5 changed files with 55 additions and 29 deletions
67
systems.cpp
67
systems.cpp
|
@ -7,6 +7,7 @@
|
|||
#include "events.hpp"
|
||||
#include "ftxui/screen/color.hpp"
|
||||
#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor
|
||||
#include "dbc.hpp"
|
||||
|
||||
using std::string;
|
||||
using namespace fmt;
|
||||
|
@ -40,9 +41,13 @@ void System::init_positions(DinkyECS::World &world) {
|
|||
collider.insert(pos.location, ent);
|
||||
}
|
||||
});
|
||||
|
||||
world.query<Position, Loot>([&](const auto &ent, auto &pos, auto &loot) {
|
||||
collider.insert(pos.location, ent);
|
||||
});
|
||||
}
|
||||
|
||||
inline void move_entity(spatial_map &collider, Map &game_map, Position &position, Motion &motion, DinkyECS::Entity ent) {
|
||||
inline bool move_entity(spatial_map &collider, Map &game_map, Position &position, Motion &motion, DinkyECS::Entity ent) {
|
||||
Point move_to = {
|
||||
position.location.x + motion.dx,
|
||||
position.location.y + motion.dy
|
||||
|
@ -50,25 +55,31 @@ inline void move_entity(spatial_map &collider, Map &game_map, Position &position
|
|||
motion = {0,0}; // clear it after getting it
|
||||
|
||||
if(game_map.inmap(move_to.x, move_to.y) &&
|
||||
!game_map.iswall(move_to.x, move_to.y) &&
|
||||
!collider.occupied(move_to))
|
||||
!game_map.iswall(move_to.x, move_to.y))
|
||||
{
|
||||
collider.move(position.location, move_to, ent);
|
||||
position.location = move_to;
|
||||
if(collider.occupied(move_to)) {
|
||||
return true;
|
||||
} else {
|
||||
collider.move(position.location, move_to, ent);
|
||||
position.location = move_to;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void System::motion(DinkyECS::World &world, Map &game_map) {
|
||||
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.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);
|
||||
// if there's a collision
|
||||
if(move_entity(collider, game_map, position, motion, ent)) {
|
||||
// send a collision event?
|
||||
println("hit it!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -99,25 +110,31 @@ void System::combat(DinkyECS::World &world, Player &player) {
|
|||
|
||||
if(found) {
|
||||
for(auto entity : nearby) {
|
||||
auto& enemy_combat = world.get<Combat>(entity);
|
||||
int player_dmg = player_combat.attack(enemy_combat);
|
||||
if(world.has<Combat>(entity)) {
|
||||
auto& enemy_combat = world.get<Combat>(entity);
|
||||
int player_dmg = player_combat.attack(enemy_combat);
|
||||
|
||||
if(player_dmg > 0) {
|
||||
world.send<Events::GUI>(Events::GUI::HIT, entity);
|
||||
} else {
|
||||
world.send<Events::GUI>(Events::GUI::MISS, entity);
|
||||
}
|
||||
|
||||
if(enemy_combat.hp > 0) {
|
||||
int enemy_dmg = enemy_combat.attack(player_combat);
|
||||
|
||||
if(enemy_dmg > 0) {
|
||||
world.send<Events::GUI>(Events::GUI::HIT, player.entity);
|
||||
if(player_dmg > 0) {
|
||||
world.send<Events::GUI>(Events::GUI::HIT, entity);
|
||||
} else {
|
||||
world.send<Events::GUI>(Events::GUI::MISS, player.entity);
|
||||
world.send<Events::GUI>(Events::GUI::MISS, entity);
|
||||
}
|
||||
|
||||
if(enemy_combat.hp > 0) {
|
||||
int enemy_dmg = enemy_combat.attack(player_combat);
|
||||
|
||||
if(enemy_dmg > 0) {
|
||||
world.send<Events::GUI>(Events::GUI::HIT, player.entity);
|
||||
} else {
|
||||
world.send<Events::GUI>(Events::GUI::MISS, player.entity);
|
||||
}
|
||||
} else {
|
||||
world.send<Events::GUI>(Events::GUI::DEAD, entity);
|
||||
}
|
||||
} else if(world.has<Loot>(entity)) {
|
||||
println("YOU FOUND LOOT");
|
||||
} else {
|
||||
world.send<Events::GUI>(Events::GUI::DEAD, entity);
|
||||
println("UNKNOWN COLLISION TYPE {}", entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue