summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-12 16:39:49 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-12 16:39:49 -0500
commit5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b (patch)
tree283cef0e95f237e70892e8b90c841f971cb61fd9
parent77be863f4f15d2481a64e4e8dadb4060a6e4e590 (diff)
downloadtherapy-5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b.tar.gz
therapy-5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b.tar.bz2
therapy-5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b.zip
Abstracted behavior related to "orientable" entities
A lot of the stuff that ControllingSystem did to control the player character was moved into the new OrientingSystem. This is so that the player, or any player-like entities, can also be controlled by AI, with the underlying behavior being delegated in the same way as if the player were being controlled by the user.

Fixed the issue where, if the player were blocked while moving horizontally, they would remain blocked even if vertical movement were to remove the collision.

Fixed cases of the player animating incorrectly after performing certain movements.
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/components/droppable.h24
-rw-r--r--src/components/orientable.h45
-rw-r--r--src/components/ponderable.h17
-rw-r--r--src/consts.h10
-rw-r--r--src/game.cpp4
-rw-r--r--src/systems/controlling.cpp129
-rw-r--r--src/systems/controlling.h8
-rw-r--r--src/systems/mapping.cpp2
-rw-r--r--src/systems/orienting.cpp236
-rw-r--r--src/systems/orienting.h35
-rw-r--r--src/systems/pondering.cpp89
12 files changed, 393 insertions, 207 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index f918156..39b1bbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -66,6 +66,7 @@ add_executable(Aromatherapy
66 src/systems/pondering.cpp 66 src/systems/pondering.cpp
67 src/systems/animating.cpp 67 src/systems/animating.cpp
68 src/systems/mapping.cpp 68 src/systems/mapping.cpp
69 src/systems/orienting.cpp
69) 70)
70target_link_libraries(Aromatherapy ${ALL_LIBS}) 71target_link_libraries(Aromatherapy ${ALL_LIBS})
71install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR}) 72install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR})
diff --git a/src/components/droppable.h b/src/components/droppable.h deleted file mode 100644 index 722c139..0000000 --- a/src/components/droppable.h +++ /dev/null
@@ -1,24 +0,0 @@
1#ifndef DROPPABLE_H_5DB254EF
2#define DROPPABLE_H_5DB254EF
3
4#include "component.h"
5
6class DroppableComponent : public Component {
7public:
8
9 inline bool isDroppable() const
10 {
11 return droppable_;
12 }
13
14 inline void setDroppable(bool can)
15 {
16 droppable_ = can;
17 }
18
19private:
20
21 bool droppable_ = false;
22};
23
24#endif /* end of include guard: DROPPABLE_H_5DB254EF */
diff --git a/src/components/orientable.h b/src/components/orientable.h index 8f56912..e356b78 100644 --- a/src/components/orientable.h +++ b/src/components/orientable.h
@@ -6,6 +6,18 @@
6class OrientableComponent : public Component { 6class OrientableComponent : public Component {
7public: 7public:
8 8
9 enum class WalkState {
10 still,
11 left,
12 right
13 };
14
15 enum class DropState {
16 none,
17 ready,
18 active
19 };
20
9 inline bool isFacingRight() const 21 inline bool isFacingRight() const
10 { 22 {
11 return facingRight_; 23 return facingRight_;
@@ -16,9 +28,42 @@ public:
16 facingRight_ = v; 28 facingRight_ = v;
17 } 29 }
18 30
31 inline WalkState getWalkState() const
32 {
33 return walkState_;
34 }
35
36 inline void setWalkState(WalkState v)
37 {
38 walkState_ = v;
39 }
40
41 inline bool isJumping() const
42 {
43 return jumping_;
44 }
45
46 inline void setJumping(bool v)
47 {
48 jumping_ = v;
49 }
50
51 inline DropState getDropState() const
52 {
53 return dropState_;
54 }
55
56 inline void setDropState(DropState v)
57 {
58 dropState_ = v;
59 }
60
19private: 61private:
20 62
21 bool facingRight_ = false; 63 bool facingRight_ = false;
64 WalkState walkState_ = WalkState::still;
65 bool jumping_ = false;
66 DropState dropState_ = DropState::none;
22}; 67};
23 68
24#endif /* end of include guard: ORIENTABLE_H_EDB6C4A1 */ 69#endif /* end of include guard: ORIENTABLE_H_EDB6C4A1 */
diff --git a/src/components/ponderable.h b/src/components/ponderable.h index ac759b6..e21cbab 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h
@@ -11,13 +11,6 @@ public:
11 freefalling 11 freefalling
12 }; 12 };
13 13
14 enum class State {
15 grounded,
16 jumping,
17 falling,
18 dropping
19 };
20
21 PonderableComponent(Type type) : type_(type) 14 PonderableComponent(Type type) : type_(type)
22 { 15 {
23 } 16 }
@@ -67,14 +60,14 @@ public:
67 accelY_ = v; 60 accelY_ = v;
68 } 61 }
69 62
70 inline State getState() const 63 inline bool isGrounded() const
71 { 64 {
72 return state_; 65 return grounded_;
73 } 66 }
74 67
75 inline void setState(State arg) 68 inline void setGrounded(bool v)
76 { 69 {
77 state_ = arg; 70 grounded_ = v;
78 } 71 }
79 72
80private: 73private:
@@ -84,7 +77,7 @@ private:
84 double accelX_ = 0.0; 77 double accelX_ = 0.0;
85 double accelY_ = 0.0; 78 double accelY_ = 0.0;
86 Type type_ = Type::vacuumed; 79 Type type_ = Type::vacuumed;
87 State state_ = State::grounded; 80 bool grounded_ = false;
88}; 81};
89 82
90#endif /* end of include guard: TANGIBLE_H_746DB3EE */ 83#endif /* end of include guard: TANGIBLE_H_746DB3EE */
diff --git a/src/consts.h b/src/consts.h index a6c9985..581018d 100644 --- a/src/consts.h +++ b/src/consts.h
@@ -14,7 +14,13 @@ const int FONT_COLS = 16;
14const int FRAMES_PER_SECOND = 60; 14const int FRAMES_PER_SECOND = 60;
15const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; 15const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND;
16 16
17#define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) 17#define CALC_VELOCITY(h, l) (-2 * (h) / (l))
18#define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) 18#define CALC_GRAVITY(h, l) (2 * ((h) / (l)) / (l))
19
20const double NORMAL_GRAVITY = CALC_GRAVITY(TILE_HEIGHT*3.5, 0.233);
21const double JUMP_GRAVITY = CALC_GRAVITY(TILE_HEIGHT*4.5, 0.3);
22const double JUMP_VELOCITY = CALC_VELOCITY(TILE_HEIGHT*4.5, 0.3);
23
24const double WALK_SPEED = 90;
19 25
20#endif 26#endif
diff --git a/src/game.cpp b/src/game.cpp index 7cbe7e0..39bb3f1 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -2,13 +2,13 @@
2#include "components/animatable.h" 2#include "components/animatable.h"
3#include "components/transformable.h" 3#include "components/transformable.h"
4#include "components/controllable.h" 4#include "components/controllable.h"
5#include "components/droppable.h"
6#include "components/ponderable.h" 5#include "components/ponderable.h"
7#include "components/orientable.h" 6#include "components/orientable.h"
8#include "systems/controlling.h" 7#include "systems/controlling.h"
9#include "systems/pondering.h" 8#include "systems/pondering.h"
10#include "systems/animating.h" 9#include "systems/animating.h"
11#include "systems/mapping.h" 10#include "systems/mapping.h"
11#include "systems/orienting.h"
12#include "animation.h" 12#include "animation.h"
13#include "renderer.h" 13#include "renderer.h"
14#include "consts.h" 14#include "consts.h"
@@ -33,6 +33,7 @@ Game::Game(
33 world_("res/maps.xml") 33 world_("res/maps.xml")
34{ 34{
35 systemManager_.emplaceSystem<ControllingSystem>(*this); 35 systemManager_.emplaceSystem<ControllingSystem>(*this);
36 systemManager_.emplaceSystem<OrientingSystem>(*this);
36 systemManager_.emplaceSystem<PonderingSystem>(*this); 37 systemManager_.emplaceSystem<PonderingSystem>(*this);
37 systemManager_.emplaceSystem<MappingSystem>(*this); 38 systemManager_.emplaceSystem<MappingSystem>(*this);
38 systemManager_.emplaceSystem<AnimatingSystem>(*this); 39 systemManager_.emplaceSystem<AnimatingSystem>(*this);
@@ -58,7 +59,6 @@ Game::Game(
58 player, 59 player,
59 PonderableComponent::Type::freefalling); 60 PonderableComponent::Type::freefalling);
60 61
61 entityManager_.emplaceComponent<DroppableComponent>(player);
62 entityManager_.emplaceComponent<ControllableComponent>(player); 62 entityManager_.emplaceComponent<ControllableComponent>(player);
63 entityManager_.emplaceComponent<OrientableComponent>(player); 63 entityManager_.emplaceComponent<OrientableComponent>(player);
64 64
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index fa09d11..e1609bd 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp
@@ -1,14 +1,8 @@
1#include "controlling.h" 1#include "controlling.h"
2#include "game.h" 2#include "game.h"
3#include "components/controllable.h" 3#include "components/controllable.h"
4#include "components/ponderable.h"
5#include "components/animatable.h"
6#include "components/droppable.h"
7#include "components/orientable.h" 4#include "components/orientable.h"
8#include "systems/animating.h" 5#include "systems/orienting.h"
9#include "direction.h"
10#include "muxer.h"
11#include "consts.h"
12 6
13void ControllingSystem::tick(double) 7void ControllingSystem::tick(double)
14{ 8{
@@ -19,9 +13,6 @@ void ControllingSystem::tick(double)
19 13
20 auto entities = game_.getEntityManager().getEntitiesWithComponents< 14 auto entities = game_.getEntityManager().getEntitiesWithComponents<
21 ControllableComponent, 15 ControllableComponent,
22 PonderableComponent,
23 AnimatableComponent,
24 DroppableComponent,
25 OrientableComponent>(); 16 OrientableComponent>();
26 17
27 for (auto entity : entities) 18 for (auto entity : entities)
@@ -29,6 +20,8 @@ void ControllingSystem::tick(double)
29 auto& controllable = game_.getEntityManager(). 20 auto& controllable = game_.getEntityManager().
30 getComponent<ControllableComponent>(entity); 21 getComponent<ControllableComponent>(entity);
31 22
23 auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>();
24
32 if (action == GLFW_PRESS) 25 if (action == GLFW_PRESS)
33 { 26 {
34 if (key == controllable.getLeftKey()) 27 if (key == controllable.getLeftKey())
@@ -37,7 +30,7 @@ void ControllingSystem::tick(double)
37 30
38 if (!controllable.isFrozen()) 31 if (!controllable.isFrozen())
39 { 32 {
40 walkLeft(entity); 33 orienting.moveLeft(entity);
41 } 34 }
42 } else if (key == controllable.getRightKey()) 35 } else if (key == controllable.getRightKey())
43 { 36 {
@@ -45,19 +38,19 @@ void ControllingSystem::tick(double)
45 38
46 if (!controllable.isFrozen()) 39 if (!controllable.isFrozen())
47 { 40 {
48 walkRight(entity); 41 orienting.moveRight(entity);
49 } 42 }
50 } else if (key == controllable.getJumpKey()) 43 } else if (key == controllable.getJumpKey())
51 { 44 {
52 if (!controllable.isFrozen()) 45 if (!controllable.isFrozen())
53 { 46 {
54 jump(entity); 47 orienting.jump(entity);
55 } 48 }
56 } else if (key == controllable.getDropKey()) 49 } else if (key == controllable.getDropKey())
57 { 50 {
58 if (!controllable.isFrozen()) 51 if (!controllable.isFrozen())
59 { 52 {
60 drop(entity, true); 53 orienting.drop(entity);
61 } 54 }
62 } 55 }
63 } else if (action == GLFW_RELEASE) 56 } else if (action == GLFW_RELEASE)
@@ -70,9 +63,9 @@ void ControllingSystem::tick(double)
70 { 63 {
71 if (controllable.isHoldingRight()) 64 if (controllable.isHoldingRight())
72 { 65 {
73 walkRight(entity); 66 orienting.moveRight(entity);
74 } else { 67 } else {
75 stopWalking(entity); 68 orienting.stopWalking(entity);
76 } 69 }
77 } 70 }
78 } else if (key == controllable.getRightKey()) 71 } else if (key == controllable.getRightKey())
@@ -83,22 +76,22 @@ void ControllingSystem::tick(double)
83 { 76 {
84 if (controllable.isHoldingLeft()) 77 if (controllable.isHoldingLeft())
85 { 78 {
86 walkLeft(entity); 79 orienting.moveLeft(entity);
87 } else { 80 } else {
88 stopWalking(entity); 81 orienting.stopWalking(entity);
89 } 82 }
90 } 83 }
91 } else if (key == controllable.getDropKey()) 84 } else if (key == controllable.getDropKey())
92 { 85 {
93 if (!controllable.isFrozen()) 86 if (!controllable.isFrozen())
94 { 87 {
95 drop(entity, false); 88 orienting.stopDropping(entity);
96 } 89 }
97 } else if (key == controllable.getJumpKey()) 90 } else if (key == controllable.getJumpKey())
98 { 91 {
99 if (!controllable.isFrozen()) 92 if (!controllable.isFrozen())
100 { 93 {
101 stopJumping(entity); 94 orienting.stopJumping(entity);
102 } 95 }
103 } 96 }
104 } 97 }
@@ -112,99 +105,3 @@ void ControllingSystem::input(int key, int action)
112{ 105{
113 actions_.push(std::make_pair(key, action)); 106 actions_.push(std::make_pair(key, action));
114} 107}
115
116void ControllingSystem::walkLeft(id_type entity)
117{
118 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
119 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
120
121 orientable.setFacingRight(false);
122 ponderable.setVelocityX(-90);
123
124 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
125
126 if (ponderable.getState() == PonderableComponent::State::grounded)
127 {
128 animating.startAnimation(entity, "walkingLeft");
129 } else {
130 animating.startAnimation(entity, "stillLeft");
131 }
132}
133
134void ControllingSystem::walkRight(id_type entity)
135{
136 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
137 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
138
139 orientable.setFacingRight(true);
140 ponderable.setVelocityX(90);
141
142 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
143
144 if (ponderable.getState() == PonderableComponent::State::grounded)
145 {
146 animating.startAnimation(entity, "walkingRight");
147 } else {
148 animating.startAnimation(entity, "stillRight");
149 }
150}
151
152void ControllingSystem::stopWalking(id_type entity)
153{
154 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
155 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
156
157 ponderable.setVelocityX(0);
158
159 if (ponderable.getState() == PonderableComponent::State::grounded)
160 {
161 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
162
163 if (orientable.isFacingRight())
164 {
165 animating.startAnimation(entity, "stillRight");
166 } else {
167 animating.startAnimation(entity, "stillLeft");
168 }
169 }
170}
171
172void ControllingSystem::jump(id_type entity)
173{
174 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
175
176 if (ponderable.getState() == PonderableComponent::State::grounded)
177 {
178 playSound("res/Randomize87.wav", 0.25);
179
180 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3));
181 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3));
182 ponderable.setState(PonderableComponent::State::jumping);
183 }
184}
185
186void ControllingSystem::stopJumping(id_type entity)
187{
188 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
189
190 if (ponderable.getState() == PonderableComponent::State::jumping)
191 {
192 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
193 ponderable.setState(PonderableComponent::State::falling);
194 }
195}
196
197void ControllingSystem::drop(id_type entity, bool start)
198{
199 auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity);
200 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
201
202 if (start && (ponderable.getState() == PonderableComponent::State::grounded))
203 {
204 ponderable.setState(PonderableComponent::State::dropping);
205 } else if ((!start) && (ponderable.getState() == PonderableComponent::State::dropping))
206 {
207 ponderable.setState(PonderableComponent::State::grounded);
208 }
209 droppable.setDroppable(start);
210}
diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 1f1e8a0..01ed7a0 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h
@@ -3,7 +3,6 @@
3 3
4#include "system.h" 4#include "system.h"
5#include <queue> 5#include <queue>
6#include "entity_manager.h"
7 6
8class ControllingSystem : public System { 7class ControllingSystem : public System {
9public: 8public:
@@ -17,13 +16,6 @@ public:
17 16
18private: 17private:
19 18
20 void walkLeft(id_type entity);
21 void walkRight(id_type entity);
22 void stopWalking(id_type entity);
23 void jump(id_type entity);
24 void stopJumping(id_type entity);
25 void drop(id_type entity, bool start);
26
27 std::queue<std::pair<int,int>> actions_; 19 std::queue<std::pair<int,int>> actions_;
28}; 20};
29 21
diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index 8723e16..5b63ded 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp
@@ -3,8 +3,6 @@
3#include "game.h" 3#include "game.h"
4#include "consts.h" 4#include "consts.h"
5 5
6#include <iostream>
7
8template <typename Storage> 6template <typename Storage>
9inline void addBoundary( 7inline void addBoundary(
10 Storage& boundaries, 8 Storage& boundaries,
diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp new file mode 100644 index 0000000..187bebc --- /dev/null +++ b/src/systems/orienting.cpp
@@ -0,0 +1,236 @@
1#include "orienting.h"
2#include "game.h"
3#include "components/orientable.h"
4#include "components/ponderable.h"
5#include "systems/animating.h"
6#include "consts.h"
7#include "muxer.h"
8
9void OrientingSystem::tick(double)
10{
11 auto entities = game_.getEntityManager().getEntitiesWithComponents<
12 OrientableComponent,
13 PonderableComponent>();
14
15 for (id_type entity : entities)
16 {
17 auto& orientable = game_.getEntityManager().
18 getComponent<OrientableComponent>(entity);
19
20 auto& ponderable = game_.getEntityManager().
21 getComponent<PonderableComponent>(entity);
22
23 switch (orientable.getWalkState())
24 {
25 case OrientableComponent::WalkState::still:
26 {
27 ponderable.setVelocityX(0);
28
29 break;
30 }
31
32 case OrientableComponent::WalkState::left:
33 {
34 ponderable.setVelocityX(-WALK_SPEED);
35
36 break;
37 }
38
39 case OrientableComponent::WalkState::right:
40 {
41 ponderable.setVelocityX(WALK_SPEED);
42
43 break;
44 }
45 }
46
47 if (orientable.isJumping() && (ponderable.getVelocityY() > 0))
48 {
49 orientable.setJumping(false);
50 }
51 }
52}
53
54void OrientingSystem::moveLeft(id_type entity)
55{
56 auto& ponderable = game_.getEntityManager().
57 getComponent<PonderableComponent>(entity);
58
59 auto& orientable = game_.getEntityManager().
60 getComponent<OrientableComponent>(entity);
61
62 orientable.setFacingRight(false);
63 orientable.setWalkState(OrientableComponent::WalkState::left);
64
65 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
66 if (ponderable.isGrounded())
67 {
68 animating.startAnimation(entity, "walkingLeft");
69 } else {
70 animating.startAnimation(entity, "stillLeft");
71 }
72}
73
74void OrientingSystem::moveRight(id_type entity)
75{
76 auto& ponderable = game_.getEntityManager().
77 getComponent<PonderableComponent>(entity);
78
79 auto& orientable = game_.getEntityManager().
80 getComponent<OrientableComponent>(entity);
81
82 orientable.setFacingRight(true);
83 orientable.setWalkState(OrientableComponent::WalkState::right);
84
85 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
86 if (ponderable.isGrounded())
87 {
88 animating.startAnimation(entity, "walkingRight");
89 } else {
90 animating.startAnimation(entity, "stillRight");
91 }
92}
93
94void OrientingSystem::stopWalking(id_type entity)
95{
96 auto& ponderable = game_.getEntityManager().
97 getComponent<PonderableComponent>(entity);
98
99 auto& orientable = game_.getEntityManager().
100 getComponent<OrientableComponent>(entity);
101
102 orientable.setWalkState(OrientableComponent::WalkState::still);
103
104 if (ponderable.isGrounded())
105 {
106 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
107
108 if (orientable.isFacingRight())
109 {
110 animating.startAnimation(entity, "stillRight");
111 } else {
112 animating.startAnimation(entity, "stillLeft");
113 }
114 }
115}
116
117void OrientingSystem::jump(id_type entity)
118{
119 auto& ponderable = game_.getEntityManager().
120 getComponent<PonderableComponent>(entity);
121
122 if (ponderable.isGrounded())
123 {
124 auto& orientable = game_.getEntityManager().
125 getComponent<OrientableComponent>(entity);
126
127 orientable.setJumping(true);
128
129 playSound("res/Randomize87.wav", 0.25);
130
131 ponderable.setVelocityY(JUMP_VELOCITY);
132 ponderable.setAccelY(JUMP_GRAVITY);
133
134 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
135 if (orientable.isFacingRight())
136 {
137 animating.startAnimation(entity, "stillRight");
138 } else {
139 animating.startAnimation(entity, "stillLeft");
140 }
141 }
142}
143
144void OrientingSystem::stopJumping(id_type entity)
145{
146 auto& orientable = game_.getEntityManager().
147 getComponent<OrientableComponent>(entity);
148
149 if (orientable.isJumping())
150 {
151 orientable.setJumping(false);
152
153 auto& ponderable = game_.getEntityManager().
154 getComponent<PonderableComponent>(entity);
155
156 ponderable.setAccelY(NORMAL_GRAVITY);
157 }
158}
159
160void OrientingSystem::land(id_type entity)
161{
162 auto& orientable = game_.getEntityManager().
163 getComponent<OrientableComponent>(entity);
164
165 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
166
167 switch (orientable.getWalkState())
168 {
169 case OrientableComponent::WalkState::still:
170 {
171 if (orientable.isFacingRight())
172 {
173 animating.startAnimation(entity, "stillRight");
174 } else {
175 animating.startAnimation(entity, "stillLeft");
176 }
177
178 break;
179 }
180
181 case OrientableComponent::WalkState::left:
182 {
183 animating.startAnimation(entity, "walkingLeft");
184
185 break;
186 }
187
188 case OrientableComponent::WalkState::right:
189 {
190 animating.startAnimation(entity, "walkingRight");
191
192 break;
193 }
194 }
195}
196
197void OrientingSystem::startFalling(id_type entity)
198{
199 auto& orientable = game_.getEntityManager().
200 getComponent<OrientableComponent>(entity);
201
202 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
203
204 if (orientable.isFacingRight())
205 {
206 animating.startAnimation(entity, "stillRight");
207 } else {
208 animating.startAnimation(entity, "stillLeft");
209 }
210}
211
212void OrientingSystem::drop(id_type entity)
213{
214 auto& orientable = game_.getEntityManager().
215 getComponent<OrientableComponent>(entity);
216
217 auto& ponderable = game_.getEntityManager().
218 getComponent<PonderableComponent>(entity);
219
220 if (ponderable.isGrounded()
221 && (orientable.getDropState() == OrientableComponent::DropState::none))
222 {
223 orientable.setDropState(OrientableComponent::DropState::ready);
224 }
225}
226
227void OrientingSystem::stopDropping(id_type entity)
228{
229 auto& orientable = game_.getEntityManager().
230 getComponent<OrientableComponent>(entity);
231
232 if (orientable.getDropState() == OrientableComponent::DropState::ready)
233 {
234 orientable.setDropState(OrientableComponent::DropState::none);
235 }
236}
diff --git a/src/systems/orienting.h b/src/systems/orienting.h new file mode 100644 index 0000000..4ded612 --- /dev/null +++ b/src/systems/orienting.h
@@ -0,0 +1,35 @@
1#ifndef ORIENTING_H_099F0C23
2#define ORIENTING_H_099F0C23
3
4#include "system.h"
5
6class OrientingSystem : public System {
7public:
8
9 OrientingSystem(Game& game) : System(game)
10 {
11 }
12
13 void tick(double dt);
14
15 void moveLeft(id_type entity);
16
17 void moveRight(id_type entity);
18
19 void stopWalking(id_type entity);
20
21 void jump(id_type entity);
22
23 void stopJumping(id_type entity);
24
25 void land(id_type entity);
26
27 void startFalling(id_type entity);
28
29 void drop(id_type entity);
30
31 void stopDropping(id_type entity);
32
33};
34
35#endif /* end of include guard: ORIENTING_H_099F0C23 */
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 26a6f56..4a165b1 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp
@@ -2,7 +2,9 @@
2#include "game.h" 2#include "game.h"
3#include "components/ponderable.h" 3#include "components/ponderable.h"
4#include "components/transformable.h" 4#include "components/transformable.h"
5#include "components/droppable.h" 5#include "components/orientable.h"
6#include "components/mappable.h"
7#include "systems/orienting.h"
6#include "consts.h" 8#include "consts.h"
7 9
8void PonderingSystem::tick(double dt) 10void PonderingSystem::tick(double dt)
@@ -37,10 +39,8 @@ void PonderingSystem::tick(double dt)
37 double newX = oldX + ponderable.getVelocityX() * dt; 39 double newX = oldX + ponderable.getVelocityX() * dt;
38 double newY = oldY + ponderable.getVelocityY() * dt; 40 double newY = oldY + ponderable.getVelocityY() * dt;
39 41
40 if (ponderable.getVelocityY() > 0.0) 42 bool oldGrounded = ponderable.isGrounded();
41 { 43 ponderable.setGrounded(false);
42 ponderable.setState(PonderableComponent::State::falling);
43 }
44 44
45 for (id_type mapEntity : maps) 45 for (id_type mapEntity : maps)
46 { 46 {
@@ -64,8 +64,6 @@ void PonderingSystem::tick(double dt)
64 newY, 64 newY,
65 it->first, 65 it->first,
66 it->second.getType()); 66 it->second.getType());
67
68 break;
69 } 67 }
70 } 68 }
71 } else if (newX > oldX) 69 } else if (newX > oldX)
@@ -86,8 +84,6 @@ void PonderingSystem::tick(double dt)
86 newY, 84 newY,
87 it->first, 85 it->first,
88 it->second.getType()); 86 it->second.getType());
89
90 break;
91 } 87 }
92 } 88 }
93 } 89 }
@@ -109,8 +105,6 @@ void PonderingSystem::tick(double dt)
109 newY, 105 newY,
110 it->first, 106 it->first,
111 it->second.getType()); 107 it->second.getType());
112
113 break;
114 } 108 }
115 } 109 }
116 } else if (newY > oldY) 110 } else if (newY > oldY)
@@ -131,8 +125,6 @@ void PonderingSystem::tick(double dt)
131 newY, 125 newY,
132 it->first, 126 it->first,
133 it->second.getType()); 127 it->second.getType());
134
135 break;
136 } 128 }
137 } 129 }
138 } 130 }
@@ -141,6 +133,31 @@ void PonderingSystem::tick(double dt)
141 // Move 133 // Move
142 transformable.setX(newX); 134 transformable.setX(newX);
143 transformable.setY(newY); 135 transformable.setY(newY);
136
137 // Perform cleanup for orientable entites
138 if (game_.getEntityManager().hasComponent<OrientableComponent>(entity))
139 {
140 auto& orientable = game_.getEntityManager().
141 getComponent<OrientableComponent>(entity);
142
143 // Handle changes in groundedness
144 if (ponderable.isGrounded() != oldGrounded)
145 {
146 if (ponderable.isGrounded())
147 {
148 game_.getSystemManager().getSystem<OrientingSystem>().land(entity);
149 } else {
150 game_.getSystemManager().
151 getSystem<OrientingSystem>().startFalling(entity);
152 }
153 }
154
155 // Complete dropping, if necessary
156 if (orientable.getDropState() == OrientableComponent::DropState::active)
157 {
158 orientable.setDropState(OrientableComponent::DropState::none);
159 }
160 }
144 } 161 }
145} 162}
146 163
@@ -153,8 +170,7 @@ void PonderingSystem::initializeBody(
153 170
154 if (type == PonderableComponent::Type::freefalling) 171 if (type == PonderableComponent::Type::freefalling)
155 { 172 {
156 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); 173 ponderable.setAccelY(NORMAL_GRAVITY);
157 ponderable.setState(PonderableComponent::State::falling);
158 } 174 }
159} 175}
160 176
@@ -172,6 +188,8 @@ void PonderingSystem::processCollision(
172 auto& transformable = game_.getEntityManager(). 188 auto& transformable = game_.getEntityManager().
173 getComponent<TransformableComponent>(entity); 189 getComponent<TransformableComponent>(entity);
174 190
191 bool touchedGround = false;
192
175 switch (type) 193 switch (type)
176 { 194 {
177 case MappableComponent::Boundary::Type::wall: 195 case MappableComponent::Boundary::Type::wall:
@@ -204,13 +222,7 @@ void PonderingSystem::processCollision(
204 222
205 case Direction::down: 223 case Direction::down:
206 { 224 {
207 newY = axis - transformable.getH(); 225 touchedGround = true;
208 ponderable.setVelocityY(0.0);
209
210 if (ponderable.getState() == PonderableComponent::State::falling)
211 {
212 ponderable.setState(PonderableComponent::State::grounded);
213 }
214 226
215 break; 227 break;
216 } 228 }
@@ -221,31 +233,19 @@ void PonderingSystem::processCollision(
221 233
222 case MappableComponent::Boundary::Type::platform: 234 case MappableComponent::Boundary::Type::platform:
223 { 235 {
224 if (game_.getEntityManager().hasComponent<DroppableComponent>(entity)) 236 if (game_.getEntityManager().hasComponent<OrientableComponent>(entity))
225 { 237 {
226 auto& droppable = game_.getEntityManager(). 238 auto& orientable = game_.getEntityManager().
227 getComponent<DroppableComponent>(entity); 239 getComponent<OrientableComponent>(entity);
228 240
229 if (droppable.isDroppable()) 241 if (orientable.getDropState() != OrientableComponent::DropState::none)
230 { 242 {
231 droppable.setDroppable(false); 243 orientable.setDropState(OrientableComponent::DropState::active);
232 } else { 244 } else {
233 newY = axis - transformable.getH(); 245 touchedGround = true;
234 ponderable.setVelocityY(0.0);
235
236 if (ponderable.getState() == PonderableComponent::State::falling)
237 {
238 ponderable.setState(PonderableComponent::State::grounded);
239 }
240 } 246 }
241 } else { 247 } else {
242 newY = axis - transformable.getH(); 248 touchedGround = true;
243 ponderable.setVelocityY(0.0);
244
245 if (ponderable.getState() == PonderableComponent::State::falling)
246 {
247 ponderable.setState(PonderableComponent::State::grounded);
248 }
249 } 249 }
250 250
251 break; 251 break;
@@ -258,4 +258,11 @@ void PonderingSystem::processCollision(
258 break; 258 break;
259 } 259 }
260 } 260 }
261
262 if (touchedGround)
263 {
264 newY = axis - transformable.getH();
265 ponderable.setVelocityY(0.0);
266 ponderable.setGrounded(true);
267 }
261} 268}