Peoplemon  0.1.0
Peoplemon 3 game source documentation
Position.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Cameras/2D/Camera2D.hpp>
5 #include <Core/Properties.hpp>
7 #include <cmath>
8 
9 namespace core
10 {
11 namespace system
12 {
14 : owner(owner) {}
15 
16 void Position::init(bl::engine::Engine&) {
17  bl::event::Dispatcher::subscribe(this);
18  owner.engine().ecs().getAllComponents<bl::tmap::Position>().forEach(
19  [this](bl::ecs::Entity ent, bl::tmap::Position& pos) {
20  observe(bl::ecs::event::ComponentAdded<bl::tmap::Position>(ent, pos));
21  });
22 }
23 
24 void Position::update(std::mutex&, float, float, float, float) {
25  // noop
26 }
27 
28 bool Position::spaceFree(const bl::tmap::Position& position) const {
29  if (position.level >= entityMap.size()) return false;
30  const auto& level = entityMap[position.level];
31  if (static_cast<unsigned>(position.position.x) >= level.getWidth()) return false;
32  if (static_cast<unsigned>(position.position.y) >= level.getHeight()) return false;
33 
34  const bl::ecs::Entity e = level(position.position.x, position.position.y);
35  return e == bl::ecs::InvalidEntity ||
36  !owner.engine().ecs().hasComponent<component::Collision>(e);
37 }
38 
39 bl::ecs::Entity Position::search(const bl::tmap::Position& start, bl::tmap::Direction dir,
40  unsigned int range) {
41  bl::tmap::Position spos = start;
42  spos.direction = dir;
43  for (unsigned int i = 0; i < range; ++i) {
44  const auto oldPos = spos;
45  spos = owner.world().activeMap().adjacentTile(spos, dir);
46  const bl::ecs::Entity e = get(spos);
47  if (e != bl::ecs::InvalidEntity) return e;
48 
49  // break after check to allow entity on collision to be found
50  if (!owner.world().activeMap().movePossible(oldPos, dir)) break;
51  }
52 
53  return bl::ecs::InvalidEntity;
54 }
55 
56 void Position::observe(const event::EntityMoved& event) {
57  get(event.previousPosition) = bl::ecs::InvalidEntity;
58  get(event.position) = event.entity;
59 }
60 
61 void Position::observe(const bl::ecs::event::ComponentAdded<bl::tmap::Position>& event) {
62  get(event.component) = event.entity;
63 }
64 
65 void Position::observe(const bl::ecs::event::ComponentRemoved<bl::tmap::Position>& event) {
66  get(event.component) = bl::ecs::InvalidEntity;
67 }
68 
69 void Position::observe(const event::MapSwitch& event) {
70  entityMap.clear();
71  entityMap.reserve(event.map.levelCount());
72  for (std::uint8_t i = 0; i < event.map.levelCount(); ++i) {
73  entityMap.emplace_back(
74  event.map.sizeTiles().x, event.map.sizeTiles().y, bl::ecs::InvalidEntity);
75  }
76 }
77 
78 bl::ecs::Entity& Position::get(const bl::tmap::Position& p) {
79  static bl::ecs::Entity null = bl::ecs::InvalidEntity;
80 
81  if (p.level < entityMap.size()) {
82  if (p.position.x >= 0 && p.position.x < static_cast<int>(entityMap[p.level].getWidth())) {
83  if (p.position.y >= 0 &&
84  p.position.y < static_cast<int>(entityMap[p.level].getHeight())) {
85  return entityMap[p.level](p.position.x, p.position.y);
86  }
87  }
88  }
89 
90  BL_LOG_WARN << "Out of bounds entity check at (" << static_cast<int>(p.level) << ", "
91  << p.position.x << ", " << p.position.y << ")";
92  null = bl::ecs::InvalidEntity;
93  return null;
94 }
95 
96 bl::ecs::Entity Position::getEntity(const bl::tmap::Position& p) const {
97  if (p.level < entityMap.size()) {
98  if (p.position.x >= 0 && p.position.x < static_cast<int>(entityMap[p.level].getWidth())) {
99  if (p.position.y >= 0 &&
100  p.position.y < static_cast<int>(entityMap[p.level].getHeight())) {
101  return entityMap.at(p.level)(p.position.x, p.position.y);
102  }
103  }
104  }
105 
106  BL_LOG_WARN << "Out of bounds entity check at (" << static_cast<int>(p.level) << ", "
107  << p.position.x << ", " << p.position.y << ")";
108  return bl::ecs::InvalidEntity;
109 }
110 
112  entityMap.emplace_back(
113  entityMap.front().getWidth(), entityMap.front().getHeight(), bl::ecs::InvalidEntity);
114 }
115 
116 void Position::editorPopLevel() { entityMap.pop_back(); }
117 
118 } // namespace system
119 } // namespace core
Core classes and functionality for both the editor and game.
Empty component to indicate that an entity cannot be moved through.
Definition: Collision.hpp:14
Fired after an entity begins moving from one position to another.
Definition: EntityMoved.hpp:18
const bl::tmap::Position previousPosition
The previous position of the entity.
Definition: EntityMoved.hpp:23
const bl::tmap::Position & position
The current position of the entity.
Definition: EntityMoved.hpp:26
bl::tmap::Position adjacentTile(const bl::tmap::Position &pos, bl::tmap::Direction dir) const
Returns the adjacent position to the given position when moving in the given direction....
Definition: Map.cpp:276
bool movePossible(const bl::tmap::Position &position, bl::tmap::Direction dir) const
Returns whether or not a particular movement is possible. Does not take into account entities blockin...
Definition: Map.cpp:329
Position(Systems &owner)
Construct the Position system.
Definition: Position.cpp:13
bl::ecs::Entity search(const bl::tmap::Position &start, bl::tmap::Direction dir, unsigned int range)
Searches for an entity from the starting position in the given direction for the given number of spac...
Definition: Position.cpp:39
void editorPushLevel()
Called by the editor when a level is added.
Definition: Position.cpp:111
bl::ecs::Entity getEntity(const bl::tmap::Position &pos) const
Returns the entity at the given position or InvalidEntity if not found.
Definition: Position.cpp:96
void editorPopLevel()
Called by the editor when a level is removed.
Definition: Position.cpp:116
bool spaceFree(const bl::tmap::Position &position) const
Returns whether or not a tile is currently occupied.
Definition: Position.cpp:28
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Definition: Systems.cpp:35
World & world()
Modifiable accessor for the world system.
Definition: Systems.cpp:43
map::Map & activeMap()
Returns a reference to the active map.
Definition: World.cpp:84