diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-10 19:42:04 -0400 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-10 19:42:04 -0400 |
| commit | 0e0389752a0912614737e5c059b5cd4719ef9cf2 (patch) | |
| tree | fc39e676bd819ee973f27fc40150a7874d2f8503 /src | |
| parent | 7f0e8c7ef70c62814c274f110367db92f01cbb26 (diff) | |
| download | therapy-0e0389752a0912614737e5c059b5cd4719ef9cf2.tar.gz therapy-0e0389752a0912614737e5c059b5cd4719ef9cf2.tar.bz2 therapy-0e0389752a0912614737e5c059b5cd4719ef9cf2.zip | |
Const correctness!
Also created savefile and refactored collisions a bit.
Diffstat (limited to 'src')
| -rw-r--r-- | src/components.cpp | 173 | ||||
| -rw-r--r-- | src/components.h | 33 | ||||
| -rw-r--r-- | src/entity.cpp | 2 | ||||
| -rw-r--r-- | src/entity.h | 4 | ||||
| -rw-r--r-- | src/game.cpp | 19 | ||||
| -rw-r--r-- | src/game.h | 10 | ||||
| -rw-r--r-- | src/map.cpp | 12 | ||||
| -rw-r--r-- | src/map.h | 16 |
8 files changed, 156 insertions, 113 deletions
| diff --git a/src/components.cpp b/src/components.cpp index 369bb9e..4d62a87 100644 --- a/src/components.cpp +++ b/src/components.cpp | |||
| @@ -68,7 +68,7 @@ void UserMovementComponent::input(Game& game, Entity& entity, int key, int actio | |||
| 68 | 68 | ||
| 69 | // Physics component | 69 | // Physics component |
| 70 | 70 | ||
| 71 | void PhysicsBodyComponent::receive(Game&, Entity&, Message& msg) | 71 | void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) |
| 72 | { | 72 | { |
| 73 | if (msg.type == Message::Type::walkLeft) | 73 | if (msg.type == Message::Type::walkLeft) |
| 74 | { | 74 | { |
| @@ -142,7 +142,7 @@ void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) | |||
| 142 | buffer.blit(sprite, src_rect, dst_rect); | 142 | buffer.blit(sprite, src_rect, dst_rect); |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | void PlayerSpriteComponent::receive(Game&, Entity&, Message& msg) | 145 | void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) |
| 146 | { | 146 | { |
| 147 | if (msg.type == Message::Type::walkLeft) | 147 | if (msg.type == Message::Type::walkLeft) |
| 148 | { | 148 | { |
| @@ -178,7 +178,7 @@ PlayerPhysicsComponent::PlayerPhysicsComponent() | |||
| 178 | accel.second = jump_gravity_short; | 178 | accel.second = jump_gravity_short; |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | void PlayerPhysicsComponent::receive(Game&, Entity& entity, Message& msg) | 181 | void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) |
| 182 | { | 182 | { |
| 183 | if (msg.type == Message::Type::walkLeft) | 183 | if (msg.type == Message::Type::walkLeft) |
| 184 | { | 184 | { |
| @@ -264,7 +264,7 @@ void PlayerPhysicsComponent::tick(Game& game, Entity& entity) | |||
| 264 | 264 | ||
| 265 | // Map rendering | 265 | // Map rendering |
| 266 | 266 | ||
| 267 | MapRenderComponent::MapRenderComponent(Map& map) | 267 | MapRenderComponent::MapRenderComponent(const Map& map) |
| 268 | { | 268 | { |
| 269 | screen.fill(screen.entirety(), 0, 0, 0); | 269 | screen.fill(screen.entirety(), 0, 0, 0); |
| 270 | 270 | ||
| @@ -302,13 +302,13 @@ void MapRenderComponent::render(Game&, Entity&, Texture& buffer) | |||
| 302 | 302 | ||
| 303 | // Map collision | 303 | // Map collision |
| 304 | 304 | ||
| 305 | MapCollisionComponent::MapCollisionComponent(Map& map) | 305 | MapCollisionComponent::MapCollisionComponent(const Map& map) |
| 306 | { | 306 | { |
| 307 | leftMap = map.getLeftMap(); | 307 | leftMap = map.getLeftMap(); |
| 308 | rightMap = map.getRightMap(); | 308 | rightMap = map.getRightMap(); |
| 309 | 309 | ||
| 310 | add_collision(-6, 0, GAME_WIDTH, left, (map.getLeftMap() == nullptr) ? 1 : 2); | 310 | addCollision(-6, 0, GAME_WIDTH, Direction::left, (leftMap == nullptr) ? 1 : 2); |
| 311 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (map.getRightMap() == nullptr) ? 3 : 2); | 311 | addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (rightMap == nullptr) ? 3 : 2); |
| 312 | 312 | ||
| 313 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | 313 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) |
| 314 | { | 314 | { |
| @@ -318,24 +318,24 @@ MapCollisionComponent::MapCollisionComponent(Map& map) | |||
| 318 | 318 | ||
| 319 | if ((tile > 0) && (!((tile >= 5) && (tile <= 7)))) | 319 | if ((tile > 0) && (!((tile >= 5) && (tile <= 7)))) |
| 320 | { | 320 | { |
| 321 | add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0); | 321 | addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, 0); |
| 322 | add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0); | 322 | addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, 0); |
| 323 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | 323 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 0); |
| 324 | add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0); | 324 | addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, 0); |
| 325 | } else if ((tile >= 5) && (tile <= 7)) | 325 | } else if ((tile >= 5) && (tile <= 7)) |
| 326 | { | 326 | { |
| 327 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 3); | 327 | addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); |
| 328 | } | 328 | } |
| 329 | } | 329 | } |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | void MapCollisionComponent::add_collision(int axis, int lower, int upper, direction_t dir, int type) | 332 | void MapCollisionComponent::addCollision(int axis, int lower, int upper, Direction dir, int type) |
| 333 | { | 333 | { |
| 334 | std::list<collision_t>::iterator it; | 334 | std::list<Collision>::iterator it; |
| 335 | 335 | ||
| 336 | switch (dir) | 336 | switch (dir) |
| 337 | { | 337 | { |
| 338 | case up: | 338 | case Direction::up: |
| 339 | it = up_collisions.begin(); | 339 | it = up_collisions.begin(); |
| 340 | for (; it!=up_collisions.end(); it++) | 340 | for (; it!=up_collisions.end(); it++) |
| 341 | { | 341 | { |
| @@ -345,7 +345,7 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct | |||
| 345 | up_collisions.insert(it, {axis, lower, upper, type}); | 345 | up_collisions.insert(it, {axis, lower, upper, type}); |
| 346 | 346 | ||
| 347 | break; | 347 | break; |
| 348 | case down: | 348 | case Direction::down: |
| 349 | it = down_collisions.begin(); | 349 | it = down_collisions.begin(); |
| 350 | for (; it!=down_collisions.end(); it++) | 350 | for (; it!=down_collisions.end(); it++) |
| 351 | { | 351 | { |
| @@ -355,7 +355,7 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct | |||
| 355 | down_collisions.insert(it, {axis, lower, upper, type}); | 355 | down_collisions.insert(it, {axis, lower, upper, type}); |
| 356 | 356 | ||
| 357 | break; | 357 | break; |
| 358 | case left: | 358 | case Direction::left: |
| 359 | it = left_collisions.begin(); | 359 | it = left_collisions.begin(); |
| 360 | for (; it!=left_collisions.end(); it++) | 360 | for (; it!=left_collisions.end(); it++) |
| 361 | { | 361 | { |
| @@ -365,7 +365,7 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct | |||
| 365 | left_collisions.insert(it, {axis, lower, upper, type}); | 365 | left_collisions.insert(it, {axis, lower, upper, type}); |
| 366 | 366 | ||
| 367 | break; | 367 | break; |
| 368 | case right: | 368 | case Direction::right: |
| 369 | it = right_collisions.begin(); | 369 | it = right_collisions.begin(); |
| 370 | for (; it!=right_collisions.end(); it++) | 370 | for (; it!=right_collisions.end(); it++) |
| 371 | { | 371 | { |
| @@ -395,20 +395,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide | |||
| 395 | if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) | 395 | if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) |
| 396 | { | 396 | { |
| 397 | // We have a collision! | 397 | // We have a collision! |
| 398 | if (collision.type == 0) | 398 | processCollision(game, collider, collision, Direction::left); |
| 399 | { | ||
| 400 | collider.position.first = collision.axis; | ||
| 401 | |||
| 402 | Message msg(Message::Type::stopMovingHorizontally); | ||
| 403 | collider.send(game, msg); | ||
| 404 | } else if (collision.type == 1) | ||
| 405 | { | ||
| 406 | collider.position.first = GAME_WIDTH-collider.size.first/2; | ||
| 407 | } else if (collision.type == 2) | ||
| 408 | { | ||
| 409 | collider.position.first = GAME_WIDTH-collider.size.first/2; | ||
| 410 | game.loadMap(*leftMap); | ||
| 411 | } | ||
| 412 | 399 | ||
| 413 | break; | 400 | break; |
| 414 | } | 401 | } |
| @@ -423,26 +410,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide | |||
| 423 | if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) | 410 | if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) |
| 424 | { | 411 | { |
| 425 | // We have a collision! | 412 | // We have a collision! |
| 426 | if (collision.type == 0) | 413 | processCollision(game, collider, collision, Direction::right); |
| 427 | { | ||
| 428 | collider.position.first = collision.axis - collider.size.first; | ||
| 429 | |||
| 430 | Message msg(Message::Type::stopMovingHorizontally); | ||
| 431 | collider.send(game, msg); | ||
| 432 | } else if (collision.type == 1) | ||
| 433 | { | ||
| 434 | collider.position.first = -collider.size.first/2; | ||
| 435 | } else if (collision.type == 2) | ||
| 436 | { | ||
| 437 | collider.position.first = -collider.size.first/2; | ||
| 438 | game.loadMap(*rightMap); | ||
| 439 | } else if (collision.type == 3) | ||
| 440 | { | ||
| 441 | collider.position.first = collision.axis - collider.size.first; | ||
| 442 | |||
| 443 | Message msg(Message::Type::walkLeft); | ||
| 444 | collider.send(game, msg); | ||
| 445 | } | ||
| 446 | 414 | ||
| 447 | break; | 415 | break; |
| 448 | } | 416 | } |
| @@ -462,16 +430,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide | |||
| 462 | if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) | 430 | if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) |
| 463 | { | 431 | { |
| 464 | // We have a collision! | 432 | // We have a collision! |
| 465 | if (collision.type == 0) | 433 | processCollision(game, collider, collision, Direction::up); |
| 466 | { | ||
| 467 | collider.position.second = collision.axis; | ||
| 468 | |||
| 469 | Message msg(Message::Type::stopMovingVertically); | ||
| 470 | collider.send(game, msg); | ||
| 471 | } else if (collision.type == 1) | ||
| 472 | { | ||
| 473 | collider.position.second = GAME_HEIGHT-collider.size.second/2-1; | ||
| 474 | } | ||
| 475 | 434 | ||
| 476 | break; | 435 | break; |
| 477 | } | 436 | } |
| @@ -486,25 +445,83 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide | |||
| 486 | if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) | 445 | if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) |
| 487 | { | 446 | { |
| 488 | // We have a collision! | 447 | // We have a collision! |
| 489 | if (collision.type == 0) | 448 | processCollision(game, collider, collision, Direction::down); |
| 490 | { | ||
| 491 | collider.position.second = collision.axis - collider.size.second; | ||
| 492 | |||
| 493 | Message msg(Message::Type::stopMovingVertically); | ||
| 494 | collider.send(game, msg); | ||
| 495 | } else if (collision.type == 1) | ||
| 496 | { | ||
| 497 | collider.position.second = -collider.size.second/2; | ||
| 498 | } else if (collision.type == 3) | ||
| 499 | { | ||
| 500 | Message msg(Message::Type::drop); | ||
| 501 | msg.dropAxis = collision.axis; | ||
| 502 | |||
| 503 | collider.send(game, msg); | ||
| 504 | } | ||
| 505 | 449 | ||
| 506 | break; | 450 | break; |
| 507 | } | 451 | } |
| 508 | } | 452 | } |
| 509 | } | 453 | } |
| 510 | } | 454 | } |
| 455 | |||
| 456 | void MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir) | ||
| 457 | { | ||
| 458 | if (collision.type == 0) | ||
| 459 | { | ||
| 460 | if (dir == Direction::left) | ||
| 461 | { | ||
| 462 | collider.position.first = collision.axis; | ||
| 463 | |||
| 464 | Message msg(Message::Type::stopMovingHorizontally); | ||
| 465 | collider.send(game, msg); | ||
| 466 | } else if (dir == Direction::right) | ||
| 467 | { | ||
| 468 | collider.position.first = collision.axis - collider.size.first; | ||
| 469 | |||
| 470 | Message msg(Message::Type::stopMovingHorizontally); | ||
| 471 | collider.send(game, msg); | ||
| 472 | } else if (dir == Direction::up) | ||
| 473 | { | ||
| 474 | collider.position.second = collision.axis; | ||
| 475 | |||
| 476 | Message msg(Message::Type::stopMovingVertically); | ||
| 477 | collider.send(game, msg); | ||
| 478 | } else if (dir == Direction::down) | ||
| 479 | { | ||
| 480 | collider.position.second = collision.axis - collider.size.second; | ||
| 481 | |||
| 482 | Message msg(Message::Type::stopMovingVertically); | ||
| 483 | collider.send(game, msg); | ||
| 484 | } | ||
| 485 | } else if (collision.type == 1) | ||
| 486 | { | ||
| 487 | if (dir == Direction::left) | ||
| 488 | { | ||
| 489 | collider.position.first = GAME_WIDTH-collider.size.first/2; | ||
| 490 | } else if (dir == Direction::right) | ||
| 491 | { | ||
| 492 | collider.position.first = -collider.size.first/2; | ||
| 493 | } else if (dir == Direction::up) | ||
| 494 | { | ||
| 495 | collider.position.second = GAME_HEIGHT-collider.size.second/2-1; | ||
| 496 | } else if (dir == Direction::down) | ||
| 497 | { | ||
| 498 | collider.position.second = -collider.size.second/2; | ||
| 499 | } | ||
| 500 | } else if (collision.type == 2) | ||
| 501 | { | ||
| 502 | if (dir == Direction::left) | ||
| 503 | { | ||
| 504 | collider.position.first = GAME_WIDTH-collider.size.first/2; | ||
| 505 | game.loadMap(*leftMap); | ||
| 506 | } else if (dir == Direction::right) | ||
| 507 | { | ||
| 508 | collider.position.first = -collider.size.first/2; | ||
| 509 | game.loadMap(*rightMap); | ||
| 510 | } | ||
| 511 | } else if (collision.type == 3) | ||
| 512 | { | ||
| 513 | if (dir == Direction::right) | ||
| 514 | { | ||
| 515 | collider.position.first = collision.axis - collider.size.first; | ||
| 516 | |||
| 517 | Message msg(Message::Type::walkLeft); | ||
| 518 | collider.send(game, msg); | ||
| 519 | } | ||
| 520 | } else if (collision.type == 4) | ||
| 521 | { | ||
| 522 | Message msg(Message::Type::drop); | ||
| 523 | msg.dropAxis = collision.axis; | ||
| 524 | |||
| 525 | collider.send(game, msg); | ||
| 526 | } | ||
| 527 | } | ||
| diff --git a/src/components.h b/src/components.h index f9b6e1e..985025c 100644 --- a/src/components.h +++ b/src/components.h | |||
| @@ -18,11 +18,11 @@ class UserMovementComponent : public Component { | |||
| 18 | 18 | ||
| 19 | class PhysicsBodyComponent : public Component { | 19 | class PhysicsBodyComponent : public Component { |
| 20 | public: | 20 | public: |
| 21 | void receive(Game& game, Entity& entity, Message& msg); | 21 | void receive(Game& game, Entity& entity, const Message& msg); |
| 22 | void tick(Game& game, Entity& entity); | 22 | void tick(Game& game, Entity& entity); |
| 23 | void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position); | 23 | void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position); |
| 24 | 24 | ||
| 25 | private: | 25 | private: |
| 26 | std::pair<double, double> velocity; | 26 | std::pair<double, double> velocity; |
| 27 | std::pair<double, double> accel; | 27 | std::pair<double, double> accel; |
| 28 | }; | 28 | }; |
| @@ -30,7 +30,7 @@ class PhysicsBodyComponent : public Component { | |||
| 30 | class PlayerSpriteComponent : public Component { | 30 | class PlayerSpriteComponent : public Component { |
| 31 | public: | 31 | public: |
| 32 | void render(Game& game, Entity& entity, Texture& buffer); | 32 | void render(Game& game, Entity& entity, Texture& buffer); |
| 33 | void receive(Game& game, Entity& entity, Message& msg); | 33 | void receive(Game& game, Entity& entity, const Message& msg); |
| 34 | void tick(Game& game, Entity& entity); | 34 | void tick(Game& game, Entity& entity); |
| 35 | 35 | ||
| 36 | private: | 36 | private: |
| @@ -44,7 +44,7 @@ class PlayerPhysicsComponent : public Component { | |||
| 44 | public: | 44 | public: |
| 45 | PlayerPhysicsComponent(); | 45 | PlayerPhysicsComponent(); |
| 46 | void tick(Game& game, Entity& entity); | 46 | void tick(Game& game, Entity& entity); |
| 47 | void receive(Game& game, Entity& entity, Message& msg); | 47 | void receive(Game& game, Entity& entity, const Message& msg); |
| 48 | 48 | ||
| 49 | private: | 49 | private: |
| 50 | std::pair<double, double> velocity; | 50 | std::pair<double, double> velocity; |
| @@ -58,7 +58,7 @@ class PlayerPhysicsComponent : public Component { | |||
| 58 | 58 | ||
| 59 | class MapRenderComponent : public Component { | 59 | class MapRenderComponent : public Component { |
| 60 | public: | 60 | public: |
| 61 | MapRenderComponent(Map& map); | 61 | MapRenderComponent(const Map& map); |
| 62 | void render(Game& game, Entity& entity, Texture& buffer); | 62 | void render(Game& game, Entity& entity, Texture& buffer); |
| 63 | 63 | ||
| 64 | private: | 64 | private: |
| @@ -67,29 +67,30 @@ class MapRenderComponent : public Component { | |||
| 67 | 67 | ||
| 68 | class MapCollisionComponent : public Component { | 68 | class MapCollisionComponent : public Component { |
| 69 | public: | 69 | public: |
| 70 | MapCollisionComponent(Map& map); | 70 | MapCollisionComponent(const Map& map); |
| 71 | void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position); | 71 | void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position); |
| 72 | 72 | ||
| 73 | private: | 73 | private: |
| 74 | enum direction_t { | 74 | enum class Direction { |
| 75 | up, left, down, right | 75 | up, left, down, right |
| 76 | }; | 76 | }; |
| 77 | 77 | ||
| 78 | typedef struct { | 78 | struct Collision { |
| 79 | int axis; | 79 | int axis; |
| 80 | int lower; | 80 | int lower; |
| 81 | int upper; | 81 | int upper; |
| 82 | int type; | 82 | int type; |
| 83 | } collision_t; | 83 | }; |
| 84 | 84 | ||
| 85 | void add_collision(int axis, int lower, int upper, direction_t dir, int type); | 85 | void addCollision(int axis, int lower, int upper, Direction dir, int type); |
| 86 | void processCollision(Game& game, Entity& collider, Collision collision, Direction dir); | ||
| 86 | 87 | ||
| 87 | std::list<collision_t> left_collisions; | 88 | std::list<Collision> left_collisions; |
| 88 | std::list<collision_t> right_collisions; | 89 | std::list<Collision> right_collisions; |
| 89 | std::list<collision_t> up_collisions; | 90 | std::list<Collision> up_collisions; |
| 90 | std::list<collision_t> down_collisions; | 91 | std::list<Collision> down_collisions; |
| 91 | Map* leftMap; | 92 | const Map* leftMap; |
| 92 | Map* rightMap; | 93 | const Map* rightMap; |
| 93 | }; | 94 | }; |
| 94 | 95 | ||
| 95 | #endif | 96 | #endif |
| diff --git a/src/entity.cpp b/src/entity.cpp index 950969e..38ddffe 100644 --- a/src/entity.cpp +++ b/src/entity.cpp | |||
| @@ -5,7 +5,7 @@ void Entity::addComponent(std::shared_ptr<Component> c) | |||
| 5 | components.push_back(c); | 5 | components.push_back(c); |
| 6 | } | 6 | } |
| 7 | 7 | ||
| 8 | void Entity::send(Game& game, Message& msg) | 8 | void Entity::send(Game& game, const Message& msg) |
| 9 | { | 9 | { |
| 10 | for (auto component : components) | 10 | for (auto component : components) |
| 11 | { | 11 | { |
| diff --git a/src/entity.h b/src/entity.h index 803a9b8..266fe11 100644 --- a/src/entity.h +++ b/src/entity.h | |||
| @@ -34,7 +34,7 @@ class Message { | |||
| 34 | class Entity { | 34 | class Entity { |
| 35 | public: | 35 | public: |
| 36 | void addComponent(std::shared_ptr<Component> c); | 36 | void addComponent(std::shared_ptr<Component> c); |
| 37 | void send(Game& game, Message& msg); | 37 | void send(Game& game, const Message& msg); |
| 38 | void tick(Game& game); | 38 | void tick(Game& game); |
| 39 | void input(Game& game, int key, int action); | 39 | void input(Game& game, int key, int action); |
| 40 | void render(Game& game, Texture& buffer); | 40 | void render(Game& game, Texture& buffer); |
| @@ -49,7 +49,7 @@ class Entity { | |||
| 49 | 49 | ||
| 50 | class Component { | 50 | class Component { |
| 51 | public: | 51 | public: |
| 52 | virtual void receive(Game&, Entity&, Message&) {} | 52 | virtual void receive(Game&, Entity&, const Message&) {} |
| 53 | virtual void render(Game&, Entity&, Texture&) {} | 53 | virtual void render(Game&, Entity&, Texture&) {} |
| 54 | virtual void tick(Game&, Entity&) {} | 54 | virtual void tick(Game&, Entity&) {} |
| 55 | virtual void input(Game&, Entity&, int, int) {} | 55 | virtual void input(Game&, Entity&, int, int) {} |
| diff --git a/src/game.cpp b/src/game.cpp index cbbae06..e392923 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
| @@ -20,6 +20,8 @@ Game::Game() | |||
| 20 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); | 20 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); |
| 21 | player->addComponent(player_anim); | 21 | player->addComponent(player_anim); |
| 22 | 22 | ||
| 23 | save = {&m, player->position}; | ||
| 24 | |||
| 23 | loadMap(m); | 25 | loadMap(m); |
| 24 | } | 26 | } |
| 25 | 27 | ||
| @@ -87,7 +89,7 @@ void Game::execute(GLFWwindow* window) | |||
| 87 | } | 89 | } |
| 88 | } | 90 | } |
| 89 | 91 | ||
| 90 | void Game::loadMap(Map& map) | 92 | void Game::loadMap(const Map& map) |
| 91 | { | 93 | { |
| 92 | auto mapEn = std::make_shared<Entity>(); | 94 | auto mapEn = std::make_shared<Entity>(); |
| 93 | 95 | ||
| @@ -111,3 +113,18 @@ void Game::detectCollision(Entity& collider, std::pair<double, double> old_posit | |||
| 111 | entity->detectCollision(*this, collider, old_position); | 113 | entity->detectCollision(*this, collider, old_position); |
| 112 | } | 114 | } |
| 113 | } | 115 | } |
| 116 | |||
| 117 | void Game::saveGame(const Map& map, std::pair<double, double> position) | ||
| 118 | { | ||
| 119 | save = {&map, position}; | ||
| 120 | } | ||
| 121 | |||
| 122 | void Game::loadGame(const Map& curMap) | ||
| 123 | { | ||
| 124 | if (&curMap != save.map) | ||
| 125 | { | ||
| 126 | loadMap(*(save.map)); | ||
| 127 | } | ||
| 128 | |||
| 129 | player->position = save.position; | ||
| 130 | } | ||
| diff --git a/src/game.h b/src/game.h index c419c5d..69b8df7 100644 --- a/src/game.h +++ b/src/game.h | |||
| @@ -17,12 +17,19 @@ const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT; | |||
| 17 | const int FRAMES_PER_SECOND = 60; | 17 | const int FRAMES_PER_SECOND = 60; |
| 18 | const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; | 18 | const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; |
| 19 | 19 | ||
| 20 | struct Savefile { | ||
| 21 | const Map* map; | ||
| 22 | std::pair<double, double> position; | ||
| 23 | }; | ||
| 24 | |||
| 20 | class Game { | 25 | class Game { |
| 21 | public: | 26 | public: |
| 22 | Game(); | 27 | Game(); |
| 23 | void execute(GLFWwindow* window); | 28 | void execute(GLFWwindow* window); |
| 24 | void loadMap(Map& map); | 29 | void loadMap(const Map& map); |
| 25 | void detectCollision(Entity& collider, std::pair<double, double> old_position); | 30 | void detectCollision(Entity& collider, std::pair<double, double> old_position); |
| 31 | void saveGame(const Map& map, std::pair<double, double> position); | ||
| 32 | void loadGame(const Map& curMap); | ||
| 26 | 33 | ||
| 27 | bool shouldQuit = false; | 34 | bool shouldQuit = false; |
| 28 | private: | 35 | private: |
| @@ -34,6 +41,7 @@ class Game { | |||
| 34 | std::shared_ptr<Entity> player; | 41 | std::shared_ptr<Entity> player; |
| 35 | Map m{"../maps/embarass.txt"}; | 42 | Map m{"../maps/embarass.txt"}; |
| 36 | Map m2{"../maps/second.txt"}; | 43 | Map m2{"../maps/second.txt"}; |
| 44 | Savefile save; | ||
| 37 | }; | 45 | }; |
| 38 | 46 | ||
| 39 | #endif | 47 | #endif |
| diff --git a/src/map.cpp b/src/map.cpp index 87080e8..3976b63 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
| @@ -67,32 +67,32 @@ void swap(Map& first, Map& second) | |||
| 67 | std::swap(first.m_rightMap, second.m_rightMap); | 67 | std::swap(first.m_rightMap, second.m_rightMap); |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | const int* Map::mapdata() | 70 | const int* Map::mapdata() const |
| 71 | { | 71 | { |
| 72 | return m_mapdata; | 72 | return m_mapdata; |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | const char* Map::title() | 75 | const char* Map::title() const |
| 76 | { | 76 | { |
| 77 | return m_title; | 77 | return m_title; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | Map* Map::getLeftMap() | 80 | const Map* Map::getLeftMap() const |
| 81 | { | 81 | { |
| 82 | return m_leftMap; | 82 | return m_leftMap; |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | Map* Map::getRightMap() | 85 | const Map* Map::getRightMap() const |
| 86 | { | 86 | { |
| 87 | return m_rightMap; | 87 | return m_rightMap; |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | void Map::setLeftMap(Map* m) | 90 | void Map::setLeftMap(const Map* m) |
| 91 | { | 91 | { |
| 92 | m_leftMap = m; | 92 | m_leftMap = m; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | void Map::setRightMap(Map* m) | 95 | void Map::setRightMap(const Map* m) |
| 96 | { | 96 | { |
| 97 | m_rightMap = m; | 97 | m_rightMap = m; |
| 98 | } | 98 | } |
| diff --git a/src/map.h b/src/map.h index e3d1802..071b6f2 100644 --- a/src/map.h +++ b/src/map.h | |||
| @@ -10,19 +10,19 @@ class Map { | |||
| 10 | Map& operator= (Map other); | 10 | Map& operator= (Map other); |
| 11 | friend void swap(Map& first, Map& second); | 11 | friend void swap(Map& first, Map& second); |
| 12 | 12 | ||
| 13 | const int* mapdata(); | 13 | const int* mapdata() const; |
| 14 | const char* title(); | 14 | const char* title() const; |
| 15 | Map* getLeftMap(); | 15 | const Map* getLeftMap() const; |
| 16 | Map* getRightMap(); | 16 | const Map* getRightMap() const; |
| 17 | void setLeftMap(Map* m); | 17 | void setLeftMap(const Map* m); |
| 18 | void setRightMap(Map* m); | 18 | void setRightMap(const Map* m); |
| 19 | private: | 19 | private: |
| 20 | Map(); | 20 | Map(); |
| 21 | 21 | ||
| 22 | int* m_mapdata; | 22 | int* m_mapdata; |
| 23 | char* m_title; | 23 | char* m_title; |
| 24 | Map* m_leftMap; | 24 | const Map* m_leftMap = nullptr; |
| 25 | Map* m_rightMap; | 25 | const Map* m_rightMap = nullptr; |
| 26 | }; | 26 | }; |
| 27 | 27 | ||
| 28 | #endif | 28 | #endif |
