summary refs log tree commit diff stats
path: root/src/systems/playing.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-18 12:35:45 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-18 12:35:45 -0500
commite16fb5be90c889c371cbb0ca2444735c2e12073c (patch)
treecbaa20e14a34c460b6c9886f266c4b4b6f62ae87 /src/systems/playing.cpp
parented08b673c50b076042d8f0c49501372168142764 (diff)
downloadtherapy-e16fb5be90c889c371cbb0ca2444735c2e12073c.tar.gz
therapy-e16fb5be90c889c371cbb0ca2444735c2e12073c.tar.bz2
therapy-e16fb5be90c889c371cbb0ca2444735c2e12073c.zip
Implemented map adjacency
This brings along with it the ability to move to different maps, for which the PlayingSystem and PlayableComponent were introduced. The PlayingSystem is a general overseer system that handles big picture stuff like initializing the player and changing maps. The PlayableComponent represents the player. While the ControllableComponent is also likely to always only be on the player entity, the two are distinct by separation of concerns.

This also required a refactoring of how collisions are processed, because of a bug where the player can move to a new map when horizontal collisions are checked, and vertical collisions are skipped, causing the player to clip through the ground because the normal force was never handled.
Diffstat (limited to 'src/systems/playing.cpp')
-rw-r--r--src/systems/playing.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp new file mode 100644 index 0000000..2c6a419 --- /dev/null +++ b/src/systems/playing.cpp
@@ -0,0 +1,98 @@
1#include "playing.h"
2#include "game.h"
3#include "components/transformable.h"
4#include "components/animatable.h"
5#include "components/playable.h"
6#include "components/controllable.h"
7#include "components/orientable.h"
8#include "systems/mapping.h"
9#include "systems/pondering.h"
10#include "animation.h"
11
12void PlayingSystem::tick(double)
13{
14 // Check if we need to change the map
15 auto players = game_.getEntityManager().getEntitiesWithComponents<
16 PlayableComponent>();
17
18 for (id_type player : players)
19 {
20 auto& playable = game_.getEntityManager().
21 getComponent<PlayableComponent>(player);
22
23 if (playable.changingMap)
24 {
25 // Change the map!
26 auto entities = game_.getEntityManager().getEntities();
27
28 for (id_type entity : entities)
29 {
30 if (entity != player)
31 {
32 game_.getEntityManager().deleteEntity(entity);
33 }
34 }
35
36 game_.getSystemManager().getSystem<MappingSystem>().
37 loadMap(playable.newMapId);
38
39 auto& transformable = game_.getEntityManager().
40 getComponent<TransformableComponent>(player);
41
42 transformable.setX(playable.newMapX);
43 transformable.setY(playable.newMapY);
44
45 playable.changingMap = false;
46
47 break;
48 }
49 }
50}
51
52void PlayingSystem::initPlayer()
53{
54 id_type player = game_.getEntityManager().emplaceEntity();
55
56 AnimationSet playerGraphics {"res/Starla.png", 10, 12, 6};
57 playerGraphics.emplaceAnimation("stillLeft", 3, 1, 1);
58 playerGraphics.emplaceAnimation("stillRight", 0, 1, 1);
59 playerGraphics.emplaceAnimation("walkingLeft", 4, 2, 10);
60 playerGraphics.emplaceAnimation("walkingRight", 1, 2, 10);
61
62 game_.getEntityManager().emplaceComponent<AnimatableComponent>(
63 player,
64 std::move(playerGraphics),
65 "stillLeft");
66
67 game_.getEntityManager().emplaceComponent<TransformableComponent>(
68 player,
69 203, 44, 10, 12);
70
71 game_.getSystemManager().getSystem<PonderingSystem>().initializeBody(
72 player,
73 PonderableComponent::Type::freefalling);
74
75 game_.getEntityManager().emplaceComponent<ControllableComponent>(player);
76 game_.getEntityManager().emplaceComponent<OrientableComponent>(player);
77 game_.getEntityManager().emplaceComponent<PlayableComponent>(player);
78}
79
80void PlayingSystem::changeMap(
81 size_t mapId,
82 double x,
83 double y)
84{
85 auto players = game_.getEntityManager().getEntitiesWithComponents<
86 PlayableComponent>();
87
88 for (id_type player : players)
89 {
90 auto& playable = game_.getEntityManager().
91 getComponent<PlayableComponent>(player);
92
93 playable.changingMap = true;
94 playable.newMapId = mapId;
95 playable.newMapX = x;
96 playable.newMapY = y;
97 }
98}