diff options
Diffstat (limited to 'src/components.cpp')
-rw-r--r-- | src/components.cpp | 353 |
1 files changed, 163 insertions, 190 deletions
diff --git a/src/components.cpp b/src/components.cpp index f9c7c10..369bb9e 100644 --- a/src/components.cpp +++ b/src/components.cpp | |||
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | // User movement component | 4 | // User movement component |
5 | 5 | ||
6 | void UserMovementComponent::input(int key, int action) | 6 | void UserMovementComponent::input(Game& game, Entity& entity, int key, int action) |
7 | { | 7 | { |
8 | if (action == GLFW_PRESS) | 8 | if (action == GLFW_PRESS) |
9 | { | 9 | { |
@@ -11,30 +11,22 @@ void UserMovementComponent::input(int key, int action) | |||
11 | { | 11 | { |
12 | holdingLeft = true; | 12 | holdingLeft = true; |
13 | 13 | ||
14 | message_t msg; | 14 | Message msg(Message::Type::walkLeft); |
15 | msg.type = CM_WALK_LEFT; | 15 | entity.send(game, msg); |
16 | |||
17 | entity.send(msg); | ||
18 | } else if (key == GLFW_KEY_RIGHT) | 16 | } else if (key == GLFW_KEY_RIGHT) |
19 | { | 17 | { |
20 | holdingRight = true; | 18 | holdingRight = true; |
21 | 19 | ||
22 | message_t msg; | 20 | Message msg(Message::Type::walkRight); |
23 | msg.type = CM_WALK_RIGHT; | 21 | entity.send(game, msg); |
24 | |||
25 | entity.send(msg); | ||
26 | } else if (key == GLFW_KEY_UP) | 22 | } else if (key == GLFW_KEY_UP) |
27 | { | 23 | { |
28 | message_t msg; | 24 | Message msg(Message::Type::jump); |
29 | msg.type = CM_JUMP; | 25 | entity.send(game, msg); |
30 | |||
31 | entity.send(msg); | ||
32 | } else if (key == GLFW_KEY_DOWN) | 26 | } else if (key == GLFW_KEY_DOWN) |
33 | { | 27 | { |
34 | message_t msg; | 28 | Message msg(Message::Type::canDrop); |
35 | msg.type = CM_CAN_DROP; | 29 | entity.send(game, msg); |
36 | |||
37 | entity.send(msg); | ||
38 | } | 30 | } |
39 | } else if (action == GLFW_RELEASE) | 31 | } else if (action == GLFW_RELEASE) |
40 | { | 32 | { |
@@ -44,15 +36,11 @@ void UserMovementComponent::input(int key, int action) | |||
44 | 36 | ||
45 | if (holdingRight) | 37 | if (holdingRight) |
46 | { | 38 | { |
47 | message_t msg; | 39 | Message msg(Message::Type::walkRight); |
48 | msg.type = CM_WALK_RIGHT; | 40 | entity.send(game, msg); |
49 | |||
50 | entity.send(msg); | ||
51 | } else { | 41 | } else { |
52 | message_t msg; | 42 | Message msg(Message::Type::stopWalking); |
53 | msg.type = CM_STOP_WALKING; | 43 | entity.send(game, msg); |
54 | |||
55 | entity.send(msg); | ||
56 | } | 44 | } |
57 | } else if (key == GLFW_KEY_RIGHT) | 45 | } else if (key == GLFW_KEY_RIGHT) |
58 | { | 46 | { |
@@ -60,95 +48,82 @@ void UserMovementComponent::input(int key, int action) | |||
60 | 48 | ||
61 | if (holdingLeft) | 49 | if (holdingLeft) |
62 | { | 50 | { |
63 | message_t msg; | 51 | Message msg(Message::Type::walkLeft); |
64 | msg.type = CM_WALK_LEFT; | 52 | entity.send(game, msg); |
65 | |||
66 | entity.send(msg); | ||
67 | } else { | 53 | } else { |
68 | message_t msg; | 54 | Message msg(Message::Type::stopWalking); |
69 | msg.type = CM_STOP_WALKING; | 55 | entity.send(game, msg); |
70 | |||
71 | entity.send(msg); | ||
72 | } | 56 | } |
73 | } else if (key == GLFW_KEY_DOWN) | 57 | } else if (key == GLFW_KEY_DOWN) |
74 | { | 58 | { |
75 | message_t msg; | 59 | Message msg(Message::Type::cantDrop); |
76 | msg.type = CM_CANT_DROP; | 60 | entity.send(game, msg); |
77 | |||
78 | entity.send(msg); | ||
79 | } else if (key == GLFW_KEY_UP) | 61 | } else if (key == GLFW_KEY_UP) |
80 | { | 62 | { |
81 | message_t msg; | 63 | Message msg(Message::Type::stopJump); |
82 | msg.type = CM_STOP_JUMP; | 64 | entity.send(game, msg); |
83 | |||
84 | entity.send(msg); | ||
85 | } | 65 | } |
86 | } | 66 | } |
87 | } | 67 | } |
88 | 68 | ||
89 | // Physics component | 69 | // Physics component |
90 | 70 | ||
91 | void PhysicsBodyComponent::receive(message_t msg) | 71 | void PhysicsBodyComponent::receive(Game&, Entity&, Message& msg) |
92 | { | 72 | { |
93 | if (msg.type == CM_WALK_LEFT) | 73 | if (msg.type == Message::Type::walkLeft) |
94 | { | 74 | { |
95 | velocity.first = -1.5; | 75 | velocity.first = -1.5; |
96 | } else if (msg.type == CM_WALK_RIGHT) | 76 | } else if (msg.type == Message::Type::walkRight) |
97 | { | 77 | { |
98 | velocity.first = 1.5; | 78 | velocity.first = 1.5; |
99 | } else if (msg.type == CM_STOP_WALKING) | 79 | } else if (msg.type == Message::Type::stopWalking) |
100 | { | 80 | { |
101 | velocity.first = 0.0; | 81 | velocity.first = 0.0; |
82 | } else if (msg.type == Message::Type::stopMovingHorizontally) | ||
83 | { | ||
84 | velocity.first = 0.0; | ||
85 | } else if (msg.type == Message::Type::stopMovingVertically) | ||
86 | { | ||
87 | velocity.second = 0.0; | ||
102 | } | 88 | } |
103 | } | 89 | } |
104 | 90 | ||
105 | void PhysicsBodyComponent::tick() | 91 | void PhysicsBodyComponent::tick(Game&, Entity& entity) |
106 | { | 92 | { |
107 | velocity.first += accel.first; | 93 | velocity.first += accel.first; |
108 | velocity.second += accel.second; | 94 | velocity.second += accel.second; |
109 | 95 | ||
110 | position.first += velocity.first; | 96 | entity.position.first += velocity.first; |
111 | position.second += velocity.second; | 97 | entity.position.second += velocity.second; |
112 | } | 98 | } |
113 | 99 | ||
114 | void PhysicsBodyComponent::detectCollision(Entity& player, Locatable& physics, std::pair<double, double> old_position) | 100 | void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) |
115 | { | 101 | { |
116 | // If already colliding, do nothing! | 102 | // If already colliding, do nothing! |
117 | if ((old_position.first + physics.size.first > this->position.first) | 103 | if ((old_position.first + collider.size.first > entity.position.first) |
118 | && (old_position.first < this->position.first + this->size.first) | 104 | && (old_position.first < entity.position.first + entity.size.first) |
119 | && (old_position.second + physics.size.second > this->position.second) | 105 | && (old_position.second + collider.size.second > entity.position.second) |
120 | && (old_position.second < this->position.second + this->size.second)) | 106 | && (old_position.second < entity.position.second + entity.size.second)) |
121 | { | 107 | { |
122 | return; | 108 | return; |
123 | } | 109 | } |
124 | 110 | ||
125 | // If newly colliding, SHOCK AND HORROR! | 111 | // If newly colliding, SHOCK AND HORROR! |
126 | if ((physics.position.first + physics.size.first > this->position.first) | 112 | if ((collider.position.first + collider.size.first > entity.position.first) |
127 | && (physics.position.first < this->position.first + this->size.first) | 113 | && (collider.position.first < entity.position.first + entity.size.first) |
128 | && (physics.position.second + physics.size.second > this->position.second) | 114 | && (collider.position.second + collider.size.second > entity.position.second) |
129 | && (physics.position.second < this->position.second + this->size.second)) | 115 | && (collider.position.second < entity.position.second + entity.size.second)) |
130 | { | 116 | { |
131 | message_t msg; | 117 | Message msg(Message::Type::collision); |
132 | msg.type = CM_COLLISION; | 118 | msg.collisionEntity = &collider; |
133 | msg.collisionEntity = &player; | ||
134 | 119 | ||
135 | entity.send(msg); | 120 | entity.send(game, msg); |
136 | } | 121 | } |
137 | } | 122 | } |
138 | 123 | ||
139 | // Render player | 124 | // Render player |
140 | 125 | ||
141 | PlayerSpriteComponent::PlayerSpriteComponent(Entity& entity, Locatable& physics) : Component(entity), physics(physics) | 126 | void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) |
142 | { | ||
143 | sprite = loadTextureFromFile("../res/Starla.png"); | ||
144 | } | ||
145 | |||
146 | PlayerSpriteComponent::~PlayerSpriteComponent() | ||
147 | { | ||
148 | destroyTexture(sprite); | ||
149 | } | ||
150 | |||
151 | void PlayerSpriteComponent::render(Texture* buffer) | ||
152 | { | 127 | { |
153 | int frame = 0; | 128 | int frame = 0; |
154 | if (isMoving) | 129 | if (isMoving) |
@@ -162,28 +137,28 @@ void PlayerSpriteComponent::render(Texture* buffer) | |||
162 | } | 137 | } |
163 | if (facingLeft) frame++; | 138 | if (facingLeft) frame++; |
164 | 139 | ||
165 | Rectangle src_rect(frame*10, 0, 10, 12); | 140 | Rectangle src_rect {frame*10, 0, 10, 12}; |
166 | Rectangle dst_rect((int) physics.position.first, (int) physics.position.second, physics.size.first, physics.size.second); | 141 | Rectangle dst_rect {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second}; |
167 | blitTexture(sprite, buffer, &src_rect, &dst_rect); | 142 | buffer.blit(sprite, src_rect, dst_rect); |
168 | } | 143 | } |
169 | 144 | ||
170 | void PlayerSpriteComponent::receive(message_t msg) | 145 | void PlayerSpriteComponent::receive(Game&, Entity&, Message& msg) |
171 | { | 146 | { |
172 | if (msg.type == CM_WALK_LEFT) | 147 | if (msg.type == Message::Type::walkLeft) |
173 | { | 148 | { |
174 | facingLeft = true; | 149 | facingLeft = true; |
175 | isMoving = true; | 150 | isMoving = true; |
176 | } else if (msg.type == CM_WALK_RIGHT) | 151 | } else if (msg.type == Message::Type::walkRight) |
177 | { | 152 | { |
178 | facingLeft = false; | 153 | facingLeft = false; |
179 | isMoving = true; | 154 | isMoving = true; |
180 | } else if (msg.type == CM_STOP_WALKING) | 155 | } else if (msg.type == Message::Type::stopWalking) |
181 | { | 156 | { |
182 | isMoving = false; | 157 | isMoving = false; |
183 | } | 158 | } |
184 | } | 159 | } |
185 | 160 | ||
186 | void PlayerSpriteComponent::tick() | 161 | void PlayerSpriteComponent::tick(Game&, Entity&) |
187 | { | 162 | { |
188 | animFrame++; | 163 | animFrame++; |
189 | animFrame %= 20; | 164 | animFrame %= 20; |
@@ -194,7 +169,7 @@ void PlayerSpriteComponent::tick() | |||
194 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) | 169 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) |
195 | #define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) | 170 | #define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) |
196 | 171 | ||
197 | PlayerPhysicsComponent::PlayerPhysicsComponent(Entity& entity) : Component(entity) | 172 | PlayerPhysicsComponent::PlayerPhysicsComponent() |
198 | { | 173 | { |
199 | jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3*FRAMES_PER_SECOND); | 174 | jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3*FRAMES_PER_SECOND); |
200 | jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3*FRAMES_PER_SECOND); | 175 | jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3*FRAMES_PER_SECOND); |
@@ -203,46 +178,52 @@ PlayerPhysicsComponent::PlayerPhysicsComponent(Entity& entity) : Component(entit | |||
203 | accel.second = jump_gravity_short; | 178 | accel.second = jump_gravity_short; |
204 | } | 179 | } |
205 | 180 | ||
206 | void PlayerPhysicsComponent::receive(message_t msg) | 181 | void PlayerPhysicsComponent::receive(Game&, Entity& entity, Message& msg) |
207 | { | 182 | { |
208 | if (msg.type == CM_WALK_LEFT) | 183 | if (msg.type == Message::Type::walkLeft) |
209 | { | 184 | { |
210 | velocity.first = -1.5; | 185 | velocity.first = -1.5; |
211 | direction = -1; | 186 | direction = -1; |
212 | } else if (msg.type == CM_WALK_RIGHT) | 187 | } else if (msg.type == Message::Type::walkRight) |
213 | { | 188 | { |
214 | velocity.first = 1.5; | 189 | velocity.first = 1.5; |
215 | direction = 1; | 190 | direction = 1; |
216 | } else if (msg.type == CM_STOP_WALKING) | 191 | } else if (msg.type == Message::Type::stopWalking) |
217 | { | 192 | { |
218 | velocity.first = 0.0; | 193 | velocity.first = 0.0; |
219 | direction = 0; | 194 | direction = 0; |
220 | } else if (msg.type == CM_JUMP) | 195 | } else if (msg.type == Message::Type::stopMovingHorizontally) |
196 | { | ||
197 | velocity.first = 0.0; | ||
198 | } else if (msg.type == Message::Type::stopMovingVertically) | ||
199 | { | ||
200 | velocity.second = 0.0; | ||
201 | } else if (msg.type == Message::Type::jump) | ||
221 | { | 202 | { |
222 | velocity.second = jump_velocity; | 203 | velocity.second = jump_velocity; |
223 | accel.second = jump_gravity; | 204 | accel.second = jump_gravity; |
224 | } else if (msg.type == CM_STOP_JUMP) | 205 | } else if (msg.type == Message::Type::stopJump) |
225 | { | 206 | { |
226 | accel.second = jump_gravity_short; | 207 | accel.second = jump_gravity_short; |
227 | } else if (msg.type == CM_CAN_DROP) | 208 | } else if (msg.type == Message::Type::canDrop) |
228 | { | 209 | { |
229 | canDrop = true; | 210 | canDrop = true; |
230 | } else if (msg.type == CM_CANT_DROP) | 211 | } else if (msg.type == Message::Type::cantDrop) |
231 | { | 212 | { |
232 | canDrop = false; | 213 | canDrop = false; |
233 | } else if (msg.type == CM_DROP) | 214 | } else if (msg.type == Message::Type::drop) |
234 | { | 215 | { |
235 | if (canDrop) | 216 | if (canDrop) |
236 | { | 217 | { |
237 | canDrop = false; | 218 | canDrop = false; |
238 | } else { | 219 | } else { |
239 | position.second = msg.dropAxis - size.second; | 220 | entity.position.second = msg.dropAxis - entity.size.second; |
240 | velocity.second = 0; | 221 | velocity.second = 0; |
241 | } | 222 | } |
242 | } | 223 | } |
243 | } | 224 | } |
244 | 225 | ||
245 | void PlayerPhysicsComponent::tick() | 226 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity) |
246 | { | 227 | { |
247 | // Continue walking even if blocked earlier | 228 | // Continue walking even if blocked earlier |
248 | if (velocity.first == 0) | 229 | if (velocity.first == 0) |
@@ -273,80 +254,67 @@ void PlayerPhysicsComponent::tick() | |||
273 | if (velocity.second > 16) velocity.second = 16; | 254 | if (velocity.second > 16) velocity.second = 16; |
274 | 255 | ||
275 | // Do the movement | 256 | // Do the movement |
276 | std::pair<double, double> old_position = std::make_pair(position.first, position.second); | 257 | std::pair<double, double> old_position = entity.position; |
277 | position.first += velocity.first; | 258 | entity.position.first += velocity.first; |
278 | position.second += velocity.second; | 259 | entity.position.second += velocity.second; |
279 | 260 | ||
280 | // Check for collisions | 261 | // Check for collisions |
281 | for (auto it = entity.world->bodies.begin(); it != entity.world->bodies.end(); it++) | 262 | game.detectCollision(entity, old_position); |
282 | { | ||
283 | auto poop = *it; | ||
284 | poop->detectCollision(entity, *this, old_position); | ||
285 | } | ||
286 | } | 263 | } |
287 | 264 | ||
288 | // Map rendering | 265 | // Map rendering |
289 | 266 | ||
290 | MapRenderComponent::MapRenderComponent(Entity& entity, Map* map) : Component(entity) | 267 | MapRenderComponent::MapRenderComponent(Map& map) |
291 | { | 268 | { |
292 | screen = createTexture(GAME_WIDTH, GAME_HEIGHT); | 269 | screen.fill(screen.entirety(), 0, 0, 0); |
293 | fillTexture(screen, NULL, 0, 0, 0); | ||
294 | 270 | ||
295 | Texture* tiles = loadTextureFromFile("../res/tiles.png"); | 271 | Texture tiles("../res/tiles.png"); |
296 | 272 | ||
297 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | 273 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) |
298 | { | 274 | { |
299 | int tile = map->mapdata()[i]; | 275 | int tile = map.mapdata()[i]; |
300 | int x = i % MAP_WIDTH; | 276 | int x = i % MAP_WIDTH; |
301 | int y = i / MAP_WIDTH; | 277 | int y = i / MAP_WIDTH; |
302 | Rectangle dst(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | 278 | Rectangle dst {x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; |
303 | Rectangle src(tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | 279 | Rectangle src {tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; |
304 | 280 | ||
305 | if (tile > 0) | 281 | if (tile > 0) |
306 | { | 282 | { |
307 | blitTexture(tiles, screen, &src, &dst); | 283 | screen.blit(tiles, src, dst); |
308 | } | 284 | } |
309 | } | 285 | } |
310 | 286 | ||
311 | destroyTexture(tiles); | 287 | Texture font("../res/font.bmp"); |
312 | 288 | const char* map_name = map.title(); | |
313 | Texture* font = loadTextureFromFile("../res/font.bmp"); | ||
314 | const char* map_name = map->title(); | ||
315 | int start_x = (40/2) - (strlen(map_name)/2); | 289 | int start_x = (40/2) - (strlen(map_name)/2); |
316 | for (size_t i=0; i<strlen(map_name); i++) | 290 | for (size_t i=0; i<strlen(map_name); i++) |
317 | { | 291 | { |
318 | Rectangle srcRect(map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8); | 292 | Rectangle srcRect {map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8}; |
319 | Rectangle dstRect((start_x + i)*8, 24*8, 8, 8); | 293 | Rectangle dstRect {(start_x + (int)i)*8, 24*8, 8, 8}; |
320 | blitTexture(font, screen, &srcRect, &dstRect); | 294 | screen.blit(font, srcRect, dstRect); |
321 | } | 295 | } |
322 | |||
323 | destroyTexture(font); | ||
324 | } | 296 | } |
325 | 297 | ||
326 | MapRenderComponent::~MapRenderComponent() | 298 | void MapRenderComponent::render(Game&, Entity&, Texture& buffer) |
327 | { | 299 | { |
328 | destroyTexture(screen); | 300 | buffer.blit(screen, screen.entirety(), buffer.entirety()); |
329 | } | ||
330 | |||
331 | void MapRenderComponent::render(Texture* buffer) | ||
332 | { | ||
333 | blitTexture(screen, buffer, NULL, NULL); | ||
334 | } | 301 | } |
335 | 302 | ||
336 | // Map collision | 303 | // Map collision |
337 | 304 | ||
338 | MapCollisionComponent::MapCollisionComponent(Entity& entity, Map* map) : Component(entity) | 305 | MapCollisionComponent::MapCollisionComponent(Map& map) |
339 | { | 306 | { |
340 | this->map = map; | 307 | leftMap = map.getLeftMap(); |
308 | rightMap = map.getRightMap(); | ||
341 | 309 | ||
342 | add_collision(-6, 0, GAME_WIDTH, left, (map->getLeftMap() == NULL) ? 1 : 2); | 310 | add_collision(-6, 0, GAME_WIDTH, left, (map.getLeftMap() == nullptr) ? 1 : 2); |
343 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (map->getRightMap() == NULL) ? 3 : 2); | 311 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (map.getRightMap() == nullptr) ? 3 : 2); |
344 | 312 | ||
345 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | 313 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) |
346 | { | 314 | { |
347 | int x = i % MAP_WIDTH; | 315 | int x = i % MAP_WIDTH; |
348 | int y = i / MAP_WIDTH; | 316 | int y = i / MAP_WIDTH; |
349 | int tile = map->mapdata()[i]; | 317 | int tile = map.mapdata()[i]; |
350 | 318 | ||
351 | if ((tile > 0) && (!((tile >= 5) && (tile <= 7)))) | 319 | if ((tile > 0) && (!((tile >= 5) && (tile <= 7)))) |
352 | { | 320 | { |
@@ -410,34 +378,36 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct | |||
410 | } | 378 | } |
411 | } | 379 | } |
412 | 380 | ||
413 | void MapCollisionComponent::detectCollision(Entity& player, Locatable& physics, std::pair<double, double> old_position) | 381 | void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collider, std::pair<double, double> old_position) |
414 | { | 382 | { |
415 | int fixed_x = (int) physics.position.first; | 383 | int fixed_x = (int) collider.position.first; |
416 | int fixed_y = (int) physics.position.second; | 384 | int fixed_y = (int) collider.position.second; |
417 | int fixed_ox = (int) old_position.first; | 385 | int fixed_ox = (int) old_position.first; |
418 | int fixed_oy = (int) old_position.second; | 386 | int fixed_oy = (int) old_position.second; |
419 | 387 | ||
420 | if (fixed_x < fixed_ox) | 388 | if (fixed_x < fixed_ox) |
421 | { | 389 | { |
422 | for (auto it=left_collisions.begin(); it!=left_collisions.end(); it++) | 390 | for (auto collision : left_collisions) |
423 | { | 391 | { |
424 | if (it->axis > fixed_ox) continue; | 392 | if (collision.axis > fixed_ox) continue; |
425 | if (it->axis < fixed_x) break; | 393 | if (collision.axis < fixed_x) break; |
426 | 394 | ||
427 | if ((fixed_oy+physics.size.second > it->lower) && (fixed_oy < it->upper)) | 395 | if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) |
428 | { | 396 | { |
429 | // We have a collision! | 397 | // We have a collision! |
430 | if (it->type == 0) | 398 | if (collision.type == 0) |
431 | { | 399 | { |
432 | physics.position.first = it->axis; | 400 | collider.position.first = collision.axis; |
433 | physics.velocity.first = 0; | 401 | |
434 | } else if (it->type == 1) | 402 | Message msg(Message::Type::stopMovingHorizontally); |
403 | collider.send(game, msg); | ||
404 | } else if (collision.type == 1) | ||
435 | { | 405 | { |
436 | physics.position.first = GAME_WIDTH-physics.size.first/2; | 406 | collider.position.first = GAME_WIDTH-collider.size.first/2; |
437 | } else if (it->type == 2) | 407 | } else if (collision.type == 2) |
438 | { | 408 | { |
439 | physics.position.first = GAME_WIDTH-physics.size.first/2; | 409 | collider.position.first = GAME_WIDTH-collider.size.first/2; |
440 | Game::getInstance().loadMap(map->getLeftMap()); | 410 | game.loadMap(*leftMap); |
441 | } | 411 | } |
442 | 412 | ||
443 | break; | 413 | break; |
@@ -445,32 +415,33 @@ void MapCollisionComponent::detectCollision(Entity& player, Locatable& physics, | |||
445 | } | 415 | } |
446 | } else if (fixed_x > fixed_ox) | 416 | } else if (fixed_x > fixed_ox) |
447 | { | 417 | { |
448 | for (auto it=right_collisions.begin(); it!=right_collisions.end(); it++) | 418 | for (auto collision : right_collisions) |
449 | { | 419 | { |
450 | if (it->axis < fixed_ox+physics.size.first) continue; | 420 | if (collision.axis < fixed_ox+collider.size.first) continue; |
451 | if (it->axis > fixed_x+physics.size.first) break; | 421 | if (collision.axis > fixed_x+collider.size.first) break; |
452 | 422 | ||
453 | if ((fixed_oy+physics.size.second > it->lower) && (fixed_oy < it->upper)) | 423 | if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) |
454 | { | 424 | { |
455 | // We have a collision! | 425 | // We have a collision! |
456 | if (it->type == 0) | 426 | if (collision.type == 0) |
457 | { | 427 | { |
458 | physics.position.first = it->axis - physics.size.first; | 428 | collider.position.first = collision.axis - collider.size.first; |
459 | physics.velocity.first = 0; | 429 | |
460 | } else if (it->type == 1) | 430 | Message msg(Message::Type::stopMovingHorizontally); |
431 | collider.send(game, msg); | ||
432 | } else if (collision.type == 1) | ||
461 | { | 433 | { |
462 | physics.position.first = -physics.size.first/2; | 434 | collider.position.first = -collider.size.first/2; |
463 | } else if (it->type == 2) | 435 | } else if (collision.type == 2) |
464 | { | 436 | { |
465 | physics.position.first = -physics.size.first/2; | 437 | collider.position.first = -collider.size.first/2; |
466 | Game::getInstance().loadMap(map->getRightMap()); | 438 | game.loadMap(*rightMap); |
467 | } else if (it->type == 3) | 439 | } else if (collision.type == 3) |
468 | { | 440 | { |
469 | physics.position.first = it->axis - physics.size.first; | 441 | collider.position.first = collision.axis - collider.size.first; |
470 | 442 | ||
471 | message_t msg; | 443 | Message msg(Message::Type::walkLeft); |
472 | msg.type = CM_WALK_LEFT; | 444 | collider.send(game, msg); |
473 | player.send(msg); | ||
474 | } | 445 | } |
475 | 446 | ||
476 | break; | 447 | break; |
@@ -478,26 +449,28 @@ void MapCollisionComponent::detectCollision(Entity& player, Locatable& physics, | |||
478 | } | 449 | } |
479 | } | 450 | } |
480 | 451 | ||
481 | fixed_x = (int) physics.position.first; | 452 | fixed_x = (int) collider.position.first; |
482 | fixed_y = (int) physics.position.second; | 453 | fixed_y = (int) collider.position.second; |
483 | 454 | ||
484 | if (fixed_y < fixed_oy) | 455 | if (fixed_y < fixed_oy) |
485 | { | 456 | { |
486 | for (auto it=up_collisions.begin(); it!=up_collisions.end(); it++) | 457 | for (auto collision : up_collisions) |
487 | { | 458 | { |
488 | if (it->axis > fixed_oy) continue; | 459 | if (collision.axis > fixed_oy) continue; |
489 | if (it->axis < fixed_y) break; | 460 | if (collision.axis < fixed_y) break; |
490 | 461 | ||
491 | if ((fixed_x+physics.size.first > it->lower) && (fixed_x < it->upper)) | 462 | if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) |
492 | { | 463 | { |
493 | // We have a collision! | 464 | // We have a collision! |
494 | if (it->type == 0) | 465 | if (collision.type == 0) |
495 | { | 466 | { |
496 | physics.position.second = it->axis; | 467 | collider.position.second = collision.axis; |
497 | physics.velocity.second = 0; | 468 | |
498 | } else if (it->type == 1) | 469 | Message msg(Message::Type::stopMovingVertically); |
470 | collider.send(game, msg); | ||
471 | } else if (collision.type == 1) | ||
499 | { | 472 | { |
500 | physics.position.second = GAME_HEIGHT-physics.size.second/2-1; | 473 | collider.position.second = GAME_HEIGHT-collider.size.second/2-1; |
501 | } | 474 | } |
502 | 475 | ||
503 | break; | 476 | break; |
@@ -505,29 +478,29 @@ void MapCollisionComponent::detectCollision(Entity& player, Locatable& physics, | |||
505 | } | 478 | } |
506 | } else if (fixed_y > fixed_oy) | 479 | } else if (fixed_y > fixed_oy) |
507 | { | 480 | { |
508 | for (auto it=down_collisions.begin(); it!=down_collisions.end(); it++) | 481 | for (auto collision : down_collisions) |
509 | { | 482 | { |
510 | if (it->axis < fixed_oy+physics.size.second) continue; | 483 | if (collision.axis < fixed_oy+collider.size.second) continue; |
511 | if (it->axis > fixed_y+physics.size.second) break; | 484 | if (collision.axis > fixed_y+collider.size.second) break; |
512 | 485 | ||
513 | if ((fixed_x+physics.size.first > it->lower) && (fixed_x < it->upper)) | 486 | if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) |
514 | { | 487 | { |
515 | // We have a collision! | 488 | // We have a collision! |
516 | if (it->type == 0) | 489 | if (collision.type == 0) |
517 | { | 490 | { |
518 | physics.position.second = it->axis - physics.size.second; | 491 | collider.position.second = collision.axis - collider.size.second; |
519 | physics.velocity.second = 0; | 492 | |
520 | //mob->onGround = true; | 493 | Message msg(Message::Type::stopMovingVertically); |
521 | } else if (it->type == 1) | 494 | collider.send(game, msg); |
495 | } else if (collision.type == 1) | ||
522 | { | 496 | { |
523 | physics.position.second = -physics.size.second/2; | 497 | collider.position.second = -collider.size.second/2; |
524 | } else if (it->type == 3) | 498 | } else if (collision.type == 3) |
525 | { | 499 | { |
526 | message_t msg; | 500 | Message msg(Message::Type::drop); |
527 | msg.type = CM_DROP; | 501 | msg.dropAxis = collision.axis; |
528 | msg.dropAxis = it->axis; | 502 | |
529 | 503 | collider.send(game, msg); | |
530 | player.send(msg); | ||
531 | } | 504 | } |
532 | 505 | ||
533 | break; | 506 | break; |