summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt14
-rw-r--r--src/component.h7
-rw-r--r--src/components/ai.cpp142
-rw-r--r--src/components/ai.h73
-rw-r--r--src/components/map_collision.cpp241
-rw-r--r--src/components/map_collision.h47
-rw-r--r--src/components/map_render.cpp40
-rw-r--r--src/components/map_render.h19
-rw-r--r--src/components/physics_body.cpp66
-rw-r--r--src/components/physics_body.h20
-rw-r--r--src/components/player_physics.cpp118
-rw-r--r--src/components/player_physics.h25
-rw-r--r--src/components/player_sprite.cpp60
-rw-r--r--src/components/player_sprite.h23
-rw-r--r--src/components/simple_collider.cpp14
-rw-r--r--src/components/simple_collider.h18
-rw-r--r--src/components/static_image.cpp11
-rw-r--r--src/components/static_image.h18
-rw-r--r--src/components/user_movement.cpp100
-rw-r--r--src/components/user_movement.h19
-rw-r--r--src/entity.cpp46
-rw-r--r--src/entity.h64
-rw-r--r--src/entity_manager.h279
-rw-r--r--src/entityfactory.cpp182
-rw-r--r--src/entityfactory.h15
-rw-r--r--src/game.cpp178
-rw-r--r--src/game.h45
-rw-r--r--src/main.cpp12
-rw-r--r--src/map.cpp151
-rw-r--r--src/map.h70
-rw-r--r--src/world.cpp150
-rw-r--r--src/world.h21
32 files changed, 294 insertions, 1994 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0af1fa2..5dfce6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -47,22 +47,8 @@ include_directories(
47set(CMAKE_BUILD_TYPE Debug) 47set(CMAKE_BUILD_TYPE Debug)
48add_executable(Aromatherapy 48add_executable(Aromatherapy
49 src/main.cpp 49 src/main.cpp
50 src/map.cpp
51 src/renderer.cpp 50 src/renderer.cpp
52 src/entity.cpp
53 src/game.cpp
54 src/muxer.cpp 51 src/muxer.cpp
55 src/entityfactory.cpp
56 src/world.cpp
57 src/components/ai.cpp
58 src/components/map_collision.cpp
59 src/components/map_render.cpp
60 src/components/physics_body.cpp
61 src/components/player_physics.cpp
62 src/components/player_sprite.cpp
63 src/components/simple_collider.cpp
64 src/components/static_image.cpp
65 src/components/user_movement.cpp
66) 52)
67target_link_libraries(Aromatherapy ${ALL_LIBS}) 53target_link_libraries(Aromatherapy ${ALL_LIBS})
68install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR}) 54install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR})
diff --git a/src/component.h b/src/component.h new file mode 100644 index 0000000..2cbdc9d --- /dev/null +++ b/src/component.h
@@ -0,0 +1,7 @@
1#ifndef COMPONENT_H_F0CE4573
2#define COMPONENT_H_F0CE4573
3
4class Component {
5};
6
7#endif /* end of include guard: COMPONENT_H_F0CE4573 */
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
5void AIActionContainer::addAction(std::shared_ptr<AIAction> action)
6{
7 actions.push_back(action);
8}
9
10void 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
20void 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
38bool AIActionContainer::isDone() const
39{
40 return currentAction == end(actions);
41}
42
43AI::AI(int chance)
44{
45 this->chance = chance;
46}
47
48int AI::getChance() const
49{
50 return chance;
51}
52
53AI& AIComponent::emplaceAI(int chance)
54{
55 maxChance += chance;
56 ais.emplace_back(chance);
57
58 return ais.back();
59}
60
61void 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
94MoveAIAction::MoveAIAction(Direction dir, int len, int speed)
95{
96 this->dir = dir;
97 this->len = len;
98 this->speed = speed;
99}
100
101void MoveAIAction::start(Game& game, Entity& entity)
102{
103 remaining = len;
104}
105
106void 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
139bool 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
11class 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
18class 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
30class AI : public AIActionContainer {
31 public:
32 AI(int chance);
33
34 int getChance() const;
35
36 private:
37 int chance;
38};
39
40class 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
51class 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
6MapCollisionComponent::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
35void 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
85void 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
152void 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
232MapCollisionComponent::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
8class Game;
9
10class 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
6MapRenderComponent::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
37void 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
7class Map;
8class Game;
9
10class 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
5void 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
25void 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
44void 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
7class Game;
8
9class 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
9PlayerPhysicsComponent::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
18void 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
80void 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
7class Game;
8
9class 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
3PlayerSpriteComponent::PlayerSpriteComponent() : sprite("res/Starla.png")
4{
5
6}
7
8void 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
39void 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
7class Game;
8
9class 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
3SimpleColliderComponent::SimpleColliderComponent(std::function<void (Game& game, Entity& collider)> callback) : callback(callback)
4{
5
6}
7
8void 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
7class Game;
8
9class 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
3StaticImageComponent::StaticImageComponent(const char* filename) : sprite(Texture(filename))
4{
5
6}
7
8void 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
7class Game;
8
9class 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
4void 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
81void 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
6class Game;
7
8class 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
diff --git a/src/entity.cpp b/src/entity.cpp deleted file mode 100644 index 2b6cd7f..0000000 --- a/src/entity.cpp +++ /dev/null
@@ -1,46 +0,0 @@
1#include "entity.h"
2
3void Entity::addComponent(std::shared_ptr<Component> c)
4{
5 components.push_back(c);
6}
7
8void Entity::send(Game& game, const Message& msg)
9{
10 for (auto component : components)
11 {
12 component->receive(game, *this, msg);
13 }
14}
15
16void Entity::tick(Game& game, double dt)
17{
18 for (auto component : components)
19 {
20 component->tick(game, *this, dt);
21 }
22}
23
24void Entity::input(Game& game, int key, int action)
25{
26 for (auto component : components)
27 {
28 component->input(game, *this, key, action);
29 }
30}
31
32void Entity::render(Game& game, Texture& buffer)
33{
34 for (auto component : components)
35 {
36 component->render(game, *this, buffer);
37 }
38}
39
40void Entity::detectCollision(Game& game, Entity& collider, std::pair<double, double> old_position)
41{
42 for (auto component : components)
43 {
44 component->detectCollision(game, *this, collider, old_position);
45 }
46}
diff --git a/src/entity.h b/src/entity.h deleted file mode 100644 index 7f09f2d..0000000 --- a/src/entity.h +++ /dev/null
@@ -1,64 +0,0 @@
1#ifndef ENTITY_H
2#define ENTITY_H
3
4#include <list>
5#include "renderer.h"
6
7class Game;
8class Map;
9class Entity;
10class Component;
11
12class Message {
13 public:
14 enum class Type {
15 walkLeft,
16 walkRight,
17 stopWalking,
18 setHorizontalVelocity,
19 setVerticalVelocity,
20 collision,
21 jump,
22 stopJump,
23 drop,
24 canDrop,
25 cantDrop,
26 die,
27 stopDying,
28 hitTheGround
29 };
30
31 Message(Type type) : type(type) {}
32
33 Type type;
34 Entity* collisionEntity;
35 int dropAxis;
36 double velocity;
37};
38
39class Entity {
40 public:
41 void addComponent(std::shared_ptr<Component> c);
42 void send(Game& game, const Message& msg);
43 void tick(Game& game, double dt);
44 void input(Game& game, int key, int action);
45 void render(Game& game, Texture& buffer);
46 void detectCollision(Game& game, Entity& collider, std::pair<double, double> old_position);
47
48 std::pair<double, double> position;
49 std::pair<int, int> size;
50
51 private:
52 std::list<std::shared_ptr<Component>> components;
53};
54
55class Component {
56 public:
57 virtual void receive(Game&, Entity&, const Message&) {}
58 virtual void render(Game&, Entity&, Texture&) {}
59 virtual void tick(Game&, Entity&, double) {}
60 virtual void input(Game&, Entity&, int, int) {}
61 virtual void detectCollision(Game&, Entity&, Entity&, std::pair<double, double>) {}
62};
63
64#endif
diff --git a/src/entity_manager.h b/src/entity_manager.h new file mode 100644 index 0000000..74e758c --- /dev/null +++ b/src/entity_manager.h
@@ -0,0 +1,279 @@
1#ifndef ENTITY_MANAGER_H_C5832F11
2#define ENTITY_MANAGER_H_C5832F11
3
4#include <map>
5#include <typeindex>
6#include <set>
7#include <cassert>
8#include "component.h"
9
10class EntityManager {
11 private:
12 struct EntityData {
13 int parent = -1;
14 std::map<std::type_index, std::unique_ptr<Component>> components;
15 };
16
17 std::map<int, EntityData> entities;
18 std::map<int, std::set<int>> cachedChildren;
19 std::map<std::set<std::type_index>, std::set<int>> cachedComponents;
20
21 int nextEntityID = 0;
22
23 bool ensureNoParentCycles(int entity, int parent)
24 {
25 EntityData& data = entities[parent];
26 if (data.parent == entity)
27 {
28 return false;
29 } else if (data.parent == -1)
30 {
31 return true;
32 }
33
34 return ensureNoParentCycles(entity, data.parent);
35 }
36
37 std::set<int> getEntitiesWithComponents(std::set<std::type_index>& componentTypes)
38 {
39 if (cachedComponents.count(componentTypes) == 1)
40 {
41 return cachedComponents[componentTypes];
42 }
43
44 std::set<int>& cache = cachedComponents[componentTypes];
45 for (auto& entity : entities)
46 {
47 EntityData& data = entity.second;
48 bool cacheEntity = true;
49
50 for (auto& componentType : componentTypes)
51 {
52 if (data.components.count(componentType) == 0)
53 {
54 cacheEntity = false;
55 break;
56 }
57 }
58
59 if (cacheEntity)
60 {
61 cache.insert(entity.first);
62 }
63 }
64
65 return cache;
66 }
67
68 template <class T, class... R> std::set<int> getEntitiesWithComponents(std::set<std::type_index>& componentTypes)
69 {
70 componentTypes.insert(typeid(T));
71
72 return getEntitiesWithComponents<R...>(componentTypes);
73 }
74
75 public:
76 EntityManager() = default;
77 EntityManager(const EntityManager& copy) = delete;
78
79 int emplaceEntity()
80 {
81 // Find a suitable entity ID
82 while ((entities.count(nextEntityID) == 1) && (nextEntityID >= 0))
83 {
84 nextEntityID++;
85 }
86
87 if (nextEntityID < 0)
88 {
89 nextEntityID = 0;
90
91 while ((entities.count(nextEntityID) == 1) && (nextEntityID >= 0))
92 {
93 nextEntityID++;
94 }
95
96 assert(nextEntityID >= 0);
97 }
98
99 // Initialize the data
100 int id = nextEntityID++;
101 entities[id];
102
103 return id;
104 }
105
106 void deleteEntity(int entity)
107 {
108 assert(entities.count(entity) == 1);
109
110 EntityData& data = entities[entity];
111
112 // Destroy the children
113 std::set<int> children = getChildren(entity);
114 for (int child : children)
115 {
116 EntityData& childData = entities[child];
117 childData.parent = -1;
118
119 deleteEntity(child);
120 }
121
122 // Uncache children
123 cachedChildren.erase(entity);
124
125 if ((data.parent != -1) && (cachedChildren.count(data.parent) == 1))
126 {
127 cachedChildren[data.parent].erase(entity);
128 }
129
130 // Uncache components
131 for (auto& cache : cachedComponents)
132 {
133 cache.second.erase(entity);
134 }
135
136 // Destroy the data
137 entities.erase(entity);
138 }
139
140 std::set<int> getChildren(int parent)
141 {
142 assert(entities.count(parent) == 1);
143
144 if (cachedChildren.count(parent) == 1)
145 {
146 return cachedChildren[parent];
147 }
148
149 std::set<int>& cache = cachedChildren[parent];
150 for (auto& entity : entities)
151 {
152 EntityData& data = entity.second;
153 if (data.parent == parent)
154 {
155 cache.insert(entity.first);
156 }
157 }
158
159 return cache;
160 }
161
162 void setParent(int entity, int parent)
163 {
164 assert(entities.count(entity) == 1);
165 assert(entities.count(parent) == 1);
166 assert(ensureNoParentCycles(entity, parent));
167
168 EntityData& data = entities[entity];
169
170 // Remove from old parent
171 if (data.parent != -1)
172 {
173 if (cachedChildren.count(data.parent) == 1)
174 {
175 cachedChildren[data.parent].erase(entity);
176 }
177 }
178
179 data.parent = parent;
180
181 // Cache new parent
182 if (cachedChildren.count(parent) == 1)
183 {
184 cachedChildren[parent].insert(entity);
185 }
186 }
187
188 void setNoParent(int entity)
189 {
190 assert(entities.count(entity) == 1);
191
192 EntityData& data = entities[entity];
193
194 // Remove from old parent
195 if (data.parent != -1)
196 {
197 if (cachedChildren.count(data.parent) == 1)
198 {
199 cachedChildren[data.parent].erase(entity);
200 }
201 }
202
203 data.parent = -1;
204 }
205
206 int getParent(int entity)
207 {
208 assert(entities.count(entity) == 1);
209
210 EntityData& data = entities[entity];
211 return data.parent;
212 }
213
214 template <class T, class... Args> T& emplaceComponent(int entity, Args&&... args)
215 {
216 assert(entities.count(entity) == 1);
217
218 EntityData& data = entities[entity];
219 std::type_index componentType = typeid(T);
220
221 assert(data.components.count(componentType) == 0);
222
223 // Initialize the component
224 std::unique_ptr<T> ptr = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
225 T& component = *ptr;
226 data.components[componentType] = std::move(ptr);
227
228 // Invalidate related caches
229 std::remove_if(begin(cachedComponents), end(cachedComponents), [&] (std::pair<std::set<std::type_index>, int>& cache) {
230 return cache.first.count(componentType) == 1;
231 });
232
233 return component;
234 }
235
236 template <class T> void removeComponent(int entity)
237 {
238 assert(entities.count(entity) == 1);
239
240 EntityData& data = entities[entity];
241 std::type_index componentType = typeid(T);
242
243 assert(data.components.count(componentType) == 1);
244
245 // Destroy the component
246 data.components.erase(componentType);
247
248 // Uncache the component
249 for (auto& cache : cachedComponents)
250 {
251 if (cache.first.count(componentType) == 1)
252 {
253 cache.second.erase(entity);
254 }
255 }
256 }
257
258 template <class T> T& getComponent(int entity)
259 {
260 assert(entities.count(entity) == 1);
261
262 EntityData& data = entities[entity];
263 std::type_index componentType = typeid(T);
264
265 assert(data.components.count(componentType) == 1);
266
267 return *(data.components[componentType]);
268 }
269
270 template <class T, class... R> std::set<int> getEntitiesWithComponents()
271 {
272 std::set<std::type_index> componentTypes;
273 componentTypes.insert(typeid(T));
274
275 return getEntitiesWithComponents<R...>(componentTypes);
276 }
277};
278
279#endif /* end of include guard: ENTITY_MANAGER_H_C5832F11 */
diff --git a/src/entityfactory.cpp b/src/entityfactory.cpp deleted file mode 100644 index b80fe99..0000000 --- a/src/entityfactory.cpp +++ /dev/null
@@ -1,182 +0,0 @@
1#include "entityfactory.h"
2#include <libxml/parser.h>
3#include "muxer.h"
4#include <cstdio>
5#include <map>
6#include <list>
7#include "components/static_image.h"
8#include "components/simple_collider.h"
9#include "components/physics_body.h"
10#include "components/ai.h"
11#include "game.h"
12
13void parseEntityAIData(AI& ai, xmlNodePtr node, const std::map<std::string, int>& items)
14{
15 xmlChar* key;
16
17 for (xmlNodePtr aiNode = node->xmlChildrenNode; aiNode != NULL; aiNode = aiNode->next)
18 {
19 if (!xmlStrcmp(aiNode->name, (xmlChar*) "move"))
20 {
21 MoveAIAction::Direction dir;
22 int len;
23 int speed;
24
25 key = xmlGetProp(aiNode, (xmlChar*) "direction");
26 if (key == 0) exit(2);
27 if (!xmlStrcmp(key, (xmlChar*) "left"))
28 {
29 dir = MoveAIAction::Direction::Left;
30 } else if (!xmlStrcmp(key, (xmlChar*) "right"))
31 {
32 dir = MoveAIAction::Direction::Right;
33 } else if (!xmlStrcmp(key, (xmlChar*) "up"))
34 {
35 dir = MoveAIAction::Direction::Up;
36 } else if (!xmlStrcmp(key, (xmlChar*) "down"))
37 {
38 dir = MoveAIAction::Direction::Down;
39 } else {
40 exit(2);
41 }
42 xmlFree(key);
43
44 key = xmlGetProp(aiNode, (xmlChar*) "length");
45 if (key != 0)
46 {
47 len = atoi((char*) key);
48 } else {
49 key = xmlGetProp(aiNode, (xmlChar*) "length-var");
50 if (key == 0) exit(2);
51 std::string varName = (char*) key;
52 len = items.at(varName);
53 }
54 xmlFree(key);
55
56 key = xmlGetProp(aiNode, (xmlChar*) "speed");
57 if (key != 0)
58 {
59 speed = atoi((char*) key);
60 } else {
61 key = xmlGetProp(aiNode, (xmlChar*) "speed-var");
62 if (key == 0) exit(2);
63 std::string varName = (char*) key;
64 speed = items.at(varName);
65 }
66 xmlFree(key);
67
68 ai.addAction(std::make_shared<MoveAIAction>(dir, len, speed));
69 } else if (!xmlStrcmp(aiNode->name, (xmlChar*) "switch"))
70 {
71 key = xmlGetProp(aiNode, (xmlChar*) "item");
72 if (key == 0) exit(2);
73 std::string switchItem = (char*) key;
74 xmlFree(key);
75
76 for (xmlNodePtr switchNode = aiNode->xmlChildrenNode; switchNode != NULL; switchNode = switchNode->next)
77 {
78 if (!xmlStrcmp(switchNode->name, (xmlChar*) "case"))
79 {
80 key = xmlGetProp(switchNode, (xmlChar*) "value");
81 if (key == 0) exit(2);
82 int caseValue = atoi((char*) key);
83 xmlFree(key);
84
85 if (items.at(switchItem) == caseValue)
86 {
87 parseEntityAIData(ai, switchNode, items);
88 }
89 }
90 }
91 }
92 }
93}
94
95std::shared_ptr<Entity> EntityFactory::createNamedEntity(const std::string name, const std::map<std::string, int>& items)
96{
97 xmlDocPtr doc = xmlParseFile("res/entities.xml");
98 if (doc == nullptr)
99 {
100 fprintf(stderr, "Error reading entities\n");
101 exit(-1);
102 }
103
104 xmlNodePtr top = xmlDocGetRootElement(doc);
105 if (top == nullptr)
106 {
107 fprintf(stderr, "Empty entities file\n");
108 exit(-1);
109 }
110
111 if (xmlStrcmp(top->name, (const xmlChar*) "entities"))
112 {
113 fprintf(stderr, "Invalid entities definition\n");
114 exit(-1);
115 }
116
117 auto entity = std::make_shared<Entity>();
118
119 xmlChar* key;
120 for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
121 {
122 if (!xmlStrcmp(node->name, (xmlChar*) "entity"))
123 {
124 key = xmlGetProp(node, (xmlChar*) "id");
125 if (key == 0) exit(-1);
126 std::string entityID = (char*) key;
127 xmlFree(key);
128
129 if (entityID == name)
130 {
131 key = xmlGetProp(node, (xmlChar*) "sprite");
132 if (key == 0) exit(-1);
133 auto spriteComponent = std::make_shared<StaticImageComponent>((char*) key);
134 entity->addComponent(spriteComponent);
135 xmlFree(key);
136
137 auto physicsComponent = std::make_shared<PhysicsBodyComponent>();
138 entity->addComponent(physicsComponent);
139
140 key = xmlGetProp(node, (xmlChar*) "width");
141 if (key == 0) exit(-1);
142 entity->size.first = atoi((char*) key);
143 xmlFree(key);
144
145 key = xmlGetProp(node, (xmlChar*) "height");
146 if (key == 0) exit(-1);
147 entity->size.second = atoi((char*) key);
148 xmlFree(key);
149
150 bool addAI = false;
151 auto aiComponent = std::make_shared<AIComponent>();
152
153 for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next)
154 {
155 if (!xmlStrcmp(entityNode->name, (xmlChar*) "ai"))
156 {
157 addAI = true;
158
159 xmlChar* chanceKey = xmlGetProp(entityNode, (xmlChar*) "chance");
160 if (chanceKey == 0) exit(2);
161 int chance = atoi((char*) chanceKey);
162 xmlFree(chanceKey);
163
164 AI& ai = aiComponent->emplaceAI(chance);
165 parseEntityAIData(ai, entityNode, items);
166 }
167 }
168
169 if (addAI)
170 {
171 entity->addComponent(aiComponent);
172 }
173
174 break;
175 }
176 }
177 }
178
179 xmlFreeDoc(doc);
180
181 return entity;
182}
diff --git a/src/entityfactory.h b/src/entityfactory.h deleted file mode 100644 index 56f7216..0000000 --- a/src/entityfactory.h +++ /dev/null
@@ -1,15 +0,0 @@
1#ifndef ENTITYFACTORY_H
2#define ENTITYFACTORY_H
3
4#include <string>
5#include <map>
6
7class Entity;
8class Map;
9
10class EntityFactory {
11 public:
12 static std::shared_ptr<Entity> createNamedEntity(const std::string name, const std::map<std::string, int>& items);
13};
14
15#endif
diff --git a/src/game.cpp b/src/game.cpp deleted file mode 100644 index 673c804..0000000 --- a/src/game.cpp +++ /dev/null
@@ -1,178 +0,0 @@
1#include "game.h"
2#include <cstdlib>
3#include "renderer.h"
4#include "muxer.h"
5#include "map.h"
6#include "components/user_movement.h"
7#include "components/player_physics.h"
8#include "components/player_sprite.h"
9#include "components/map_render.h"
10#include "components/map_collision.h"
11#include "consts.h"
12
13Game::Game(const char* mapfile) : world(mapfile)
14{
15 // Set up entities
16 player = std::make_shared<Entity>();
17 player->position = world.getStartingPosition();
18 player->size = std::make_pair(10.0,12.0);
19
20 auto player_input = std::make_shared<UserMovementComponent>();
21 player->addComponent(player_input);
22
23 auto player_physics = std::make_shared<PlayerPhysicsComponent>();
24 player->addComponent(player_physics);
25
26 auto player_anim = std::make_shared<PlayerSpriteComponent>();
27 player->addComponent(player_anim);
28
29 const Map& startingMap = world.getStartingMap();
30 save = {&startingMap, player->position};
31
32 loadMap(startingMap, player->position);
33}
34
35void key_callback(GLFWwindow* window, int key, int, int action, int)
36{
37 Game* game = (Game*) glfwGetWindowUserPointer(window);
38
39 if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
40 {
41 game->shouldQuit = true;
42 }
43
44 for (auto entity : game->entities)
45 {
46 entity->input(*game, key, action);
47 }
48}
49
50void Game::execute(GLFWwindow* window)
51{
52 glfwSwapInterval(1);
53 glfwSetWindowUserPointer(window, this);
54 glfwSetKeyCallback(window, key_callback);
55
56 Texture buffer(GAME_WIDTH, GAME_HEIGHT);
57
58 double lastTime = glfwGetTime();
59 const double dt = 0.01;
60 double accumulator = 0.0;
61
62 while (!(shouldQuit || glfwWindowShouldClose(window)))
63 {
64 double currentTime = glfwGetTime();
65 double frameTime = currentTime - lastTime;
66 lastTime = currentTime;
67
68 // Should we load a new world?
69 if (newWorld)
70 {
71 newWorld = false;
72 entities.clear();
73 entities = std::move(nextEntities);
74
75 player->position = nextPosition;
76 }
77
78 // Handle input
79 glfwPollEvents();
80
81 // Tick!
82 accumulator += frameTime;
83 while (accumulator >= dt)
84 {
85 for (auto entity : entities)
86 {
87 entity->tick(*this, dt);
88 }
89
90 accumulator -= dt;
91 }
92
93 // Do any scheduled tasks
94 for (auto& task : scheduled)
95 {
96 task.first -= frameTime;
97
98 if (task.first <= 0)
99 {
100 task.second();
101 }
102 }
103
104 scheduled.remove_if([] (std::pair<double, std::function<void ()>> value) { return value.first <= 0; });
105
106 // Do rendering
107 buffer.fill(buffer.entirety(), 0, 0, 0);
108 for (auto entity : entities)
109 {
110 entity->render(*this, buffer);
111 }
112
113 buffer.renderScreen();
114 }
115}
116
117void Game::loadMap(const Map& map, std::pair<double, double> position)
118{
119 auto mapEn = std::make_shared<Entity>();
120
121 auto map_render = std::make_shared<MapRenderComponent>(map);
122 mapEn->addComponent(map_render);
123
124 auto map_collision = std::make_shared<MapCollisionComponent>(map);
125 mapEn->addComponent(map_collision);
126
127 // Map in the back, player on top, rest of entities in between
128 nextEntities.clear();
129 nextEntities.push_back(mapEn);
130 map.createEntities(nextEntities);
131 nextEntities.push_back(player);
132
133 newWorld = true;
134
135 currentMap = &map;
136 nextPosition = position;
137}
138
139void Game::detectCollision(Entity& collider, std::pair<double, double> old_position)
140{
141 for (auto entity : entities)
142 {
143 entity->detectCollision(*this, collider, old_position);
144 }
145}
146
147void Game::saveGame()
148{
149 save = {currentMap, player->position};
150}
151
152void Game::schedule(double time, std::function<void ()> callback)
153{
154 scheduled.emplace_front(time, std::move(callback));
155}
156
157void Game::playerDie()
158{
159 player->send(*this, Message::Type::die);
160
161 playSound("res/Hit_Hurt5.wav", 0.25);
162
163 schedule(0.75, [&] () {
164 if (*currentMap != *save.map)
165 {
166 loadMap(*save.map, save.position);
167 } else {
168 player->position = save.position;
169 }
170
171 player->send(*this, Message::Type::stopDying);
172 });
173}
174
175const World& Game::getWorld() const
176{
177 return world;
178}
diff --git a/src/game.h b/src/game.h deleted file mode 100644 index dd4b2f7..0000000 --- a/src/game.h +++ /dev/null
@@ -1,45 +0,0 @@
1#ifndef GAME_H
2#define GAME_H
3
4#include <memory>
5#include <functional>
6#include <list>
7#include <map>
8#include "map.h"
9#include "world.h"
10
11class Entity;
12struct GLFWwindow;
13
14struct Savefile {
15 const Map* map;
16 std::pair<double, double> position;
17};
18
19class Game {
20 public:
21 Game(const char* maps);
22 void execute(GLFWwindow* window);
23 void loadMap(const Map& map, std::pair<double, double> position);
24 void detectCollision(Entity& collider, std::pair<double, double> old_position);
25 void saveGame();
26 void schedule(double time, std::function<void ()> callback);
27 void playerDie();
28 const World& getWorld() const;
29
30 private:
31 friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
32
33 std::list<std::shared_ptr<Entity>> entities;
34 std::list<std::shared_ptr<Entity>> nextEntities;
35 std::pair<double, double> nextPosition;
36 bool newWorld;
37 std::shared_ptr<Entity> player;
38 const Map* currentMap;
39 Savefile save;
40 std::list<std::pair<double, std::function<void ()>>> scheduled;
41 bool shouldQuit = false;
42 World world;
43};
44
45#endif
diff --git a/src/main.cpp b/src/main.cpp index 4157350..29a364b 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,9 +1,9 @@
1#include <ctime> 1#include <ctime>
2#include <list> 2#include <list>
3#include "renderer.h"
4#include <cstdlib> 3#include <cstdlib>
5#include "game.h" 4#include "renderer.h"
6#include "muxer.h" 5#include "muxer.h"
6#include "entity_manager.h"
7 7
8int main() 8int main()
9{ 9{
@@ -14,8 +14,12 @@ int main()
14 14
15 // Put this in a block so game goes out of scope before we destroy the renderer 15 // Put this in a block so game goes out of scope before we destroy the renderer
16 { 16 {
17 Game game {"res/maps.xml"}; 17 EntityManager manager;
18 game.execute(window); 18
19 int eRef = manager.emplaceEntity();
20 int eRef2 = manager.emplaceEntity();
21 manager.setParent(eRef, eRef2);
22 printf("%d\n", manager.getParent(eRef));
19 } 23 }
20 24
21 destroyMuxer(); 25 destroyMuxer();
diff --git a/src/map.cpp b/src/map.cpp deleted file mode 100644 index e3725b2..0000000 --- a/src/map.cpp +++ /dev/null
@@ -1,151 +0,0 @@
1#include "map.h"
2#include <cstdlib>
3#include <cstring>
4#include <map>
5#include "entityfactory.h"
6#include "entity.h"
7#include "game.h"
8#include "consts.h"
9
10Map::Map(int id)
11{
12 this->id = id;
13 mapdata = (int*) calloc(1, sizeof(int));
14}
15
16Map::Map(const Map& map)
17{
18 mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
19 memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int));
20
21 id = map.id;
22 title = map.title;
23 adjacents = map.adjacents;
24 entities = map.entities;
25}
26
27Map::Map(Map&& map) : Map()
28{
29 swap(*this, map);
30}
31
32Map::~Map()
33{
34 free(mapdata);
35}
36
37Map& Map::operator= (Map map)
38{
39 swap(*this, map);
40
41 return *this;
42}
43
44void swap(Map& first, Map& second)
45{
46 std::swap(first.mapdata, second.mapdata);
47 std::swap(first.title, second.title);
48 std::swap(first.adjacents, second.adjacents);
49 std::swap(first.id, second.id);
50 std::swap(first.entities, second.entities);
51}
52
53int Map::getID() const
54{
55 return id;
56}
57
58const int* Map::getMapdata() const
59{
60 return mapdata;
61}
62
63std::string Map::getTitle() const
64{
65 return title;
66}
67
68void Map::createEntities(std::list<std::shared_ptr<Entity>>& entities) const
69{
70 for (auto data : this->entities)
71 {
72 auto entity = EntityFactory::createNamedEntity(data.name, data.items);
73 entity->position = data.position;
74
75 entities.push_back(entity);
76 }
77}
78
79bool Map::operator==(const Map& other) const
80{
81 return id == other.id;
82}
83
84bool Map::operator!=(const Map& other) const
85{
86 return id != other.id;
87}
88
89Map::MoveType Map::moveTypeForShort(std::string str)
90{
91 if (str == "wrap") return MoveType::Wrap;
92 if (str == "warp") return MoveType::Warp;
93 if (str == "reverseWarp") return MoveType::ReverseWarp;
94
95 return MoveType::Wall;
96}
97
98Map::MoveDir Map::moveDirForShort(std::string str)
99{
100 if (str == "right") return MoveDir::Right;
101 if (str == "up") return MoveDir::Up;
102 if (str == "down") return MoveDir::Down;
103
104 return MoveDir::Left;
105}
106
107static const Map::Adjacent defaultAdjacent {};
108const Map::Adjacent& Map::getAdjacent(MoveDir dir) const
109{
110 if (adjacents.count(dir) > 0)
111 {
112 return adjacents.at(dir);
113 } else {
114 return defaultAdjacent;
115 }
116}
117
118bool Map::moveTypeTakesMap(MoveType type)
119{
120 switch (type)
121 {
122 case MoveType::Wall: return false;
123 case MoveType::Wrap: return false;
124 case MoveType::Warp: return true;
125 case MoveType::ReverseWarp: return true;
126 }
127}
128
129void Map::setMapdata(int* mapdata)
130{
131 free(this->mapdata);
132 this->mapdata = mapdata;
133}
134
135void Map::setTitle(std::string title)
136{
137 this->title = title;
138}
139
140void Map::setAdjacent(MoveDir dir, MoveType type, int map)
141{
142 Adjacent& cur = adjacents[dir];
143 cur.type = type;
144 if (map != -1) cur.map = map;
145}
146
147void Map::addEntity(EntityData& data)
148{
149 entities.push_back(data);
150}
151
diff --git a/src/map.h b/src/map.h deleted file mode 100644 index 22333aa..0000000 --- a/src/map.h +++ /dev/null
@@ -1,70 +0,0 @@
1#ifndef MAP_H
2#define MAP_H
3
4#include <string>
5#include <list>
6#include <map>
7
8class Entity;
9
10class Map {
11 public:
12 Map(int id);
13 Map() : Map(-1) {}
14 Map(const Map& map);
15 Map(Map&& map);
16 ~Map();
17 Map& operator= (Map other);
18 friend void swap(Map& first, Map& second);
19
20 enum class MoveType {
21 Wall,
22 Wrap,
23 Warp,
24 ReverseWarp
25 };
26
27 enum class MoveDir {
28 Left,
29 Right,
30 Up,
31 Down
32 };
33
34 struct EntityData {
35 std::string name;
36 std::pair<int, int> position;
37 std::map<std::string, int> items;
38 };
39
40 struct Adjacent {
41 MoveType type = MoveType::Wall;
42 int map = -1;
43 };
44
45 static MoveType moveTypeForShort(std::string str);
46 static MoveDir moveDirForShort(std::string str);
47 static bool moveTypeTakesMap(MoveType type);
48
49 int getID() const;
50 const int* getMapdata() const;
51 std::string getTitle() const;
52 const Adjacent& getAdjacent(MoveDir dir) const;
53
54 void createEntities(std::list<std::shared_ptr<Entity>>& entities) const;
55 bool operator==(const Map& other) const;
56 bool operator!=(const Map& other) const;
57
58 void setMapdata(int* mapdata);
59 void setTitle(std::string title);
60 void setAdjacent(MoveDir dir, MoveType type, int map);
61 void addEntity(EntityData& data);
62 private:
63 int* mapdata;
64 std::string title;
65 int id;
66 std::list<EntityData> entities;
67 std::map<MoveDir, Adjacent> adjacents;
68};
69
70#endif
diff --git a/src/world.cpp b/src/world.cpp deleted file mode 100644 index 0c61c47..0000000 --- a/src/world.cpp +++ /dev/null
@@ -1,150 +0,0 @@
1#include "world.h"
2#include <libxml/parser.h>
3#include "consts.h"
4
5World::World(const char* filename)
6{
7 xmlDocPtr doc = xmlParseFile(filename);
8 if (doc == nullptr)
9 {
10 exit(2);
11 }
12
13 xmlNodePtr top = xmlDocGetRootElement(doc);
14 if (top == nullptr)
15 {
16 exit(2);
17 }
18
19 if (xmlStrcmp(top->name, (const xmlChar*) "world"))
20 {
21 exit(2);
22 }
23
24 xmlChar* startxKey = xmlGetProp(top, (xmlChar*) "startx");
25 if (startxKey == 0) exit(2);
26 startX = atoi((char*) startxKey);
27 xmlFree(startxKey);
28
29 xmlChar* startyKey = xmlGetProp(top, (xmlChar*) "starty");
30 if (startyKey == 0) exit(2);
31 startY = atoi((char*) startyKey);
32 xmlFree(startyKey);
33
34 xmlChar* startmapKey = xmlGetProp(top, (xmlChar*) "startmap");
35 if (startxKey == 0) exit(2);
36 startMap = atoi((char*) startmapKey);
37 xmlFree(startmapKey);
38
39 for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
40 {
41 if (!xmlStrcmp(node->name, (const xmlChar*) "map"))
42 {
43 xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id");
44 if (idKey == 0) exit(2);
45 int theId = atoi((char*) idKey);
46 xmlFree(idKey);
47
48 maps.emplace(theId, theId);
49 Map& map = maps[theId];
50
51 xmlChar* titleKey = xmlGetProp(node, (xmlChar*) "title");
52 if (titleKey == 0) exit(2);
53 map.setTitle((char*) titleKey);
54 xmlFree(titleKey);
55
56 for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next)
57 {
58 if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment"))
59 {
60 xmlChar* key = xmlNodeGetContent(mapNode);
61 int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
62 mapdata[0] = atoi(strtok((char*) key, ",\n"));
63 for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++)
64 {
65 mapdata[i] = atoi(strtok(NULL, ",\n"));
66 }
67 map.setMapdata(mapdata);
68 xmlFree(key);
69 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entity"))
70 {
71 Map::EntityData data;
72
73 xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
74 if (typeKey == 0) exit(2);
75 data.name = (char*) typeKey;
76 xmlFree(typeKey);
77
78 xmlChar* xKey = xmlGetProp(mapNode, (const xmlChar*) "x");
79 if (xKey == 0) exit(2);
80 data.position.first = atoi((char*) xKey);
81 xmlFree(xKey);
82
83 xmlChar* yKey = xmlGetProp(mapNode, (const xmlChar*) "y");
84 if (yKey == 0) exit(2);
85 data.position.second = atoi((char*) yKey);
86 xmlFree(yKey);
87
88 for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next)
89 {
90 if (!xmlStrcmp(entityNode->name, (xmlChar*) "item"))
91 {
92 xmlChar* itemIdKey = xmlGetProp(entityNode, (const xmlChar*) "id");
93 if (itemIdKey == 0) exit(2);
94 std::string itemId = (char*) itemIdKey;
95 xmlFree(itemIdKey);
96
97 xmlChar* itemIdVal = xmlNodeGetContent(entityNode);
98 if (itemIdVal == 0) exit(2);
99 data.items[itemId] = atoi((char*) itemIdVal);
100 xmlFree(itemIdVal);
101 }
102 }
103
104 map.addEntity(data);
105 } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "adjacent"))
106 {
107 Map::MoveDir direction;
108 Map::MoveType moveType;
109 int mapId = 0;
110
111 xmlChar* dirKey = xmlGetProp(mapNode, (const xmlChar*) "dir");
112 if (dirKey == 0) exit(2);
113 direction = Map::moveDirForShort((char*) dirKey);
114 xmlFree(dirKey);
115
116 xmlChar* typeKey = xmlGetProp(mapNode, (const xmlChar*) "type");
117 if (typeKey == 0) exit(2);
118 moveType = Map::moveTypeForShort((char*) typeKey);
119 xmlFree(typeKey);
120
121 xmlChar* mapIdKey = xmlGetProp(mapNode, (const xmlChar*) "map");
122 if (mapIdKey != 0)
123 {
124 mapId = atoi((char*) mapIdKey);
125 }
126 xmlFree(mapIdKey);
127
128 map.setAdjacent(direction, moveType, mapId);
129 }
130 }
131 }
132 }
133
134 xmlFreeDoc(doc);
135}
136
137const Map& World::getMap(int id) const
138{
139 return maps.at(id);
140}
141
142const Map& World::getStartingMap() const
143{
144 return maps.at(startMap);
145}
146
147std::pair<int, int> World::getStartingPosition() const
148{
149 return std::make_pair(startX, startY);
150}
diff --git a/src/world.h b/src/world.h deleted file mode 100644 index f566487..0000000 --- a/src/world.h +++ /dev/null
@@ -1,21 +0,0 @@
1#ifndef WORLD_H
2#define WORLD_H
3
4#include <map>
5#include "map.h"
6
7class World {
8 public:
9 World(const char* filename);
10 const Map& getMap(int id) const;
11 const Map& getStartingMap() const;
12 std::pair<int, int> getStartingPosition() const;
13
14 private:
15 std::map<int, Map> maps;
16 int startMap;
17 int startX;
18 int startY;
19};
20
21#endif /* end of include guard: WORLD_H */