diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/ai.cpp | 142 | ||||
-rw-r--r-- | src/components/ai.h | 73 | ||||
-rw-r--r-- | src/components/map_collision.cpp | 241 | ||||
-rw-r--r-- | src/components/map_collision.h | 47 | ||||
-rw-r--r-- | src/components/map_render.cpp | 40 | ||||
-rw-r--r-- | src/components/map_render.h | 19 | ||||
-rw-r--r-- | src/components/physics_body.cpp | 66 | ||||
-rw-r--r-- | src/components/physics_body.h | 20 | ||||
-rw-r--r-- | src/components/player_physics.cpp | 118 | ||||
-rw-r--r-- | src/components/player_physics.h | 25 | ||||
-rw-r--r-- | src/components/player_sprite.cpp | 60 | ||||
-rw-r--r-- | src/components/player_sprite.h | 23 | ||||
-rw-r--r-- | src/components/simple_collider.cpp | 14 | ||||
-rw-r--r-- | src/components/simple_collider.h | 18 | ||||
-rw-r--r-- | src/components/static_image.cpp | 11 | ||||
-rw-r--r-- | src/components/static_image.h | 18 | ||||
-rw-r--r-- | src/components/user_movement.cpp | 100 | ||||
-rw-r--r-- | src/components/user_movement.h | 19 |
18 files changed, 0 insertions, 1054 deletions
diff --git a/src/components/ai.cpp b/src/components/ai.cpp deleted file mode 100644 index 9f8c764..0000000 --- a/src/components/ai.cpp +++ /dev/null | |||
@@ -1,142 +0,0 @@ | |||
1 | #include "ai.h" | ||
2 | #include <cstdlib> | ||
3 | #include "entity.h" | ||
4 | |||
5 | void AIActionContainer::addAction(std::shared_ptr<AIAction> action) | ||
6 | { | ||
7 | actions.push_back(action); | ||
8 | } | ||
9 | |||
10 | void AIActionContainer::start(Game& game, Entity& entity) | ||
11 | { | ||
12 | currentAction = begin(actions); | ||
13 | |||
14 | if (currentAction != end(actions)) | ||
15 | { | ||
16 | (*currentAction)->start(game, entity); | ||
17 | } | ||
18 | } | ||
19 | |||
20 | void AIActionContainer::perform(Game& game, Entity& entity, double dt) | ||
21 | { | ||
22 | if (!isDone()) | ||
23 | { | ||
24 | (*currentAction)->perform(game, entity, dt); | ||
25 | |||
26 | if ((*currentAction)->isDone()) | ||
27 | { | ||
28 | currentAction++; | ||
29 | |||
30 | if (!isDone()) | ||
31 | { | ||
32 | (*currentAction)->start(game, entity); | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | bool AIActionContainer::isDone() const | ||
39 | { | ||
40 | return currentAction == end(actions); | ||
41 | } | ||
42 | |||
43 | AI::AI(int chance) | ||
44 | { | ||
45 | this->chance = chance; | ||
46 | } | ||
47 | |||
48 | int AI::getChance() const | ||
49 | { | ||
50 | return chance; | ||
51 | } | ||
52 | |||
53 | AI& AIComponent::emplaceAI(int chance) | ||
54 | { | ||
55 | maxChance += chance; | ||
56 | ais.emplace_back(chance); | ||
57 | |||
58 | return ais.back(); | ||
59 | } | ||
60 | |||
61 | void AIComponent::tick(Game& game, Entity& entity, double dt) | ||
62 | { | ||
63 | if (currentAI == nullptr) | ||
64 | { | ||
65 | int toChoose = rand() % maxChance; | ||
66 | for (auto& ai : ais) | ||
67 | { | ||
68 | if (toChoose < ai.getChance()) | ||
69 | { | ||
70 | currentAI = &ai; | ||
71 | break; | ||
72 | } else { | ||
73 | toChoose -= ai.getChance(); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | if (currentAI != nullptr) | ||
78 | { | ||
79 | currentAI->start(game, entity); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | if (currentAI != nullptr) | ||
84 | { | ||
85 | currentAI->perform(game, entity, dt); | ||
86 | |||
87 | if (currentAI->isDone()) | ||
88 | { | ||
89 | currentAI = nullptr; | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | |||
94 | MoveAIAction::MoveAIAction(Direction dir, int len, int speed) | ||
95 | { | ||
96 | this->dir = dir; | ||
97 | this->len = len; | ||
98 | this->speed = speed; | ||
99 | } | ||
100 | |||
101 | void MoveAIAction::start(Game& game, Entity& entity) | ||
102 | { | ||
103 | remaining = len; | ||
104 | } | ||
105 | |||
106 | void MoveAIAction::perform(Game&, Entity& entity, double dt) | ||
107 | { | ||
108 | double dist = dt * speed; | ||
109 | remaining -= dist; | ||
110 | |||
111 | switch (dir) | ||
112 | { | ||
113 | case Direction::Left: | ||
114 | { | ||
115 | entity.position.first -= dist; | ||
116 | break; | ||
117 | } | ||
118 | |||
119 | case Direction::Right: | ||
120 | { | ||
121 | entity.position.first += dist; | ||
122 | break; | ||
123 | } | ||
124 | |||
125 | case Direction::Up: | ||
126 | { | ||
127 | entity.position.second -= dist; | ||
128 | break; | ||
129 | } | ||
130 | |||
131 | case Direction::Down: | ||
132 | { | ||
133 | entity.position.second += dist; | ||
134 | break; | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | bool MoveAIAction::isDone() const | ||
140 | { | ||
141 | return remaining <= 0.0; | ||
142 | } | ||
diff --git a/src/components/ai.h b/src/components/ai.h deleted file mode 100644 index 840283b..0000000 --- a/src/components/ai.h +++ /dev/null | |||
@@ -1,73 +0,0 @@ | |||
1 | #ifndef AI_H | ||
2 | #define AI_H | ||
3 | |||
4 | #include <list> | ||
5 | #include <map> | ||
6 | #include <string> | ||
7 | #include <memory> | ||
8 | |||
9 | #include "entity.h" | ||
10 | |||
11 | class AIAction { | ||
12 | public: | ||
13 | virtual void start(Game& game, Entity& entity) = 0; | ||
14 | virtual void perform(Game& game, Entity& entity, double dt) = 0; | ||
15 | virtual bool isDone() const = 0; | ||
16 | }; | ||
17 | |||
18 | class AIActionContainer { | ||
19 | public: | ||
20 | void addAction(std::shared_ptr<AIAction> action); | ||
21 | virtual void start(Game& game, Entity& entity); | ||
22 | virtual void perform(Game& game, Entity& entity, double dt); | ||
23 | virtual bool isDone() const; | ||
24 | |||
25 | private: | ||
26 | std::list<std::shared_ptr<AIAction>> actions; | ||
27 | std::list<std::shared_ptr<AIAction>>::iterator currentAction {end(actions)}; | ||
28 | }; | ||
29 | |||
30 | class AI : public AIActionContainer { | ||
31 | public: | ||
32 | AI(int chance); | ||
33 | |||
34 | int getChance() const; | ||
35 | |||
36 | private: | ||
37 | int chance; | ||
38 | }; | ||
39 | |||
40 | class AIComponent : public Component { | ||
41 | public: | ||
42 | AI& emplaceAI(int chance); | ||
43 | void tick(Game& game, Entity& entity, double dt); | ||
44 | |||
45 | private: | ||
46 | int maxChance = 0; | ||
47 | std::list<AI> ais; | ||
48 | AI* currentAI = nullptr; | ||
49 | }; | ||
50 | |||
51 | class MoveAIAction : public AIAction { | ||
52 | public: | ||
53 | enum class Direction { | ||
54 | Left, | ||
55 | Right, | ||
56 | Up, | ||
57 | Down | ||
58 | }; | ||
59 | |||
60 | MoveAIAction(Direction dir, int len, int speed); | ||
61 | |||
62 | void start(Game& game, Entity& entity); | ||
63 | void perform(Game& game, Entity& entity, double dt); | ||
64 | bool isDone() const; | ||
65 | |||
66 | private: | ||
67 | Direction dir; | ||
68 | int len; | ||
69 | int speed; | ||
70 | double remaining; | ||
71 | }; | ||
72 | |||
73 | #endif /* end of include guard: AI_H */ | ||
diff --git a/src/components/map_collision.cpp b/src/components/map_collision.cpp deleted file mode 100644 index 3ad574b..0000000 --- a/src/components/map_collision.cpp +++ /dev/null | |||
@@ -1,241 +0,0 @@ | |||
1 | #include "map_collision.h" | ||
2 | #include "map.h" | ||
3 | #include "game.h" | ||
4 | #include "consts.h" | ||
5 | |||
6 | MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) | ||
7 | { | ||
8 | addCollision(-6, 0, MAP_HEIGHT*TILE_HEIGHT, Direction::left, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Left).type)); | ||
9 | addCollision(GAME_WIDTH+6, 0, MAP_HEIGHT*TILE_HEIGHT, Direction::right, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Right).type)); | ||
10 | addCollision(-6, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Up).type)); | ||
11 | addCollision(MAP_HEIGHT*TILE_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getAdjacent(Map::MoveDir::Down).type)); | ||
12 | |||
13 | for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++) | ||
14 | { | ||
15 | int x = i % MAP_WIDTH; | ||
16 | int y = i / MAP_WIDTH; | ||
17 | int tile = map.getMapdata()[i]; | ||
18 | |||
19 | if ((tile > 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7)))) | ||
20 | { | ||
21 | addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, Collision::Type::wall); | ||
22 | addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, Collision::Type::wall); | ||
23 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::wall); | ||
24 | addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, Collision::Type::wall); | ||
25 | } else if ((tile >= 5) && (tile <= 7)) | ||
26 | { | ||
27 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::platform); | ||
28 | } else if (tile == 42) | ||
29 | { | ||
30 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::danger); | ||
31 | } | ||
32 | } | ||
33 | } | ||
34 | |||
35 | void MapCollisionComponent::addCollision(double axis, double lower, double | ||
36 | upper, Direction dir, Collision::Type type) | ||
37 | { | ||
38 | std::list<Collision>::iterator it; | ||
39 | |||
40 | switch (dir) | ||
41 | { | ||
42 | case Direction::up: | ||
43 | it = up_collisions.begin(); | ||
44 | for (; it!=up_collisions.end(); it++) | ||
45 | { | ||
46 | if (it->axis < axis) break; | ||
47 | } | ||
48 | |||
49 | up_collisions.insert(it, {axis, lower, upper, type}); | ||
50 | |||
51 | break; | ||
52 | case Direction::down: | ||
53 | it = down_collisions.begin(); | ||
54 | for (; it!=down_collisions.end(); it++) | ||
55 | { | ||
56 | if (it->axis > axis) break; | ||
57 | } | ||
58 | |||
59 | down_collisions.insert(it, {axis, lower, upper, type}); | ||
60 | |||
61 | break; | ||
62 | case Direction::left: | ||
63 | it = left_collisions.begin(); | ||
64 | for (; it!=left_collisions.end(); it++) | ||
65 | { | ||
66 | if (it->axis < axis) break; | ||
67 | } | ||
68 | |||
69 | left_collisions.insert(it, {axis, lower, upper, type}); | ||
70 | |||
71 | break; | ||
72 | case Direction::right: | ||
73 | it = right_collisions.begin(); | ||
74 | for (; it!=right_collisions.end(); it++) | ||
75 | { | ||
76 | if (it->axis > axis) break; | ||
77 | } | ||
78 | |||
79 | right_collisions.insert(it, {axis, lower, upper, type}); | ||
80 | |||
81 | break; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collider, std::pair<double, double> old_position) | ||
86 | { | ||
87 | if (collider.position.first < old_position.first) | ||
88 | { | ||
89 | for (auto collision : left_collisions) | ||
90 | { | ||
91 | if (collision.axis > old_position.first) continue; | ||
92 | if (collision.axis < collider.position.first) break; | ||
93 | |||
94 | if ((old_position.second+collider.size.second > collision.lower) && (old_position.second < collision.upper)) | ||
95 | { | ||
96 | // We have a collision! | ||
97 | processCollision(game, collider, collision, Direction::left, old_position); | ||
98 | |||
99 | break; | ||
100 | } | ||
101 | } | ||
102 | } else if (collider.position.first > old_position.first) | ||
103 | { | ||
104 | for (auto collision : right_collisions) | ||
105 | { | ||
106 | if (collision.axis < old_position.first+collider.size.first) continue; | ||
107 | if (collision.axis > collider.position.first+collider.size.first) break; | ||
108 | |||
109 | if ((old_position.second+collider.size.second > collision.lower) && (old_position.second < collision.upper)) | ||
110 | { | ||
111 | // We have a collision! | ||
112 | processCollision(game, collider, collision, Direction::right, old_position); | ||
113 | |||
114 | break; | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | if (collider.position.second < old_position.second) | ||
120 | { | ||
121 | for (auto collision : up_collisions) | ||
122 | { | ||
123 | if (collision.axis > old_position.second) continue; | ||
124 | if (collision.axis < collider.position.second) break; | ||
125 | |||
126 | if ((collider.position.first+collider.size.first > collision.lower) && (collider.position.first < collision.upper)) | ||
127 | { | ||
128 | // We have a collision! | ||
129 | processCollision(game, collider, collision, Direction::up, old_position); | ||
130 | |||
131 | break; | ||
132 | } | ||
133 | } | ||
134 | } else if (collider.position.second > old_position.second) | ||
135 | { | ||
136 | for (auto collision : down_collisions) | ||
137 | { | ||
138 | if (collision.axis < old_position.second+collider.size.second) continue; | ||
139 | if (collision.axis > collider.position.second+collider.size.second) break; | ||
140 | |||
141 | if ((collider.position.first+collider.size.first > collision.lower) && (collider.position.first < collision.upper)) | ||
142 | { | ||
143 | // We have a collision! | ||
144 | processCollision(game, collider, collision, Direction::down, old_position); | ||
145 | |||
146 | break; | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | void MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir, std::pair<double, double> old_position) | ||
153 | { | ||
154 | if (collision.type == Collision::Type::wall) | ||
155 | { | ||
156 | if (dir == Direction::left) | ||
157 | { | ||
158 | collider.position.first = collision.axis; | ||
159 | |||
160 | Message msg(Message::Type::setHorizontalVelocity); | ||
161 | msg.velocity = 0.0; | ||
162 | collider.send(game, msg); | ||
163 | } else if (dir == Direction::right) | ||
164 | { | ||
165 | collider.position.first = collision.axis - collider.size.first; | ||
166 | |||
167 | Message msg(Message::Type::setHorizontalVelocity); | ||
168 | msg.velocity = 0.0; | ||
169 | collider.send(game, msg); | ||
170 | } else if (dir == Direction::up) | ||
171 | { | ||
172 | collider.position.second = collision.axis; | ||
173 | |||
174 | Message msg(Message::Type::setVerticalVelocity); | ||
175 | msg.velocity = 0.0; | ||
176 | collider.send(game, msg); | ||
177 | } else if (dir == Direction::down) | ||
178 | { | ||
179 | collider.position.second = collision.axis - collider.size.second; | ||
180 | collider.send(game, Message::Type::hitTheGround); | ||
181 | } | ||
182 | } else if (collision.type == Collision::Type::wrap) | ||
183 | { | ||
184 | if (dir == Direction::left) | ||
185 | { | ||
186 | collider.position.first = GAME_WIDTH-collider.size.first/2; | ||
187 | } else if (dir == Direction::right) | ||
188 | { | ||
189 | collider.position.first = -collider.size.first/2; | ||
190 | } else if (dir == Direction::up) | ||
191 | { | ||
192 | collider.position.second = GAME_HEIGHT-collider.size.second/2-1; | ||
193 | } else if (dir == Direction::down) | ||
194 | { | ||
195 | collider.position.second = -collider.size.second/2; | ||
196 | } | ||
197 | } else if (collision.type == Collision::Type::teleport) | ||
198 | { | ||
199 | if (dir == Direction::left) | ||
200 | { | ||
201 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Left).map), std::make_pair(GAME_WIDTH-collider.size.first/2, old_position.second)); | ||
202 | } else if (dir == Direction::right) | ||
203 | { | ||
204 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Right).map), std::make_pair(-collider.size.first/2, old_position.second)); | ||
205 | } else if (dir == Direction::up) | ||
206 | { | ||
207 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Up).map), std::make_pair(old_position.first, MAP_HEIGHT*TILE_HEIGHT-collider.size.second/2)); | ||
208 | } else if (dir == Direction::down) | ||
209 | { | ||
210 | game.loadMap(game.getWorld().getMap(map.getAdjacent(Map::MoveDir::Down).map), std::make_pair(old_position.first, -collider.size.second/2)); | ||
211 | } | ||
212 | } else if (collision.type == Collision::Type::reverse) | ||
213 | { | ||
214 | // TODO reverse | ||
215 | if (dir == Direction::right) | ||
216 | { | ||
217 | collider.position.first = collision.axis - collider.size.first; | ||
218 | collider.send(game, Message::Type::walkLeft); | ||
219 | } | ||
220 | } else if (collision.type == Collision::Type::platform) | ||
221 | { | ||
222 | Message msg(Message::Type::drop); | ||
223 | msg.dropAxis = collision.axis; | ||
224 | |||
225 | collider.send(game, msg); | ||
226 | } else if (collision.type == Collision::Type::danger) | ||
227 | { | ||
228 | game.playerDie(); | ||
229 | } | ||
230 | } | ||
231 | |||
232 | MapCollisionComponent::Collision::Type MapCollisionComponent::collisionFromMoveType(Map::MoveType type) | ||
233 | { | ||
234 | switch (type) | ||
235 | { | ||
236 | case Map::MoveType::Wall: return Collision::Type::wall; | ||
237 | case Map::MoveType::Wrap: return Collision::Type::wrap; | ||
238 | case Map::MoveType::Warp: return Collision::Type::teleport; | ||
239 | case Map::MoveType::ReverseWarp: return Collision::Type::reverse; | ||
240 | } | ||
241 | } | ||
diff --git a/src/components/map_collision.h b/src/components/map_collision.h deleted file mode 100644 index 18b9397..0000000 --- a/src/components/map_collision.h +++ /dev/null | |||
@@ -1,47 +0,0 @@ | |||
1 | #ifndef MAP_COLLISION_H | ||
2 | #define MAP_COLLISION_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include "map.h" | ||
6 | #include <list> | ||
7 | |||
8 | class Game; | ||
9 | |||
10 | class MapCollisionComponent : public Component { | ||
11 | public: | ||
12 | MapCollisionComponent(const Map& map); | ||
13 | void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position); | ||
14 | |||
15 | private: | ||
16 | enum class Direction { | ||
17 | up, left, down, right | ||
18 | }; | ||
19 | |||
20 | struct Collision { | ||
21 | enum class Type { | ||
22 | wall, | ||
23 | wrap, | ||
24 | teleport, | ||
25 | reverse, | ||
26 | platform, | ||
27 | danger | ||
28 | }; | ||
29 | |||
30 | double axis; | ||
31 | double lower; | ||
32 | double upper; | ||
33 | Type type; | ||
34 | }; | ||
35 | |||
36 | void addCollision(double axis, double lower, double upper, Direction dir, Collision::Type type); | ||
37 | void processCollision(Game& game, Entity& collider, Collision collision, Direction dir, std::pair<double, double> old_position); | ||
38 | Collision::Type collisionFromMoveType(Map::MoveType type); | ||
39 | |||
40 | std::list<Collision> left_collisions; | ||
41 | std::list<Collision> right_collisions; | ||
42 | std::list<Collision> up_collisions; | ||
43 | std::list<Collision> down_collisions; | ||
44 | const Map& map; | ||
45 | }; | ||
46 | |||
47 | #endif | ||
diff --git a/src/components/map_render.cpp b/src/components/map_render.cpp deleted file mode 100644 index 45766e1..0000000 --- a/src/components/map_render.cpp +++ /dev/null | |||
@@ -1,40 +0,0 @@ | |||
1 | #include "map_render.h" | ||
2 | #include "map.h" | ||
3 | #include "game.h" | ||
4 | #include "consts.h" | ||
5 | |||
6 | MapRenderComponent::MapRenderComponent(const Map& map) : screen(GAME_WIDTH, GAME_HEIGHT) | ||
7 | { | ||
8 | screen.fill(screen.entirety(), 0, 0, 0); | ||
9 | |||
10 | Texture tiles("res/tiles.png"); | ||
11 | |||
12 | for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++) | ||
13 | { | ||
14 | int tile = map.getMapdata()[i]; | ||
15 | int x = i % MAP_WIDTH; | ||
16 | int y = i / MAP_WIDTH; | ||
17 | Rectangle dst {x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; | ||
18 | Rectangle src {tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; | ||
19 | |||
20 | if (tile > 0) | ||
21 | { | ||
22 | screen.blit(tiles, src, dst); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | Texture font("res/font.bmp"); | ||
27 | std::string map_name = map.getTitle(); | ||
28 | int start_x = (40/2) - (map_name.length()/2); | ||
29 | for (size_t i=0; i<map_name.length(); i++) | ||
30 | { | ||
31 | Rectangle srcRect {map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8}; | ||
32 | Rectangle dstRect {(start_x + (int)i)*8, 24*8, 8, 8}; | ||
33 | screen.blit(font, srcRect, dstRect); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | void MapRenderComponent::render(Game&, Entity&, Texture& buffer) | ||
38 | { | ||
39 | buffer.blit(screen, screen.entirety(), buffer.entirety()); | ||
40 | } | ||
diff --git a/src/components/map_render.h b/src/components/map_render.h deleted file mode 100644 index a232aa6..0000000 --- a/src/components/map_render.h +++ /dev/null | |||
@@ -1,19 +0,0 @@ | |||
1 | #ifndef MAP_RENDER_H | ||
2 | #define MAP_RENDER_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include "renderer.h" | ||
6 | |||
7 | class Map; | ||
8 | class Game; | ||
9 | |||
10 | class MapRenderComponent : public Component { | ||
11 | public: | ||
12 | MapRenderComponent(const Map& map); | ||
13 | void render(Game& game, Entity& entity, Texture& buffer); | ||
14 | |||
15 | private: | ||
16 | Texture screen; | ||
17 | }; | ||
18 | |||
19 | #endif | ||
diff --git a/src/components/physics_body.cpp b/src/components/physics_body.cpp deleted file mode 100644 index 97394d1..0000000 --- a/src/components/physics_body.cpp +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | #include "physics_body.h" | ||
2 | #include "game.h" | ||
3 | #include "consts.h" | ||
4 | |||
5 | void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) | ||
6 | { | ||
7 | if (msg.type == Message::Type::walkLeft) | ||
8 | { | ||
9 | velocity.first = -90; | ||
10 | } else if (msg.type == Message::Type::walkRight) | ||
11 | { | ||
12 | velocity.first = 90; | ||
13 | } else if (msg.type == Message::Type::stopWalking) | ||
14 | { | ||
15 | velocity.first = 0.0; | ||
16 | } else if (msg.type == Message::Type::setHorizontalVelocity) | ||
17 | { | ||
18 | velocity.first = msg.velocity; | ||
19 | } else if (msg.type == Message::Type::setVerticalVelocity) | ||
20 | { | ||
21 | velocity.second = msg.velocity; | ||
22 | } | ||
23 | } | ||
24 | |||
25 | void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt) | ||
26 | { | ||
27 | // Accelerate | ||
28 | velocity.first += accel.first * dt; | ||
29 | velocity.second += accel.second * dt; | ||
30 | |||
31 | // Terminal velocity | ||
32 | #define TERMINAL_VELOCITY_X (2 * TILE_WIDTH * FRAMES_PER_SECOND) | ||
33 | #define TERMINAL_VELOCITY_Y (2 * TILE_HEIGHT * FRAMES_PER_SECOND) | ||
34 | if (velocity.first < -TERMINAL_VELOCITY_X) velocity.first = -TERMINAL_VELOCITY_X; | ||
35 | if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X; | ||
36 | if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y; | ||
37 | if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y; | ||
38 | |||
39 | // Do the movement | ||
40 | entity.position.first += velocity.first * dt; | ||
41 | entity.position.second += velocity.second * dt; | ||
42 | } | ||
43 | |||
44 | void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) | ||
45 | { | ||
46 | // If already colliding, do nothing! | ||
47 | if ((old_position.first + collider.size.first > entity.position.first) | ||
48 | && (old_position.first < entity.position.first + entity.size.first) | ||
49 | && (old_position.second + collider.size.second > entity.position.second) | ||
50 | && (old_position.second < entity.position.second + entity.size.second)) | ||
51 | { | ||
52 | return; | ||
53 | } | ||
54 | |||
55 | // If newly colliding, SHOCK AND HORROR! | ||
56 | if ((collider.position.first + collider.size.first > entity.position.first) | ||
57 | && (collider.position.first < entity.position.first + entity.size.first) | ||
58 | && (collider.position.second + collider.size.second > entity.position.second) | ||
59 | && (collider.position.second < entity.position.second + entity.size.second)) | ||
60 | { | ||
61 | Message msg(Message::Type::collision); | ||
62 | msg.collisionEntity = &collider; | ||
63 | |||
64 | entity.send(game, msg); | ||
65 | } | ||
66 | } | ||
diff --git a/src/components/physics_body.h b/src/components/physics_body.h deleted file mode 100644 index 079cc51..0000000 --- a/src/components/physics_body.h +++ /dev/null | |||
@@ -1,20 +0,0 @@ | |||
1 | #ifndef PHYSICS_BODY_H | ||
2 | #define PHYSICS_BODY_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include <utility> | ||
6 | |||
7 | class Game; | ||
8 | |||
9 | class PhysicsBodyComponent : public Component { | ||
10 | public: | ||
11 | void receive(Game& game, Entity& entity, const Message& msg); | ||
12 | void tick(Game& game, Entity& entity, double dt); | ||
13 | void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position); | ||
14 | |||
15 | protected: | ||
16 | std::pair<double, double> velocity; | ||
17 | std::pair<double, double> accel; | ||
18 | }; | ||
19 | |||
20 | #endif | ||
diff --git a/src/components/player_physics.cpp b/src/components/player_physics.cpp deleted file mode 100644 index 40e9948..0000000 --- a/src/components/player_physics.cpp +++ /dev/null | |||
@@ -1,118 +0,0 @@ | |||
1 | #include "player_physics.h" | ||
2 | #include "muxer.h" | ||
3 | #include "game.h" | ||
4 | #include "consts.h" | ||
5 | |||
6 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) | ||
7 | #define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) | ||
8 | |||
9 | PlayerPhysicsComponent::PlayerPhysicsComponent() | ||
10 | { | ||
11 | jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3); | ||
12 | jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3); | ||
13 | jump_gravity_short = JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233); | ||
14 | |||
15 | accel.second = jump_gravity_short; | ||
16 | } | ||
17 | |||
18 | void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) | ||
19 | { | ||
20 | if (msg.type == Message::Type::walkLeft) | ||
21 | { | ||
22 | velocity.first = -90; | ||
23 | direction = -1; | ||
24 | } else if (msg.type == Message::Type::walkRight) | ||
25 | { | ||
26 | velocity.first = 90; | ||
27 | direction = 1; | ||
28 | } else if (msg.type == Message::Type::stopWalking) | ||
29 | { | ||
30 | velocity.first = 0.0; | ||
31 | direction = 0; | ||
32 | } else if (msg.type == Message::Type::setHorizontalVelocity) | ||
33 | { | ||
34 | velocity.first = msg.velocity; | ||
35 | } else if (msg.type == Message::Type::setVerticalVelocity) | ||
36 | { | ||
37 | velocity.second = msg.velocity; | ||
38 | } else if (msg.type == Message::Type::hitTheGround) | ||
39 | { | ||
40 | if (isFalling) | ||
41 | { | ||
42 | playSound("res/Randomize27.wav", 0.05); | ||
43 | isFalling = false; | ||
44 | } | ||
45 | |||
46 | velocity.second = 0.0; | ||
47 | } else if (msg.type == Message::Type::jump) | ||
48 | { | ||
49 | playSound("res/Randomize87.wav", 0.25); | ||
50 | |||
51 | velocity.second = jump_velocity; | ||
52 | accel.second = jump_gravity; | ||
53 | } else if (msg.type == Message::Type::stopJump) | ||
54 | { | ||
55 | accel.second = jump_gravity_short; | ||
56 | } else if (msg.type == Message::Type::canDrop) | ||
57 | { | ||
58 | canDrop = true; | ||
59 | } else if (msg.type == Message::Type::cantDrop) | ||
60 | { | ||
61 | canDrop = false; | ||
62 | } else if (msg.type == Message::Type::drop) | ||
63 | { | ||
64 | if (canDrop) | ||
65 | { | ||
66 | canDrop = false; | ||
67 | } else { | ||
68 | entity.position.second = msg.dropAxis - entity.size.second; | ||
69 | velocity.second = 0; | ||
70 | } | ||
71 | } else if (msg.type == Message::Type::die) | ||
72 | { | ||
73 | frozen = true; | ||
74 | } else if (msg.type == Message::Type::stopDying) | ||
75 | { | ||
76 | frozen = false; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity, double dt) | ||
81 | { | ||
82 | // If frozen, do nothing | ||
83 | if (frozen) | ||
84 | { | ||
85 | return; | ||
86 | } | ||
87 | |||
88 | // Continue walking even if blocked earlier | ||
89 | if (velocity.first == 0) | ||
90 | { | ||
91 | if (direction < 0) | ||
92 | { | ||
93 | velocity.first = -90; | ||
94 | } else if (direction > 0) | ||
95 | { | ||
96 | velocity.first = 90; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | // Increase gravity at the height of jump | ||
101 | if ((accel.second == jump_gravity) && (velocity.second >= 0)) | ||
102 | { | ||
103 | accel.second = jump_gravity_short; | ||
104 | } | ||
105 | |||
106 | // Do the movement | ||
107 | std::pair<double, double> old_position = entity.position; | ||
108 | PhysicsBodyComponent::tick(game, entity, dt); | ||
109 | |||
110 | // Check for collisions | ||
111 | game.detectCollision(entity, old_position); | ||
112 | |||
113 | // Are we moving due to gravity? | ||
114 | if (velocity.second != 0.0) | ||
115 | { | ||
116 | isFalling = true; | ||
117 | } | ||
118 | } | ||
diff --git a/src/components/player_physics.h b/src/components/player_physics.h deleted file mode 100644 index 26f1fae..0000000 --- a/src/components/player_physics.h +++ /dev/null | |||
@@ -1,25 +0,0 @@ | |||
1 | #ifndef PLAYER_PHYSICS_H | ||
2 | #define PLAYER_PHYSICS_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include "physics_body.h" | ||
6 | |||
7 | class Game; | ||
8 | |||
9 | class PlayerPhysicsComponent : public PhysicsBodyComponent { | ||
10 | public: | ||
11 | PlayerPhysicsComponent(); | ||
12 | void tick(Game& game, Entity& entity, double dt); | ||
13 | void receive(Game& game, Entity& entity, const Message& msg); | ||
14 | |||
15 | private: | ||
16 | double jump_velocity; | ||
17 | double jump_gravity; | ||
18 | double jump_gravity_short; | ||
19 | int direction = 0; | ||
20 | bool canDrop = false; | ||
21 | bool frozen = false; | ||
22 | bool isFalling = false; | ||
23 | }; | ||
24 | |||
25 | #endif | ||
diff --git a/src/components/player_sprite.cpp b/src/components/player_sprite.cpp deleted file mode 100644 index 452a940..0000000 --- a/src/components/player_sprite.cpp +++ /dev/null | |||
@@ -1,60 +0,0 @@ | |||
1 | #include "player_sprite.h" | ||
2 | |||
3 | PlayerSpriteComponent::PlayerSpriteComponent() : sprite("res/Starla.png") | ||
4 | { | ||
5 | |||
6 | } | ||
7 | |||
8 | void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) | ||
9 | { | ||
10 | animFrame++; | ||
11 | |||
12 | int frame = 0; | ||
13 | if (isMoving) | ||
14 | { | ||
15 | frame += 2; | ||
16 | |||
17 | if (animFrame % 20 < 10) | ||
18 | { | ||
19 | frame += 2; | ||
20 | } | ||
21 | } | ||
22 | |||
23 | if (facingLeft) | ||
24 | { | ||
25 | frame++; | ||
26 | } | ||
27 | |||
28 | double alpha = 1.0; | ||
29 | if (dying && (animFrame % 4 < 2)) | ||
30 | { | ||
31 | alpha = 0.0; | ||
32 | } | ||
33 | |||
34 | Rectangle src_rect {frame*10, 0, 10, 12}; | ||
35 | Rectangle dst_rect {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second}; | ||
36 | buffer.blit(sprite, src_rect, dst_rect, alpha); | ||
37 | } | ||
38 | |||
39 | void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) | ||
40 | { | ||
41 | if (msg.type == Message::Type::walkLeft) | ||
42 | { | ||
43 | facingLeft = true; | ||
44 | isMoving = true; | ||
45 | } else if (msg.type == Message::Type::walkRight) | ||
46 | { | ||
47 | facingLeft = false; | ||
48 | isMoving = true; | ||
49 | } else if (msg.type == Message::Type::stopWalking) | ||
50 | { | ||
51 | isMoving = false; | ||
52 | } else if (msg.type == Message::Type::die) | ||
53 | { | ||
54 | dying = true; | ||
55 | isMoving = false; | ||
56 | } else if (msg.type == Message::Type::stopDying) | ||
57 | { | ||
58 | dying = false; | ||
59 | } | ||
60 | } | ||
diff --git a/src/components/player_sprite.h b/src/components/player_sprite.h deleted file mode 100644 index b1ac0af..0000000 --- a/src/components/player_sprite.h +++ /dev/null | |||
@@ -1,23 +0,0 @@ | |||
1 | #ifndef PLAYER_SPRITE_H | ||
2 | #define PLAYER_SPRITE_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include "renderer.h" | ||
6 | |||
7 | class Game; | ||
8 | |||
9 | class PlayerSpriteComponent : public Component { | ||
10 | public: | ||
11 | PlayerSpriteComponent(); | ||
12 | void render(Game& game, Entity& entity, Texture& buffer); | ||
13 | void receive(Game& game, Entity& entity, const Message& msg); | ||
14 | |||
15 | private: | ||
16 | Texture sprite; | ||
17 | int animFrame = 0; | ||
18 | bool facingLeft = false; | ||
19 | bool isMoving = false; | ||
20 | bool dying = false; | ||
21 | }; | ||
22 | |||
23 | #endif | ||
diff --git a/src/components/simple_collider.cpp b/src/components/simple_collider.cpp deleted file mode 100644 index f4b414e..0000000 --- a/src/components/simple_collider.cpp +++ /dev/null | |||
@@ -1,14 +0,0 @@ | |||
1 | #include "simple_collider.h" | ||
2 | |||
3 | SimpleColliderComponent::SimpleColliderComponent(std::function<void (Game& game, Entity& collider)> callback) : callback(callback) | ||
4 | { | ||
5 | |||
6 | } | ||
7 | |||
8 | void SimpleColliderComponent::receive(Game& game, Entity&, const Message& msg) | ||
9 | { | ||
10 | if (msg.type == Message::Type::collision) | ||
11 | { | ||
12 | callback(game, *msg.collisionEntity); | ||
13 | } | ||
14 | } | ||
diff --git a/src/components/simple_collider.h b/src/components/simple_collider.h deleted file mode 100644 index 15d78cf..0000000 --- a/src/components/simple_collider.h +++ /dev/null | |||
@@ -1,18 +0,0 @@ | |||
1 | #ifndef SIMPLE_COLLIDER_H | ||
2 | #define SIMPLE_COLLIDER_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include <functional> | ||
6 | |||
7 | class Game; | ||
8 | |||
9 | class SimpleColliderComponent : public Component { | ||
10 | public: | ||
11 | SimpleColliderComponent(std::function<void (Game& game, Entity& collider)> callback); | ||
12 | void receive(Game& game, Entity& entity, const Message& msg); | ||
13 | |||
14 | private: | ||
15 | std::function<void (Game& game, Entity& collider)> callback; | ||
16 | }; | ||
17 | |||
18 | #endif | ||
diff --git a/src/components/static_image.cpp b/src/components/static_image.cpp deleted file mode 100644 index 9fa8dca..0000000 --- a/src/components/static_image.cpp +++ /dev/null | |||
@@ -1,11 +0,0 @@ | |||
1 | #include "static_image.h" | ||
2 | |||
3 | StaticImageComponent::StaticImageComponent(const char* filename) : sprite(Texture(filename)) | ||
4 | { | ||
5 | |||
6 | } | ||
7 | |||
8 | void StaticImageComponent::render(Game&, Entity& entity, Texture& buffer) | ||
9 | { | ||
10 | buffer.blit(sprite, sprite.entirety(), {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second}); | ||
11 | } | ||
diff --git a/src/components/static_image.h b/src/components/static_image.h deleted file mode 100644 index 2dec78b..0000000 --- a/src/components/static_image.h +++ /dev/null | |||
@@ -1,18 +0,0 @@ | |||
1 | #ifndef STATIC_IMAGE_H | ||
2 | #define STATIC_IMAGE_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include "renderer.h" | ||
6 | |||
7 | class Game; | ||
8 | |||
9 | class StaticImageComponent : public Component { | ||
10 | public: | ||
11 | StaticImageComponent(const char* filename); | ||
12 | void render(Game& game, Entity& entity, Texture& buffer); | ||
13 | |||
14 | private: | ||
15 | Texture sprite; | ||
16 | }; | ||
17 | |||
18 | #endif | ||
diff --git a/src/components/user_movement.cpp b/src/components/user_movement.cpp deleted file mode 100644 index e499fee..0000000 --- a/src/components/user_movement.cpp +++ /dev/null | |||
@@ -1,100 +0,0 @@ | |||
1 | #include "user_movement.h" | ||
2 | #include "renderer.h" | ||
3 | |||
4 | void UserMovementComponent::input(Game& game, Entity& entity, int key, int action) | ||
5 | { | ||
6 | if (action == GLFW_PRESS) | ||
7 | { | ||
8 | if (key == GLFW_KEY_LEFT) | ||
9 | { | ||
10 | holdingLeft = true; | ||
11 | |||
12 | if (!frozen) | ||
13 | { | ||
14 | entity.send(game, Message::Type::walkLeft); | ||
15 | } | ||
16 | } else if (key == GLFW_KEY_RIGHT) | ||
17 | { | ||
18 | holdingRight = true; | ||
19 | |||
20 | if (!frozen) | ||
21 | { | ||
22 | entity.send(game, Message::Type::walkRight); | ||
23 | } | ||
24 | } else if (key == GLFW_KEY_UP) | ||
25 | { | ||
26 | if (!frozen) | ||
27 | { | ||
28 | entity.send(game, Message::Type::jump); | ||
29 | } | ||
30 | } else if (key == GLFW_KEY_DOWN) | ||
31 | { | ||
32 | if (!frozen) | ||
33 | { | ||
34 | entity.send(game, Message::Type::canDrop); | ||
35 | } | ||
36 | } | ||
37 | } else if (action == GLFW_RELEASE) | ||
38 | { | ||
39 | if (key == GLFW_KEY_LEFT) | ||
40 | { | ||
41 | holdingLeft = false; | ||
42 | |||
43 | if (!frozen) | ||
44 | { | ||
45 | if (holdingRight) | ||
46 | { | ||
47 | entity.send(game, Message::Type::walkRight); | ||
48 | } else { | ||
49 | entity.send(game, Message::Type::stopWalking); | ||
50 | } | ||
51 | } | ||
52 | } else if (key == GLFW_KEY_RIGHT) | ||
53 | { | ||
54 | holdingRight = false; | ||
55 | |||
56 | if (!frozen) | ||
57 | { | ||
58 | if (holdingLeft) | ||
59 | { | ||
60 | entity.send(game, Message::Type::walkLeft); | ||
61 | } else { | ||
62 | entity.send(game, Message::Type::stopWalking); | ||
63 | } | ||
64 | } | ||
65 | } else if (key == GLFW_KEY_DOWN) | ||
66 | { | ||
67 | if (!frozen) | ||
68 | { | ||
69 | entity.send(game, Message::Type::cantDrop); | ||
70 | } | ||
71 | } else if (key == GLFW_KEY_UP) | ||
72 | { | ||
73 | if (!frozen) | ||
74 | { | ||
75 | entity.send(game, Message::Type::stopJump); | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
81 | void UserMovementComponent::receive(Game& game, Entity& entity, const Message& msg) | ||
82 | { | ||
83 | if (msg.type == Message::Type::die) | ||
84 | { | ||
85 | frozen = true; | ||
86 | |||
87 | entity.send(game, Message::Type::stopWalking); | ||
88 | } else if (msg.type == Message::Type::stopDying) | ||
89 | { | ||
90 | frozen = false; | ||
91 | |||
92 | if (holdingLeft) | ||
93 | { | ||
94 | entity.send(game, Message::Type::walkLeft); | ||
95 | } else if (holdingRight) | ||
96 | { | ||
97 | entity.send(game, Message::Type::walkRight); | ||
98 | } | ||
99 | } | ||
100 | } | ||
diff --git a/src/components/user_movement.h b/src/components/user_movement.h deleted file mode 100644 index 1bcf05e..0000000 --- a/src/components/user_movement.h +++ /dev/null | |||
@@ -1,19 +0,0 @@ | |||
1 | #ifndef USER_MOVEMENT_H | ||
2 | #define USER_MOVEMENT_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | |||
6 | class Game; | ||
7 | |||
8 | class UserMovementComponent : public Component { | ||
9 | public: | ||
10 | void input(Game& game, Entity& entity, int key, int action); | ||
11 | void receive(Game&, Entity&, const Message& msg); | ||
12 | |||
13 | private: | ||
14 | bool holdingLeft = false; | ||
15 | bool holdingRight = false; | ||
16 | bool frozen = false; | ||
17 | }; | ||
18 | |||
19 | #endif | ||