3 #include <BLIB/Cameras/2D/Camera2D.hpp>
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));
24 void Position::update(std::mutex&,
float,
float,
float,
float) {
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;
34 const bl::ecs::Entity e = level(position.position.x, position.position.y);
35 return e == bl::ecs::InvalidEntity ||
39 bl::ecs::Entity
Position::search(
const bl::tmap::Position& start, bl::tmap::Direction dir,
41 bl::tmap::Position spos = start;
43 for (
unsigned int i = 0; i < range; ++i) {
44 const auto oldPos = spos;
46 const bl::ecs::Entity e = get(spos);
47 if (e != bl::ecs::InvalidEntity)
return e;
53 return bl::ecs::InvalidEntity;
61 void Position::observe(
const bl::ecs::event::ComponentAdded<bl::tmap::Position>& event) {
62 get(event.component) =
event.entity;
65 void Position::observe(
const bl::ecs::event::ComponentRemoved<bl::tmap::Position>& event) {
66 get(event.component) = bl::ecs::InvalidEntity;
69 void Position::observe(
const event::MapSwitch& event) {
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);
78 bl::ecs::Entity& Position::get(
const bl::tmap::Position& p) {
79 static bl::ecs::Entity
null = bl::ecs::InvalidEntity;
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);
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;
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);
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;
112 entityMap.emplace_back(
113 entityMap.front().getWidth(), entityMap.front().getHeight(), bl::ecs::InvalidEntity);
Core classes and functionality for both the editor and game.
Empty component to indicate that an entity cannot be moved through.
Fired after an entity begins moving from one position to another.
const bl::tmap::Position previousPosition
The previous position of the entity.
const bl::tmap::Position & position
The current position of the entity.
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....
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...
Position(Systems &owner)
Construct the Position system.
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...
void editorPushLevel()
Called by the editor when a level is added.
bl::ecs::Entity getEntity(const bl::tmap::Position &pos) const
Returns the entity at the given position or InvalidEntity if not found.
void editorPopLevel()
Called by the editor when a level is removed.
bool spaceFree(const bl::tmap::Position &position) const
Returns whether or not a tile is currently occupied.
Owns all primary systems and a reference to the engine.
const bl::engine::Engine & engine() const
Const accessor for the Engine.
World & world()
Modifiable accessor for the world system.
map::Map & activeMap()
Returns a reference to the active map.