diff options
Diffstat (limited to 'src/systems')
-rw-r--r-- | src/systems/animating.cpp | 68 | ||||
-rw-r--r-- | src/systems/animating.h | 23 | ||||
-rw-r--r-- | src/systems/controlling.cpp | 107 | ||||
-rw-r--r-- | src/systems/controlling.h | 22 | ||||
-rw-r--r-- | src/systems/mapping.cpp | 141 | ||||
-rw-r--r-- | src/systems/mapping.h | 19 | ||||
-rw-r--r-- | src/systems/orienting.cpp | 236 | ||||
-rw-r--r-- | src/systems/orienting.h | 35 | ||||
-rw-r--r-- | src/systems/pondering.cpp | 268 | ||||
-rw-r--r-- | src/systems/pondering.h | 32 |
10 files changed, 951 insertions, 0 deletions
diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp new file mode 100644 index 0000000..91fe925 --- /dev/null +++ b/src/systems/animating.cpp | |||
@@ -0,0 +1,68 @@ | |||
1 | #include "animating.h" | ||
2 | #include "game.h" | ||
3 | #include "components/animatable.h" | ||
4 | #include "components/transformable.h" | ||
5 | |||
6 | void AnimatingSystem::tick(double) | ||
7 | { | ||
8 | std::set<id_type> spriteEntities = | ||
9 | game_.getEntityManager().getEntitiesWithComponents<AnimatableComponent>(); | ||
10 | |||
11 | for (id_type entity : spriteEntities) | ||
12 | { | ||
13 | auto& sprite = game_.getEntityManager(). | ||
14 | getComponent<AnimatableComponent>(entity); | ||
15 | |||
16 | sprite.setCountdown(sprite.getCountdown() + 1); | ||
17 | |||
18 | const Animation& anim = sprite.getAnimation(); | ||
19 | if (sprite.getCountdown() >= anim.getDelay()) | ||
20 | { | ||
21 | sprite.setFrame(sprite.getFrame() + 1); | ||
22 | sprite.setCountdown(0); | ||
23 | |||
24 | if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) | ||
25 | { | ||
26 | sprite.setFrame(anim.getFirstFrame()); | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | void AnimatingSystem::render(Texture& texture) | ||
33 | { | ||
34 | std::set<id_type> spriteEntities = | ||
35 | game_.getEntityManager().getEntitiesWithComponents< | ||
36 | AnimatableComponent, | ||
37 | TransformableComponent>(); | ||
38 | |||
39 | for (id_type entity : spriteEntities) | ||
40 | { | ||
41 | auto& sprite = game_.getEntityManager(). | ||
42 | getComponent<AnimatableComponent>(entity); | ||
43 | |||
44 | auto& transform = game_.getEntityManager(). | ||
45 | getComponent<TransformableComponent>(entity); | ||
46 | |||
47 | Rectangle dstrect { | ||
48 | static_cast<int>(transform.getX()), | ||
49 | static_cast<int>(transform.getY()), | ||
50 | transform.getW(), | ||
51 | transform.getH()}; | ||
52 | |||
53 | const AnimationSet& aset = sprite.getAnimationSet(); | ||
54 | texture.blit( | ||
55 | aset.getTexture(), | ||
56 | aset.getFrameRect(sprite.getFrame()), | ||
57 | dstrect); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) | ||
62 | { | ||
63 | auto& sprite = game_.getEntityManager(). | ||
64 | getComponent<AnimatableComponent>(entity); | ||
65 | |||
66 | sprite.setAnimation(animation); | ||
67 | sprite.setFrame(sprite.getAnimation().getFirstFrame()); | ||
68 | } | ||
diff --git a/src/systems/animating.h b/src/systems/animating.h new file mode 100644 index 0000000..d6a89a5 --- /dev/null +++ b/src/systems/animating.h | |||
@@ -0,0 +1,23 @@ | |||
1 | #ifndef ANIMATING_H_5BBF0094 | ||
2 | #define ANIMATING_H_5BBF0094 | ||
3 | |||
4 | #include "system.h" | ||
5 | #include <string> | ||
6 | #include "renderer.h" | ||
7 | |||
8 | class AnimatingSystem : public System { | ||
9 | public: | ||
10 | |||
11 | AnimatingSystem(Game& game) : System(game) | ||
12 | { | ||
13 | } | ||
14 | |||
15 | void tick(double dt); | ||
16 | |||
17 | void render(Texture& texture); | ||
18 | |||
19 | void startAnimation(id_type entity, std::string animation); | ||
20 | |||
21 | }; | ||
22 | |||
23 | #endif /* end of include guard: ANIMATING_H_5BBF0094 */ | ||
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp new file mode 100644 index 0000000..e1609bd --- /dev/null +++ b/src/systems/controlling.cpp | |||
@@ -0,0 +1,107 @@ | |||
1 | #include "controlling.h" | ||
2 | #include "game.h" | ||
3 | #include "components/controllable.h" | ||
4 | #include "components/orientable.h" | ||
5 | #include "systems/orienting.h" | ||
6 | |||
7 | void ControllingSystem::tick(double) | ||
8 | { | ||
9 | while (!actions_.empty()) | ||
10 | { | ||
11 | int key = actions_.front().first; | ||
12 | int action = actions_.front().second; | ||
13 | |||
14 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
15 | ControllableComponent, | ||
16 | OrientableComponent>(); | ||
17 | |||
18 | for (auto entity : entities) | ||
19 | { | ||
20 | auto& controllable = game_.getEntityManager(). | ||
21 | getComponent<ControllableComponent>(entity); | ||
22 | |||
23 | auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>(); | ||
24 | |||
25 | if (action == GLFW_PRESS) | ||
26 | { | ||
27 | if (key == controllable.getLeftKey()) | ||
28 | { | ||
29 | controllable.setHoldingLeft(true); | ||
30 | |||
31 | if (!controllable.isFrozen()) | ||
32 | { | ||
33 | orienting.moveLeft(entity); | ||
34 | } | ||
35 | } else if (key == controllable.getRightKey()) | ||
36 | { | ||
37 | controllable.setHoldingRight(true); | ||
38 | |||
39 | if (!controllable.isFrozen()) | ||
40 | { | ||
41 | orienting.moveRight(entity); | ||
42 | } | ||
43 | } else if (key == controllable.getJumpKey()) | ||
44 | { | ||
45 | if (!controllable.isFrozen()) | ||
46 | { | ||
47 | orienting.jump(entity); | ||
48 | } | ||
49 | } else if (key == controllable.getDropKey()) | ||
50 | { | ||
51 | if (!controllable.isFrozen()) | ||
52 | { | ||
53 | orienting.drop(entity); | ||
54 | } | ||
55 | } | ||
56 | } else if (action == GLFW_RELEASE) | ||
57 | { | ||
58 | if (key == controllable.getLeftKey()) | ||
59 | { | ||
60 | controllable.setHoldingLeft(false); | ||
61 | |||
62 | if (!controllable.isFrozen()) | ||
63 | { | ||
64 | if (controllable.isHoldingRight()) | ||
65 | { | ||
66 | orienting.moveRight(entity); | ||
67 | } else { | ||
68 | orienting.stopWalking(entity); | ||
69 | } | ||
70 | } | ||
71 | } else if (key == controllable.getRightKey()) | ||
72 | { | ||
73 | controllable.setHoldingRight(false); | ||
74 | |||
75 | if (!controllable.isFrozen()) | ||
76 | { | ||
77 | if (controllable.isHoldingLeft()) | ||
78 | { | ||
79 | orienting.moveLeft(entity); | ||
80 | } else { | ||
81 | orienting.stopWalking(entity); | ||
82 | } | ||
83 | } | ||
84 | } else if (key == controllable.getDropKey()) | ||
85 | { | ||
86 | if (!controllable.isFrozen()) | ||
87 | { | ||
88 | orienting.stopDropping(entity); | ||
89 | } | ||
90 | } else if (key == controllable.getJumpKey()) | ||
91 | { | ||
92 | if (!controllable.isFrozen()) | ||
93 | { | ||
94 | orienting.stopJumping(entity); | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | |||
100 | actions_.pop(); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | void ControllingSystem::input(int key, int action) | ||
105 | { | ||
106 | actions_.push(std::make_pair(key, action)); | ||
107 | } | ||
diff --git a/src/systems/controlling.h b/src/systems/controlling.h new file mode 100644 index 0000000..01ed7a0 --- /dev/null +++ b/src/systems/controlling.h | |||
@@ -0,0 +1,22 @@ | |||
1 | #ifndef CONTROLLING_H_80B1BB8D | ||
2 | #define CONTROLLING_H_80B1BB8D | ||
3 | |||
4 | #include "system.h" | ||
5 | #include <queue> | ||
6 | |||
7 | class ControllingSystem : public System { | ||
8 | public: | ||
9 | |||
10 | ControllingSystem(Game& game) : System(game) | ||
11 | { | ||
12 | } | ||
13 | |||
14 | void tick(double dt); | ||
15 | void input(int key, int action); | ||
16 | |||
17 | private: | ||
18 | |||
19 | std::queue<std::pair<int,int>> actions_; | ||
20 | }; | ||
21 | |||
22 | #endif /* end of include guard: CONTROLLING_H_80B1BB8D */ | ||
diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp new file mode 100644 index 0000000..5b63ded --- /dev/null +++ b/src/systems/mapping.cpp | |||
@@ -0,0 +1,141 @@ | |||
1 | #include "mapping.h" | ||
2 | #include "components/mappable.h" | ||
3 | #include "game.h" | ||
4 | #include "consts.h" | ||
5 | |||
6 | template <typename Storage> | ||
7 | inline void addBoundary( | ||
8 | Storage& boundaries, | ||
9 | int axis, | ||
10 | int lower, | ||
11 | int upper, | ||
12 | MappableComponent::Boundary::Type type) | ||
13 | { | ||
14 | boundaries.emplace(std::piecewise_construct, | ||
15 | std::tie(axis), | ||
16 | std::tie(axis, lower, upper, type)); | ||
17 | } | ||
18 | |||
19 | void MappingSystem::render(Texture& texture) | ||
20 | { | ||
21 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
22 | MappableComponent>(); | ||
23 | |||
24 | for (id_type entity : entities) | ||
25 | { | ||
26 | auto& mappable = game_.getEntityManager(). | ||
27 | getComponent<MappableComponent>(entity); | ||
28 | |||
29 | const Map& map = game_.getWorld().getMap(mappable.getMapId()); | ||
30 | |||
31 | for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) | ||
32 | { | ||
33 | int x = i % MAP_WIDTH; | ||
34 | int y = i / MAP_WIDTH; | ||
35 | int tile = map.getTiles()[i]; | ||
36 | |||
37 | if (tile > 0) | ||
38 | { | ||
39 | Rectangle dst { | ||
40 | x * TILE_WIDTH, | ||
41 | y * TILE_HEIGHT, | ||
42 | TILE_WIDTH, | ||
43 | TILE_HEIGHT}; | ||
44 | |||
45 | Rectangle src { | ||
46 | (tile % TILESET_COLS) * TILE_WIDTH, | ||
47 | (tile / TILESET_COLS) * TILE_HEIGHT, | ||
48 | TILE_WIDTH, | ||
49 | TILE_HEIGHT}; | ||
50 | |||
51 | texture.blit(mappable.getTileset(), std::move(src), std::move(dst)); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (map.getTitle().size() / 2); | ||
56 | for (size_t i = 0; i < map.getTitle().size(); i++) | ||
57 | { | ||
58 | Rectangle src { | ||
59 | (map.getTitle()[i] % FONT_COLS) * TILE_WIDTH, | ||
60 | (map.getTitle()[i] / FONT_COLS) * TILE_HEIGHT, | ||
61 | TILE_WIDTH, | ||
62 | TILE_HEIGHT}; | ||
63 | |||
64 | Rectangle dst { | ||
65 | (startX + static_cast<int>(i)) * TILE_WIDTH, | ||
66 | 24 * TILE_HEIGHT, | ||
67 | TILE_WIDTH, | ||
68 | TILE_HEIGHT}; | ||
69 | |||
70 | texture.blit(mappable.getFont(), std::move(src), std::move(dst)); | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | void MappingSystem::loadMap(size_t mapId) | ||
76 | { | ||
77 | id_type mapEntity = game_.getEntityManager().emplaceEntity(); | ||
78 | |||
79 | auto& mappable = game_.getEntityManager(). | ||
80 | emplaceComponent<MappableComponent>(mapEntity, | ||
81 | Texture("res/tiles.png"), | ||
82 | Texture("res/font.bmp")); | ||
83 | |||
84 | mappable.setMapId(mapId); | ||
85 | |||
86 | const Map& map = game_.getWorld().getMap(mappable.getMapId()); | ||
87 | |||
88 | for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) | ||
89 | { | ||
90 | size_t x = i % MAP_WIDTH; | ||
91 | size_t y = i / MAP_WIDTH; | ||
92 | int tile = map.getTiles()[i]; | ||
93 | |||
94 | if ((tile >= 5) && (tile <= 7)) | ||
95 | { | ||
96 | addBoundary( | ||
97 | mappable.getDownBoundaries(), | ||
98 | y * TILE_HEIGHT, | ||
99 | x * TILE_WIDTH, | ||
100 | (x + 1) * TILE_WIDTH, | ||
101 | MappableComponent::Boundary::Type::platform); | ||
102 | } else if ((tile > 0) && (tile < 28)) | ||
103 | { | ||
104 | addBoundary( | ||
105 | mappable.getRightBoundaries(), | ||
106 | x * TILE_WIDTH, | ||
107 | y * TILE_HEIGHT, | ||
108 | (y+1) * TILE_HEIGHT, | ||
109 | MappableComponent::Boundary::Type::wall); | ||
110 | |||
111 | addBoundary( | ||
112 | mappable.getLeftBoundaries(), | ||
113 | (x+1) * TILE_WIDTH, | ||
114 | y * TILE_HEIGHT, | ||
115 | (y+1) * TILE_HEIGHT, | ||
116 | MappableComponent::Boundary::Type::wall); | ||
117 | |||
118 | addBoundary( | ||
119 | mappable.getDownBoundaries(), | ||
120 | y * TILE_HEIGHT, | ||
121 | x * TILE_WIDTH, | ||
122 | (x+1) * TILE_WIDTH, | ||
123 | MappableComponent::Boundary::Type::wall); | ||
124 | |||
125 | addBoundary( | ||
126 | mappable.getUpBoundaries(), | ||
127 | (y+1) * TILE_HEIGHT, | ||
128 | x * TILE_WIDTH, | ||
129 | (x+1) * TILE_WIDTH, | ||
130 | MappableComponent::Boundary::Type::wall); | ||
131 | } else if (tile == 42) | ||
132 | { | ||
133 | addBoundary( | ||
134 | mappable.getDownBoundaries(), | ||
135 | y * TILE_HEIGHT, | ||
136 | x * TILE_WIDTH, | ||
137 | (x+1) * TILE_WIDTH, | ||
138 | MappableComponent::Boundary::Type::danger); | ||
139 | } | ||
140 | } | ||
141 | } | ||
diff --git a/src/systems/mapping.h b/src/systems/mapping.h new file mode 100644 index 0000000..53d054b --- /dev/null +++ b/src/systems/mapping.h | |||
@@ -0,0 +1,19 @@ | |||
1 | #ifndef MAPPING_H_33FC2294 | ||
2 | #define MAPPING_H_33FC2294 | ||
3 | |||
4 | #include "system.h" | ||
5 | |||
6 | class MappingSystem : public System { | ||
7 | public: | ||
8 | |||
9 | MappingSystem(Game& game) : System(game) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | void render(Texture& texture); | ||
14 | |||
15 | void loadMap(size_t mapId); | ||
16 | |||
17 | }; | ||
18 | |||
19 | #endif /* end of include guard: MAPPING_H_33FC2294 */ | ||
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 | |||
9 | void 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 | |||
54 | void 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 | |||
74 | void 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 | |||
94 | void 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 | |||
117 | void 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 | |||
144 | void 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 | |||
160 | void 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 | |||
197 | void 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 | |||
212 | void 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 | |||
227 | void 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 | |||
6 | class OrientingSystem : public System { | ||
7 | public: | ||
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 new file mode 100644 index 0000000..4a165b1 --- /dev/null +++ b/src/systems/pondering.cpp | |||
@@ -0,0 +1,268 @@ | |||
1 | #include "pondering.h" | ||
2 | #include "game.h" | ||
3 | #include "components/ponderable.h" | ||
4 | #include "components/transformable.h" | ||
5 | #include "components/orientable.h" | ||
6 | #include "components/mappable.h" | ||
7 | #include "systems/orienting.h" | ||
8 | #include "consts.h" | ||
9 | |||
10 | void PonderingSystem::tick(double dt) | ||
11 | { | ||
12 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
13 | PonderableComponent, | ||
14 | TransformableComponent>(); | ||
15 | |||
16 | auto maps = game_.getEntityManager().getEntitiesWithComponents< | ||
17 | MappableComponent>(); | ||
18 | |||
19 | for (id_type entity : entities) | ||
20 | { | ||
21 | auto& transformable = game_.getEntityManager(). | ||
22 | getComponent<TransformableComponent>(entity); | ||
23 | |||
24 | auto& ponderable = game_.getEntityManager(). | ||
25 | getComponent<PonderableComponent>(entity); | ||
26 | |||
27 | // Accelerate | ||
28 | ponderable.setVelocityX( | ||
29 | ponderable.getVelocityX() + ponderable.getAccelX() * dt); | ||
30 | |||
31 | ponderable.setVelocityY( | ||
32 | ponderable.getVelocityY() + ponderable.getAccelY() * dt); | ||
33 | |||
34 | const double oldX = transformable.getX(); | ||
35 | const double oldY = transformable.getY(); | ||
36 | const double oldRight = oldX + transformable.getW(); | ||
37 | const double oldBottom = oldY + transformable.getH(); | ||
38 | |||
39 | double newX = oldX + ponderable.getVelocityX() * dt; | ||
40 | double newY = oldY + ponderable.getVelocityY() * dt; | ||
41 | |||
42 | bool oldGrounded = ponderable.isGrounded(); | ||
43 | ponderable.setGrounded(false); | ||
44 | |||
45 | for (id_type mapEntity : maps) | ||
46 | { | ||
47 | auto& mappable = game_.getEntityManager(). | ||
48 | getComponent<MappableComponent>(mapEntity); | ||
49 | |||
50 | if (newX < oldX) | ||
51 | { | ||
52 | for (auto it = mappable.getLeftBoundaries().lower_bound(oldX); | ||
53 | (it != std::end(mappable.getLeftBoundaries())) && (it->first >= newX); | ||
54 | it++) | ||
55 | { | ||
56 | if ((oldBottom > it->second.getLower()) | ||
57 | && (oldY < it->second.getUpper())) | ||
58 | { | ||
59 | // We have a collision! | ||
60 | processCollision( | ||
61 | entity, | ||
62 | Direction::left, | ||
63 | newX, | ||
64 | newY, | ||
65 | it->first, | ||
66 | it->second.getType()); | ||
67 | } | ||
68 | } | ||
69 | } else if (newX > oldX) | ||
70 | { | ||
71 | for (auto it = mappable.getRightBoundaries().lower_bound(oldRight); | ||
72 | (it != std::end(mappable.getRightBoundaries())) | ||
73 | && (it->first <= (newX + transformable.getW())); | ||
74 | it++) | ||
75 | { | ||
76 | if ((oldBottom > it->second.getLower()) | ||
77 | && (oldY < it->second.getUpper())) | ||
78 | { | ||
79 | // We have a collision! | ||
80 | processCollision( | ||
81 | entity, | ||
82 | Direction::right, | ||
83 | newX, | ||
84 | newY, | ||
85 | it->first, | ||
86 | it->second.getType()); | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | if (newY < oldY) | ||
92 | { | ||
93 | for (auto it = mappable.getUpBoundaries().lower_bound(oldY); | ||
94 | (it != std::end(mappable.getUpBoundaries())) && (it->first >= newY); | ||
95 | it++) | ||
96 | { | ||
97 | if ((oldRight > it->second.getLower()) | ||
98 | && (oldX < it->second.getUpper())) | ||
99 | { | ||
100 | // We have a collision! | ||
101 | processCollision( | ||
102 | entity, | ||
103 | Direction::up, | ||
104 | newX, | ||
105 | newY, | ||
106 | it->first, | ||
107 | it->second.getType()); | ||
108 | } | ||
109 | } | ||
110 | } else if (newY > oldY) | ||
111 | { | ||
112 | for (auto it = mappable.getDownBoundaries().lower_bound(oldBottom); | ||
113 | (it != std::end(mappable.getDownBoundaries())) | ||
114 | && (it->first <= (newY + transformable.getH())); | ||
115 | it++) | ||
116 | { | ||
117 | if ((oldRight > it->second.getLower()) | ||
118 | && (oldX < it->second.getUpper())) | ||
119 | { | ||
120 | // We have a collision! | ||
121 | processCollision( | ||
122 | entity, | ||
123 | Direction::down, | ||
124 | newX, | ||
125 | newY, | ||
126 | it->first, | ||
127 | it->second.getType()); | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | |||
133 | // Move | ||
134 | transformable.setX(newX); | ||
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 | } | ||
161 | } | ||
162 | } | ||
163 | |||
164 | void PonderingSystem::initializeBody( | ||
165 | id_type entity, | ||
166 | PonderableComponent::Type type) | ||
167 | { | ||
168 | auto& ponderable = game_.getEntityManager(). | ||
169 | emplaceComponent<PonderableComponent>(entity, type); | ||
170 | |||
171 | if (type == PonderableComponent::Type::freefalling) | ||
172 | { | ||
173 | ponderable.setAccelY(NORMAL_GRAVITY); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | void PonderingSystem::processCollision( | ||
178 | id_type entity, | ||
179 | Direction dir, | ||
180 | double& newX, | ||
181 | double& newY, | ||
182 | int axis, | ||
183 | MappableComponent::Boundary::Type type) | ||
184 | { | ||
185 | auto& ponderable = game_.getEntityManager(). | ||
186 | getComponent<PonderableComponent>(entity); | ||
187 | |||
188 | auto& transformable = game_.getEntityManager(). | ||
189 | getComponent<TransformableComponent>(entity); | ||
190 | |||
191 | bool touchedGround = false; | ||
192 | |||
193 | switch (type) | ||
194 | { | ||
195 | case MappableComponent::Boundary::Type::wall: | ||
196 | { | ||
197 | switch (dir) | ||
198 | { | ||
199 | case Direction::left: | ||
200 | { | ||
201 | newX = axis; | ||
202 | ponderable.setVelocityX(0.0); | ||
203 | |||
204 | break; | ||
205 | } | ||
206 | |||
207 | case Direction::right: | ||
208 | { | ||
209 | newX = axis - transformable.getW(); | ||
210 | ponderable.setVelocityX(0.0); | ||
211 | |||
212 | break; | ||
213 | } | ||
214 | |||
215 | case Direction::up: | ||
216 | { | ||
217 | newY = axis; | ||
218 | ponderable.setVelocityY(0.0); | ||
219 | |||
220 | break; | ||
221 | } | ||
222 | |||
223 | case Direction::down: | ||
224 | { | ||
225 | touchedGround = true; | ||
226 | |||
227 | break; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | break; | ||
232 | } | ||
233 | |||
234 | case MappableComponent::Boundary::Type::platform: | ||
235 | { | ||
236 | if (game_.getEntityManager().hasComponent<OrientableComponent>(entity)) | ||
237 | { | ||
238 | auto& orientable = game_.getEntityManager(). | ||
239 | getComponent<OrientableComponent>(entity); | ||
240 | |||
241 | if (orientable.getDropState() != OrientableComponent::DropState::none) | ||
242 | { | ||
243 | orientable.setDropState(OrientableComponent::DropState::active); | ||
244 | } else { | ||
245 | touchedGround = true; | ||
246 | } | ||
247 | } else { | ||
248 | touchedGround = true; | ||
249 | } | ||
250 | |||
251 | break; | ||
252 | } | ||
253 | |||
254 | default: | ||
255 | { | ||
256 | // Not yet implemented. | ||
257 | |||
258 | break; | ||
259 | } | ||
260 | } | ||
261 | |||
262 | if (touchedGround) | ||
263 | { | ||
264 | newY = axis - transformable.getH(); | ||
265 | ponderable.setVelocityY(0.0); | ||
266 | ponderable.setGrounded(true); | ||
267 | } | ||
268 | } | ||
diff --git a/src/systems/pondering.h b/src/systems/pondering.h new file mode 100644 index 0000000..a16622b --- /dev/null +++ b/src/systems/pondering.h | |||
@@ -0,0 +1,32 @@ | |||
1 | #ifndef PONDERING_H_F2530E0E | ||
2 | #define PONDERING_H_F2530E0E | ||
3 | |||
4 | #include "system.h" | ||
5 | #include "components/mappable.h" | ||
6 | #include "components/ponderable.h" | ||
7 | #include "direction.h" | ||
8 | |||
9 | class PonderingSystem : public System { | ||
10 | public: | ||
11 | |||
12 | PonderingSystem(Game& game) : System(game) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | void tick(double dt); | ||
17 | |||
18 | void initializeBody(id_type entity, PonderableComponent::Type type); | ||
19 | |||
20 | private: | ||
21 | |||
22 | void processCollision( | ||
23 | id_type entity, | ||
24 | Direction dir, | ||
25 | double& newX, | ||
26 | double& newY, | ||
27 | int axis, | ||
28 | MappableComponent::Boundary::Type type); | ||
29 | |||
30 | }; | ||
31 | |||
32 | #endif /* end of include guard: PONDERING_H_F2530E0E */ | ||