diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/map_collision.cpp | 211 | ||||
-rw-r--r-- | src/components/map_collision.h | 46 | ||||
-rw-r--r-- | src/components/map_render.cpp | 39 | ||||
-rw-r--r-- | src/components/map_render.h | 19 | ||||
-rw-r--r-- | src/components/physics_body.cpp | 65 | ||||
-rw-r--r-- | src/components/physics_body.h | 20 | ||||
-rw-r--r-- | src/components/player_physics.cpp | 117 | ||||
-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 |
16 files changed, 805 insertions, 0 deletions
diff --git a/src/components/map_collision.cpp b/src/components/map_collision.cpp new file mode 100644 index 0000000..f385320 --- /dev/null +++ b/src/components/map_collision.cpp | |||
@@ -0,0 +1,211 @@ | |||
1 | #include "map_collision.h" | ||
2 | #include "map.h" | ||
3 | #include "game.h" | ||
4 | |||
5 | MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) | ||
6 | { | ||
7 | addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport); | ||
8 | addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport); | ||
9 | |||
10 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | ||
11 | { | ||
12 | int x = i % MAP_WIDTH; | ||
13 | int y = i / MAP_WIDTH; | ||
14 | int tile = map.getMapdata()[i]; | ||
15 | |||
16 | if ((tile > 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7)))) | ||
17 | { | ||
18 | addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, Collision::Type::wall); | ||
19 | addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, Collision::Type::wall); | ||
20 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::wall); | ||
21 | addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, Collision::Type::wall); | ||
22 | } else if ((tile >= 5) && (tile <= 7)) | ||
23 | { | ||
24 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::platform); | ||
25 | } else if (tile == 42) | ||
26 | { | ||
27 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::danger); | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | void MapCollisionComponent::addCollision(double axis, double lower, double | ||
33 | upper, Direction dir, Collision::Type type) | ||
34 | { | ||
35 | std::list<Collision>::iterator it; | ||
36 | |||
37 | switch (dir) | ||
38 | { | ||
39 | case Direction::up: | ||
40 | it = up_collisions.begin(); | ||
41 | for (; it!=up_collisions.end(); it++) | ||
42 | { | ||
43 | if (it->axis < axis) break; | ||
44 | } | ||
45 | |||
46 | up_collisions.insert(it, {axis, lower, upper, type}); | ||
47 | |||
48 | break; | ||
49 | case Direction::down: | ||
50 | it = down_collisions.begin(); | ||
51 | for (; it!=down_collisions.end(); it++) | ||
52 | { | ||
53 | if (it->axis > axis) break; | ||
54 | } | ||
55 | |||
56 | down_collisions.insert(it, {axis, lower, upper, type}); | ||
57 | |||
58 | break; | ||
59 | case Direction::left: | ||
60 | it = left_collisions.begin(); | ||
61 | for (; it!=left_collisions.end(); it++) | ||
62 | { | ||
63 | if (it->axis < axis) break; | ||
64 | } | ||
65 | |||
66 | left_collisions.insert(it, {axis, lower, upper, type}); | ||
67 | |||
68 | break; | ||
69 | case Direction::right: | ||
70 | it = right_collisions.begin(); | ||
71 | for (; it!=right_collisions.end(); it++) | ||
72 | { | ||
73 | if (it->axis > axis) break; | ||
74 | } | ||
75 | |||
76 | right_collisions.insert(it, {axis, lower, upper, type}); | ||
77 | |||
78 | break; | ||
79 | } | ||
80 | } | ||
81 | |||
82 | void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collider, std::pair<double, double> old_position) | ||
83 | { | ||
84 | if (collider.position.first < old_position.first) | ||
85 | { | ||
86 | for (auto collision : left_collisions) | ||
87 | { | ||
88 | if (collision.axis > old_position.first) continue; | ||
89 | if (collision.axis < collider.position.first) break; | ||
90 | |||
91 | if ((old_position.second+collider.size.second > collision.lower) && (old_position.second < collision.upper)) | ||
92 | { | ||
93 | // We have a collision! | ||
94 | processCollision(game, collider, collision, Direction::left, old_position); | ||
95 | |||
96 | break; | ||
97 | } | ||
98 | } | ||
99 | } else if (collider.position.first > old_position.first) | ||
100 | { | ||
101 | for (auto collision : right_collisions) | ||
102 | { | ||
103 | if (collision.axis < old_position.first+collider.size.first) continue; | ||
104 | if (collision.axis > collider.position.first+collider.size.first) break; | ||
105 | |||
106 | if ((old_position.second+collider.size.second > collision.lower) && (old_position.second < collision.upper)) | ||
107 | { | ||
108 | // We have a collision! | ||
109 | processCollision(game, collider, collision, Direction::right, old_position); | ||
110 | |||
111 | break; | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | if (collider.position.second < old_position.second) | ||
117 | { | ||
118 | for (auto collision : up_collisions) | ||
119 | { | ||
120 | if (collision.axis > old_position.second) continue; | ||
121 | if (collision.axis < collider.position.second) break; | ||
122 | |||
123 | if ((collider.position.first+collider.size.first > collision.lower) && (collider.position.first < collision.upper)) | ||
124 | { | ||
125 | // We have a collision! | ||
126 | processCollision(game, collider, collision, Direction::up, old_position); | ||
127 | |||
128 | break; | ||
129 | } | ||
130 | } | ||
131 | } else if (collider.position.second > old_position.second) | ||
132 | { | ||
133 | for (auto collision : down_collisions) | ||
134 | { | ||
135 | if (collision.axis < old_position.second+collider.size.second) continue; | ||
136 | if (collision.axis > collider.position.second+collider.size.second) break; | ||
137 | |||
138 | if ((collider.position.first+collider.size.first > collision.lower) && (collider.position.first < collision.upper)) | ||
139 | { | ||
140 | // We have a collision! | ||
141 | processCollision(game, collider, collision, Direction::down, old_position); | ||
142 | |||
143 | break; | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | void MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir, std::pair<double, double> old_position) | ||
150 | { | ||
151 | if (collision.type == Collision::Type::wall) | ||
152 | { | ||
153 | if (dir == Direction::left) | ||
154 | { | ||
155 | collider.position.first = collision.axis; | ||
156 | collider.send(game, Message::Type::stopMovingHorizontally); | ||
157 | } else if (dir == Direction::right) | ||
158 | { | ||
159 | collider.position.first = collision.axis - collider.size.first; | ||
160 | collider.send(game, Message::Type::stopMovingHorizontally); | ||
161 | } else if (dir == Direction::up) | ||
162 | { | ||
163 | collider.position.second = collision.axis; | ||
164 | collider.send(game, Message::Type::stopMovingVertically); | ||
165 | } else if (dir == Direction::down) | ||
166 | { | ||
167 | collider.position.second = collision.axis - collider.size.second; | ||
168 | collider.send(game, Message::Type::hitTheGround); | ||
169 | } | ||
170 | } else if (collision.type == Collision::Type::wrap) | ||
171 | { | ||
172 | if (dir == Direction::left) | ||
173 | { | ||
174 | collider.position.first = GAME_WIDTH-collider.size.first/2; | ||
175 | } else if (dir == Direction::right) | ||
176 | { | ||
177 | collider.position.first = -collider.size.first/2; | ||
178 | } else if (dir == Direction::up) | ||
179 | { | ||
180 | collider.position.second = GAME_HEIGHT-collider.size.second/2-1; | ||
181 | } else if (dir == Direction::down) | ||
182 | { | ||
183 | collider.position.second = -collider.size.second/2; | ||
184 | } | ||
185 | } else if (collision.type == Collision::Type::teleport) | ||
186 | { | ||
187 | if (dir == Direction::left) | ||
188 | { | ||
189 | game.loadMap(*map.getLeftMap(), std::make_pair(GAME_WIDTH-collider.size.first/2, old_position.second)); | ||
190 | } else if (dir == Direction::right) | ||
191 | { | ||
192 | game.loadMap(*map.getRightMap(), std::make_pair(-collider.size.first/2, old_position.second)); | ||
193 | } | ||
194 | } else if (collision.type == Collision::Type::reverse) | ||
195 | { | ||
196 | if (dir == Direction::right) | ||
197 | { | ||
198 | collider.position.first = collision.axis - collider.size.first; | ||
199 | collider.send(game, Message::Type::walkLeft); | ||
200 | } | ||
201 | } else if (collision.type == Collision::Type::platform) | ||
202 | { | ||
203 | Message msg(Message::Type::drop); | ||
204 | msg.dropAxis = collision.axis; | ||
205 | |||
206 | collider.send(game, msg); | ||
207 | } else if (collision.type == Collision::Type::danger) | ||
208 | { | ||
209 | game.playerDie(); | ||
210 | } | ||
211 | } | ||
diff --git a/src/components/map_collision.h b/src/components/map_collision.h new file mode 100644 index 0000000..3b718b6 --- /dev/null +++ b/src/components/map_collision.h | |||
@@ -0,0 +1,46 @@ | |||
1 | #ifndef MAP_COLLISION_H | ||
2 | #define MAP_COLLISION_H | ||
3 | |||
4 | #include "entity.h" | ||
5 | #include <list> | ||
6 | |||
7 | class Map; | ||
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 | |||
39 | std::list<Collision> left_collisions; | ||
40 | std::list<Collision> right_collisions; | ||
41 | std::list<Collision> up_collisions; | ||
42 | std::list<Collision> down_collisions; | ||
43 | const Map& map; | ||
44 | }; | ||
45 | |||
46 | #endif | ||
diff --git a/src/components/map_render.cpp b/src/components/map_render.cpp new file mode 100644 index 0000000..d93afe6 --- /dev/null +++ b/src/components/map_render.cpp | |||
@@ -0,0 +1,39 @@ | |||
1 | #include "map_render.h" | ||
2 | #include "map.h" | ||
3 | #include "game.h" | ||
4 | |||
5 | MapRenderComponent::MapRenderComponent(const Map& map) : screen(GAME_WIDTH, GAME_HEIGHT) | ||
6 | { | ||
7 | screen.fill(screen.entirety(), 0, 0, 0); | ||
8 | |||
9 | Texture tiles("../res/tiles.png"); | ||
10 | |||
11 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | ||
12 | { | ||
13 | int tile = map.getMapdata()[i]; | ||
14 | int x = i % MAP_WIDTH; | ||
15 | int y = i / MAP_WIDTH; | ||
16 | Rectangle dst {x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; | ||
17 | Rectangle src {tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; | ||
18 | |||
19 | if (tile > 0) | ||
20 | { | ||
21 | screen.blit(tiles, src, dst); | ||
22 | } | ||
23 | } | ||
24 | |||
25 | Texture font("../res/font.bmp"); | ||
26 | const char* map_name = map.getTitle(); | ||
27 | int start_x = (40/2) - (strlen(map_name)/2); | ||
28 | for (size_t i=0; i<strlen(map_name); i++) | ||
29 | { | ||
30 | Rectangle srcRect {map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8}; | ||
31 | Rectangle dstRect {(start_x + (int)i)*8, 24*8, 8, 8}; | ||
32 | screen.blit(font, srcRect, dstRect); | ||
33 | } | ||
34 | } | ||
35 | |||
36 | void MapRenderComponent::render(Game&, Entity&, Texture& buffer) | ||
37 | { | ||
38 | buffer.blit(screen, screen.entirety(), buffer.entirety()); | ||
39 | } | ||
diff --git a/src/components/map_render.h b/src/components/map_render.h new file mode 100644 index 0000000..a232aa6 --- /dev/null +++ b/src/components/map_render.h | |||
@@ -0,0 +1,19 @@ | |||
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 new file mode 100644 index 0000000..72f2fd8 --- /dev/null +++ b/src/components/physics_body.cpp | |||
@@ -0,0 +1,65 @@ | |||
1 | #include "physics_body.h" | ||
2 | #include "game.h" | ||
3 | |||
4 | void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) | ||
5 | { | ||
6 | if (msg.type == Message::Type::walkLeft) | ||
7 | { | ||
8 | velocity.first = -90; | ||
9 | } else if (msg.type == Message::Type::walkRight) | ||
10 | { | ||
11 | velocity.first = 90; | ||
12 | } else if (msg.type == Message::Type::stopWalking) | ||
13 | { | ||
14 | velocity.first = 0.0; | ||
15 | } else if (msg.type == Message::Type::stopMovingHorizontally) | ||
16 | { | ||
17 | velocity.first = 0.0; | ||
18 | } else if (msg.type == Message::Type::stopMovingVertically) | ||
19 | { | ||
20 | velocity.second = 0.0; | ||
21 | } | ||
22 | } | ||
23 | |||
24 | void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt) | ||
25 | { | ||
26 | // Accelerate | ||
27 | velocity.first += accel.first * dt; | ||
28 | velocity.second += accel.second * dt; | ||
29 | |||
30 | // Terminal velocity | ||
31 | #define TERMINAL_VELOCITY_X (2 * TILE_WIDTH * FRAMES_PER_SECOND) | ||
32 | #define TERMINAL_VELOCITY_Y (2 * TILE_HEIGHT * FRAMES_PER_SECOND) | ||
33 | if (velocity.first < -TERMINAL_VELOCITY_X) velocity.first = -TERMINAL_VELOCITY_X; | ||
34 | if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X; | ||
35 | if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y; | ||
36 | if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y; | ||
37 | |||
38 | // Do the movement | ||
39 | entity.position.first += velocity.first * dt; | ||
40 | entity.position.second += velocity.second * dt; | ||
41 | } | ||
42 | |||
43 | void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) | ||
44 | { | ||
45 | // If already colliding, do nothing! | ||
46 | if ((old_position.first + collider.size.first > entity.position.first) | ||
47 | && (old_position.first < entity.position.first + entity.size.first) | ||
48 | && (old_position.second + collider.size.second > entity.position.second) | ||
49 | && (old_position.second < entity.position.second + entity.size.second)) | ||
50 | { | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | // If newly colliding, SHOCK AND HORROR! | ||
55 | if ((collider.position.first + collider.size.first > entity.position.first) | ||
56 | && (collider.position.first < entity.position.first + entity.size.first) | ||
57 | && (collider.position.second + collider.size.second > entity.position.second) | ||
58 | && (collider.position.second < entity.position.second + entity.size.second)) | ||
59 | { | ||
60 | Message msg(Message::Type::collision); | ||
61 | msg.collisionEntity = &collider; | ||
62 | |||
63 | entity.send(game, msg); | ||
64 | } | ||
65 | } | ||
diff --git a/src/components/physics_body.h b/src/components/physics_body.h new file mode 100644 index 0000000..079cc51 --- /dev/null +++ b/src/components/physics_body.h | |||
@@ -0,0 +1,20 @@ | |||
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 new file mode 100644 index 0000000..1be43b6 --- /dev/null +++ b/src/components/player_physics.cpp | |||
@@ -0,0 +1,117 @@ | |||
1 | #include "player_physics.h" | ||
2 | #include "muxer.h" | ||
3 | #include "game.h" | ||
4 | |||
5 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) | ||
6 | #define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) | ||
7 | |||
8 | PlayerPhysicsComponent::PlayerPhysicsComponent() | ||
9 | { | ||
10 | jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3); | ||
11 | jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3); | ||
12 | jump_gravity_short = JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233); | ||
13 | |||
14 | accel.second = jump_gravity_short; | ||
15 | } | ||
16 | |||
17 | void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) | ||
18 | { | ||
19 | if (msg.type == Message::Type::walkLeft) | ||
20 | { | ||
21 | velocity.first = -90; | ||
22 | direction = -1; | ||
23 | } else if (msg.type == Message::Type::walkRight) | ||
24 | { | ||
25 | velocity.first = 90; | ||
26 | direction = 1; | ||
27 | } else if (msg.type == Message::Type::stopWalking) | ||
28 | { | ||
29 | velocity.first = 0.0; | ||
30 | direction = 0; | ||
31 | } else if (msg.type == Message::Type::stopMovingHorizontally) | ||
32 | { | ||
33 | velocity.first = 0.0; | ||
34 | } else if (msg.type == Message::Type::stopMovingVertically) | ||
35 | { | ||
36 | velocity.second = 0.0; | ||
37 | } else if (msg.type == Message::Type::hitTheGround) | ||
38 | { | ||
39 | if (isFalling) | ||
40 | { | ||
41 | playSound("../res/Randomize27.wav", 0.05); | ||
42 | isFalling = false; | ||
43 | } | ||
44 | |||
45 | velocity.second = 0.0; | ||
46 | } else if (msg.type == Message::Type::jump) | ||
47 | { | ||
48 | playSound("../res/Randomize87.wav", 0.25); | ||
49 | |||
50 | velocity.second = jump_velocity; | ||
51 | accel.second = jump_gravity; | ||
52 | } else if (msg.type == Message::Type::stopJump) | ||
53 | { | ||
54 | accel.second = jump_gravity_short; | ||
55 | } else if (msg.type == Message::Type::canDrop) | ||
56 | { | ||
57 | canDrop = true; | ||
58 | } else if (msg.type == Message::Type::cantDrop) | ||
59 | { | ||
60 | canDrop = false; | ||
61 | } else if (msg.type == Message::Type::drop) | ||
62 | { | ||
63 | if (canDrop) | ||
64 | { | ||
65 | canDrop = false; | ||
66 | } else { | ||
67 | entity.position.second = msg.dropAxis - entity.size.second; | ||
68 | velocity.second = 0; | ||
69 | } | ||
70 | } else if (msg.type == Message::Type::die) | ||
71 | { | ||
72 | frozen = true; | ||
73 | } else if (msg.type == Message::Type::stopDying) | ||
74 | { | ||
75 | frozen = false; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity, double dt) | ||
80 | { | ||
81 | // If frozen, do nothing | ||
82 | if (frozen) | ||
83 | { | ||
84 | return; | ||
85 | } | ||
86 | |||
87 | // Continue walking even if blocked earlier | ||
88 | if (velocity.first == 0) | ||
89 | { | ||
90 | if (direction < 0) | ||
91 | { | ||
92 | velocity.first = -90; | ||
93 | } else if (direction > 0) | ||
94 | { | ||
95 | velocity.first = 90; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | // Increase gravity at the height of jump | ||
100 | if ((accel.second == jump_gravity) && (velocity.second >= 0)) | ||
101 | { | ||
102 | accel.second = jump_gravity_short; | ||
103 | } | ||
104 | |||
105 | // Do the movement | ||
106 | std::pair<double, double> old_position = entity.position; | ||
107 | PhysicsBodyComponent::tick(game, entity, dt); | ||
108 | |||
109 | // Check for collisions | ||
110 | game.detectCollision(entity, old_position); | ||
111 | |||
112 | // Are we moving due to gravity? | ||
113 | if (velocity.second != 0.0) | ||
114 | { | ||
115 | isFalling = true; | ||
116 | } | ||
117 | } | ||
diff --git a/src/components/player_physics.h b/src/components/player_physics.h new file mode 100644 index 0000000..26f1fae --- /dev/null +++ b/src/components/player_physics.h | |||
@@ -0,0 +1,25 @@ | |||
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 new file mode 100644 index 0000000..2814852 --- /dev/null +++ b/src/components/player_sprite.cpp | |||
@@ -0,0 +1,60 @@ | |||
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 new file mode 100644 index 0000000..b1ac0af --- /dev/null +++ b/src/components/player_sprite.h | |||
@@ -0,0 +1,23 @@ | |||
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 new file mode 100644 index 0000000..f4b414e --- /dev/null +++ b/src/components/simple_collider.cpp | |||
@@ -0,0 +1,14 @@ | |||
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 new file mode 100644 index 0000000..15d78cf --- /dev/null +++ b/src/components/simple_collider.h | |||
@@ -0,0 +1,18 @@ | |||
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 new file mode 100644 index 0000000..9fa8dca --- /dev/null +++ b/src/components/static_image.cpp | |||
@@ -0,0 +1,11 @@ | |||
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 new file mode 100644 index 0000000..2dec78b --- /dev/null +++ b/src/components/static_image.h | |||
@@ -0,0 +1,18 @@ | |||
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 new file mode 100644 index 0000000..e499fee --- /dev/null +++ b/src/components/user_movement.cpp | |||
@@ -0,0 +1,100 @@ | |||
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 new file mode 100644 index 0000000..1bcf05e --- /dev/null +++ b/src/components/user_movement.h | |||
@@ -0,0 +1,19 @@ | |||
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 | ||