Peoplemon  0.1.0
Peoplemon 3 game source documentation
Movement.cpp
Go to the documentation of this file.
2 
4 #include <Core/Properties.hpp>
6 
7 namespace core
8 {
9 namespace system
10 {
12 : owner(owner) {}
13 
14 void Movement::init(bl::engine::Engine&) {
15  jumpSound = bl::audio::AudioSystem::getOrLoadSound(
16  bl::util::FileUtil::joinPath(Properties::SoundPath(), "World/jump.wav"));
17 }
18 
19 bool Movement::makeMovable(bl::ecs::Entity e, float sp, float fsp) {
20  if (owner.engine().ecs().hasComponent<component::Movable>(e)) return false;
21  bl::tmap::Position* pos = owner.engine().ecs().getComponent<bl::tmap::Position>(e);
22  if (!pos) return false;
23  owner.engine().ecs().addComponent<component::Movable>(e, {*pos, sp, fsp});
24  return true;
25 }
26 
27 bool Movement::moveEntity(bl::ecs::Entity e, bl::tmap::Direction dir, bool fast) {
28  component::Movable* mv = owner.engine().ecs().getComponent<component::Movable>(e);
29  if (mv != nullptr) {
30  bl::tmap::Position& pos = mv->position;
31  if (!mv->moving()) {
32  bl::tmap::Position npos = owner.world().activeMap().adjacentTile(pos, dir);
33  if (npos.position == pos.position) {
34  const event::EntityRotated event(e, npos.direction, pos.direction);
35  pos.direction = npos.direction;
36  bl::event::Dispatcher::dispatch<event::EntityRotated>(event);
37  return true;
38  }
39  if (!owner.world().activeMap().contains(npos)) return false;
40  if (!owner.position().spaceFree(npos)) return false;
41  if (!owner.world().activeMap().movePossible(pos, dir)) return false;
42 
43  const bool isHop = owner.world().activeMap().isLedgeHop(pos, dir);
44  if (isHop) {
45  bl::audio::AudioSystem::playOrRestartSound(jumpSound);
46  npos.position.y += 1;
47  }
48 
49  std::swap(pos, npos);
50  mv->move(dir, fast, isHop);
51  bl::event::Dispatcher::dispatch<event::EntityMoved>({e, npos, pos, fast});
52  return true;
53  }
54  }
55  return false;
56 }
57 
58 void Movement::update(std::mutex&, float dt, float, float, float) {
59  bl::engine::Engine& engine = owner.engine();
60 
61  engine.ecs().getAllComponents<component::Movable>().forEach(
62  [dt, this](bl::ecs::Entity ent, component::Movable& mv) { mv.update(ent, owner, dt); });
63 }
64 
65 } // namespace system
66 } // namespace core
Core classes and functionality for both the editor and game.
Adding this component to an entity will allow it to move.
Definition: Movable.hpp:26
bool moving() const
Returns whether or not the entity is moving.
Definition: Movable.cpp:43
void update(bl::ecs::Entity owner, system::Systems &systems, float dt)
Updates the interpolation of the entity if moving.
Definition: Movable.cpp:53
void move(bl::tmap::Direction dir, bool fast, bool isLedgeHop)
Starts the entity moving in the given direction. Only updates the interpolated position,...
Definition: Movable.cpp:47
Fired when an entity rotates without moving.
Definition: EntityMoved.hpp:76
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 isLedgeHop(const bl::tmap::Position &position, bl::tmap::Direction dir) const
Test whether the given movement will be a ledge hop or not.
Definition: Map.cpp:378
bool contains(const bl::tmap::Position &position) const
Returns whether or not the map contains the given position.
Definition: Map.cpp:271
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
static const std::string & SoundPath()
Definition: Properties.cpp:696
Movement(Systems &owner)
Create the movement system.
Definition: Movement.cpp:11
bool moveEntity(bl::ecs::Entity entity, bl::tmap::Direction direction, bool fast)
Moves an entity in the given direction using either its fast or slow speed.
Definition: Movement.cpp:27
bool makeMovable(bl::ecs::Entity entity, float moveSpeed, float fastMoveSpeed)
Adds a Movable component to the given entity if it does not already exist.
Definition: Movement.cpp:19
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
Position & position()
Returns a reference to the position system.
Definition: Systems.cpp:47
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