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
|
@ -22,9 +22,9 @@ namespace components {
|
|||
DEFINE_SERIALIZABLE(Motion, dx, dy);
|
||||
};
|
||||
|
||||
struct Treasure {
|
||||
struct Loot {
|
||||
int amount;
|
||||
DEFINE_SERIALIZABLE(Treasure, amount);
|
||||
DEFINE_SERIALIZABLE(Loot, amount);
|
||||
};
|
||||
|
||||
struct Tile {
|
||||
|
|
|
@ -74,6 +74,13 @@ namespace DinkyECS {
|
|||
return std::any_cast<Comp&>(res);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has(Entity ent) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
// use .at for bounds checking
|
||||
return map.contains(ent);
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
void query(std::function<void(const Entity&, Comp&)> cb) {
|
||||
EntityMap &map = entity_map_for<Comp>();
|
||||
|
|
4
main.cpp
4
main.cpp
|
@ -41,8 +41,8 @@ void configure_world(DinkyECS::World &world, Map &game_map) {
|
|||
world.set<Tile>(enemy2, {"*"});
|
||||
|
||||
auto gold = world.entity();
|
||||
world.set<Position>(gold, {game_map.place_entity(game_map.room_count() - 1)});
|
||||
world.set<Treasure>(gold, {100});
|
||||
world.set<Position>(gold, {game_map.place_entity(3)});
|
||||
world.set<Loot>(gold, {100});
|
||||
world.set<Tile>(gold, {"$"});
|
||||
}
|
||||
|
||||
|
|
33
systems.cpp
33
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))
|
||||
{
|
||||
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,6 +110,7 @@ void System::combat(DinkyECS::World &world, Player &player) {
|
|||
|
||||
if(found) {
|
||||
for(auto entity : nearby) {
|
||||
if(world.has<Combat>(entity)) {
|
||||
auto& enemy_combat = world.get<Combat>(entity);
|
||||
int player_dmg = player_combat.attack(enemy_combat);
|
||||
|
||||
|
@ -119,6 +131,11 @@ void System::combat(DinkyECS::World &world, Player &player) {
|
|||
} else {
|
||||
world.send<Events::GUI>(Events::GUI::DEAD, entity);
|
||||
}
|
||||
} else if(world.has<Loot>(entity)) {
|
||||
println("YOU FOUND LOOT");
|
||||
} else {
|
||||
println("UNKNOWN COLLISION TYPE {}", entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -111,8 +111,10 @@ TEST_CASE("confirm ECS system works", "[ecs]") {
|
|||
});
|
||||
|
||||
// now remove Velocity
|
||||
REQUIRE(world.has<Velocity>(test));
|
||||
world.remove<Velocity>(test);
|
||||
REQUIRE_THROWS(world.get<Velocity>(test));
|
||||
REQUIRE(!world.has<Velocity>(test));
|
||||
|
||||
println("--- After remove test, should only result in test2:");
|
||||
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue