diff options
Diffstat (limited to 'src/components.cpp')
-rw-r--r-- | src/components.cpp | 68 |
1 files changed, 50 insertions, 18 deletions
diff --git a/src/components.cpp b/src/components.cpp index 11350ea..e2eb2ed 100644 --- a/src/components.cpp +++ b/src/components.cpp | |||
@@ -125,25 +125,42 @@ void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& c | |||
125 | 125 | ||
126 | void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) | 126 | void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) |
127 | { | 127 | { |
128 | animFrame++; | ||
129 | |||
128 | int frame = 0; | 130 | int frame = 0; |
129 | if (isMoving) | 131 | if (isMoving) |
130 | { | 132 | { |
131 | frame += 2; | 133 | frame += 2; |
132 | 134 | ||
133 | if (animFrame < 10) | 135 | if (animFrame % 20 < 10) |
134 | { | 136 | { |
135 | frame += 2; | 137 | frame += 2; |
136 | } | 138 | } |
137 | } | 139 | } |
138 | if (facingLeft) frame++; | 140 | |
141 | if (facingLeft) | ||
142 | { | ||
143 | frame++; | ||
144 | } | ||
145 | |||
146 | double alpha = 1.0; | ||
147 | if (dying && (animFrame % 4 < 2)) | ||
148 | { | ||
149 | alpha = 0.0; | ||
150 | } | ||
139 | 151 | ||
140 | Rectangle src_rect {frame*10, 0, 10, 12}; | 152 | Rectangle src_rect {frame*10, 0, 10, 12}; |
141 | Rectangle dst_rect {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second}; | 153 | Rectangle dst_rect {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second}; |
142 | buffer.blit(sprite, src_rect, dst_rect); | 154 | buffer.blit(sprite, src_rect, dst_rect, alpha); |
143 | } | 155 | } |
144 | 156 | ||
145 | void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) | 157 | void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) |
146 | { | 158 | { |
159 | if (dying) | ||
160 | { | ||
161 | return; | ||
162 | } | ||
163 | |||
147 | if (msg.type == Message::Type::walkLeft) | 164 | if (msg.type == Message::Type::walkLeft) |
148 | { | 165 | { |
149 | facingLeft = true; | 166 | facingLeft = true; |
@@ -155,15 +172,13 @@ void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) | |||
155 | } else if (msg.type == Message::Type::stopWalking) | 172 | } else if (msg.type == Message::Type::stopWalking) |
156 | { | 173 | { |
157 | isMoving = false; | 174 | isMoving = false; |
175 | } else if (msg.type == Message::Type::die) | ||
176 | { | ||
177 | dying = true; | ||
178 | isMoving = false; | ||
158 | } | 179 | } |
159 | } | 180 | } |
160 | 181 | ||
161 | void PlayerSpriteComponent::tick(Game&, Entity&) | ||
162 | { | ||
163 | animFrame++; | ||
164 | animFrame %= 20; | ||
165 | } | ||
166 | |||
167 | // Player physics | 182 | // Player physics |
168 | 183 | ||
169 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) | 184 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) |
@@ -220,11 +235,20 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) | |||
220 | entity.position.second = msg.dropAxis - entity.size.second; | 235 | entity.position.second = msg.dropAxis - entity.size.second; |
221 | velocity.second = 0; | 236 | velocity.second = 0; |
222 | } | 237 | } |
238 | } else if (msg.type == Message::Type::die) | ||
239 | { | ||
240 | frozen = true; | ||
223 | } | 241 | } |
224 | } | 242 | } |
225 | 243 | ||
226 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity) | 244 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity) |
227 | { | 245 | { |
246 | // If frozen, do nothing | ||
247 | if (frozen) | ||
248 | { | ||
249 | return; | ||
250 | } | ||
251 | |||
228 | // Continue walking even if blocked earlier | 252 | // Continue walking even if blocked earlier |
229 | if (velocity.first == 0) | 253 | if (velocity.first == 0) |
230 | { | 254 | { |
@@ -302,13 +326,10 @@ void MapRenderComponent::render(Game&, Entity&, Texture& buffer) | |||
302 | 326 | ||
303 | // Map collision | 327 | // Map collision |
304 | 328 | ||
305 | MapCollisionComponent::MapCollisionComponent(const Map& map) | 329 | MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) |
306 | { | 330 | { |
307 | leftMap = map.getLeftMap(); | 331 | addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? 1 : 2); |
308 | rightMap = map.getRightMap(); | 332 | addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? 3 : 2); |
309 | |||
310 | addCollision(-6, 0, GAME_WIDTH, Direction::left, (leftMap == nullptr) ? 1 : 2); | ||
311 | addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (rightMap == nullptr) ? 3 : 2); | ||
312 | 333 | ||
313 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | 334 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) |
314 | { | 335 | { |
@@ -316,7 +337,7 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) | |||
316 | int y = i / MAP_WIDTH; | 337 | int y = i / MAP_WIDTH; |
317 | int tile = map.mapdata()[i]; | 338 | int tile = map.mapdata()[i]; |
318 | 339 | ||
319 | if ((tile > 0) && (!((tile >= 5) && (tile <= 7)))) | 340 | if ((tile > 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7)))) |
320 | { | 341 | { |
321 | addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, 0); | 342 | addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, 0); |
322 | addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, 0); | 343 | addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, 0); |
@@ -325,6 +346,9 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) | |||
325 | } else if ((tile >= 5) && (tile <= 7)) | 346 | } else if ((tile >= 5) && (tile <= 7)) |
326 | { | 347 | { |
327 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); | 348 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); |
349 | } else if (tile == 42) | ||
350 | { | ||
351 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 5); | ||
328 | } | 352 | } |
329 | } | 353 | } |
330 | } | 354 | } |
@@ -518,11 +542,11 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli | |||
518 | if (dir == Direction::left) | 542 | if (dir == Direction::left) |
519 | { | 543 | { |
520 | collider.position.first = GAME_WIDTH-collider.size.first/2; | 544 | collider.position.first = GAME_WIDTH-collider.size.first/2; |
521 | game.loadMap(*leftMap); | 545 | game.loadMap(*(map.getLeftMap())); |
522 | } else if (dir == Direction::right) | 546 | } else if (dir == Direction::right) |
523 | { | 547 | { |
524 | collider.position.first = -collider.size.first/2; | 548 | collider.position.first = -collider.size.first/2; |
525 | game.loadMap(*rightMap); | 549 | game.loadMap(*(map.getRightMap())); |
526 | } | 550 | } |
527 | 551 | ||
528 | return true; | 552 | return true; |
@@ -541,6 +565,14 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli | |||
541 | msg.dropAxis = collision.axis; | 565 | msg.dropAxis = collision.axis; |
542 | 566 | ||
543 | collider.send(game, msg); | 567 | collider.send(game, msg); |
568 | } else if (collision.type == 5) | ||
569 | { | ||
570 | Message msg(Message::Type::die); | ||
571 | collider.send(game, msg); | ||
572 | |||
573 | game.schedule(FRAMES_PER_SECOND * 0.75, [&] () { | ||
574 | game.loadGame(map); | ||
575 | }); | ||
544 | } | 576 | } |
545 | 577 | ||
546 | return false; | 578 | return false; |