summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/components.cpp52
-rw-r--r--src/components.h13
-rw-r--r--src/entity.h3
-rw-r--r--src/game.cpp45
-rw-r--r--src/game.h2
5 files changed, 62 insertions, 53 deletions
diff --git a/src/components.cpp b/src/components.cpp index 5925d14..ad0f501 100644 --- a/src/components.cpp +++ b/src/components.cpp
@@ -157,6 +157,11 @@ void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer)
157 157
158void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) 158void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg)
159{ 159{
160 if (msg.type == Message::Type::stopDying)
161 {
162 dying = false;
163 }
164
160 if (dying) 165 if (dying)
161 { 166 {
162 return; 167 return;
@@ -216,7 +221,10 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg)
216 velocity.second = 0.0; 221 velocity.second = 0.0;
217 } else if (msg.type == Message::Type::jump) 222 } else if (msg.type == Message::Type::jump)
218 { 223 {
219 playSound("../res/Randomize87.wav", 0.25); 224 if (!frozen)
225 {
226 playSound("../res/Randomize87.wav", 0.25);
227 }
220 228
221 velocity.second = jump_velocity; 229 velocity.second = jump_velocity;
222 accel.second = jump_gravity; 230 accel.second = jump_gravity;
@@ -241,6 +249,9 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg)
241 } else if (msg.type == Message::Type::die) 249 } else if (msg.type == Message::Type::die)
242 { 250 {
243 frozen = true; 251 frozen = true;
252 } else if (msg.type == Message::Type::stopDying)
253 {
254 frozen = false;
244 } 255 }
245} 256}
246 257
@@ -331,8 +342,8 @@ void MapRenderComponent::render(Game&, Entity&, Texture& buffer)
331 342
332MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) 343MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map)
333{ 344{
334 addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? 1 : 2); 345 addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport);
335 addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? 3 : 2); 346 addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport);
336 347
337 for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) 348 for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++)
338 { 349 {
@@ -342,21 +353,21 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map)
342 353
343 if ((tile > 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7)))) 354 if ((tile > 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7))))
344 { 355 {
345 addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, 0); 356 addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, Collision::Type::wall);
346 addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, 0); 357 addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, Collision::Type::wall);
347 addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 0); 358 addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::wall);
348 addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, 0); 359 addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, Collision::Type::wall);
349 } else if ((tile >= 5) && (tile <= 7)) 360 } else if ((tile >= 5) && (tile <= 7))
350 { 361 {
351 addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); 362 addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::platform);
352 } else if (tile == 42) 363 } else if (tile == 42)
353 { 364 {
354 addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 5); 365 addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::danger);
355 } 366 }
356 } 367 }
357} 368}
358 369
359void MapCollisionComponent::addCollision(int axis, int lower, int upper, Direction dir, int type) 370void MapCollisionComponent::addCollision(int axis, int lower, int upper, Direction dir, Collision::Type type)
360{ 371{
361 std::list<Collision>::iterator it; 372 std::list<Collision>::iterator it;
362 373
@@ -498,7 +509,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide
498 509
499bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir) 510bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir)
500{ 511{
501 if (collision.type == 0) 512 if (collision.type == Collision::Type::wall)
502 { 513 {
503 if (dir == Direction::left) 514 if (dir == Direction::left)
504 { 515 {
@@ -525,7 +536,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli
525 Message msg(Message::Type::stopMovingVertically); 536 Message msg(Message::Type::stopMovingVertically);
526 collider.send(game, msg); 537 collider.send(game, msg);
527 } 538 }
528 } else if (collision.type == 1) 539 } else if (collision.type == Collision::Type::wrap)
529 { 540 {
530 if (dir == Direction::left) 541 if (dir == Direction::left)
531 { 542 {
@@ -540,7 +551,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli
540 { 551 {
541 collider.position.second = -collider.size.second/2; 552 collider.position.second = -collider.size.second/2;
542 } 553 }
543 } else if (collision.type == 2) 554 } else if (collision.type == Collision::Type::teleport)
544 { 555 {
545 if (dir == Direction::left) 556 if (dir == Direction::left)
546 { 557 {
@@ -553,7 +564,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli
553 } 564 }
554 565
555 return true; 566 return true;
556 } else if (collision.type == 3) 567 } else if (collision.type == Collision::Type::reverse)
557 { 568 {
558 if (dir == Direction::right) 569 if (dir == Direction::right)
559 { 570 {
@@ -562,22 +573,15 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli
562 Message msg(Message::Type::walkLeft); 573 Message msg(Message::Type::walkLeft);
563 collider.send(game, msg); 574 collider.send(game, msg);
564 } 575 }
565 } else if (collision.type == 4) 576 } else if (collision.type == Collision::Type::platform)
566 { 577 {
567 Message msg(Message::Type::drop); 578 Message msg(Message::Type::drop);
568 msg.dropAxis = collision.axis; 579 msg.dropAxis = collision.axis;
569 580
570 collider.send(game, msg); 581 collider.send(game, msg);
571 } else if (collision.type == 5) 582 } else if (collision.type == Collision::Type::danger)
572 { 583 {
573 Message msg(Message::Type::die); 584 game.playerDie(collider, map);
574 collider.send(game, msg);
575
576 playSound("../res/Hit_Hurt5.wav", 0.25);
577
578 game.schedule(FRAMES_PER_SECOND * 0.75, [&] () {
579 game.loadGame(map);
580 });
581 } 585 }
582 586
583 return false; 587 return false;
diff --git a/src/components.h b/src/components.h index f182590..f421529 100644 --- a/src/components.h +++ b/src/components.h
@@ -77,13 +77,22 @@ class MapCollisionComponent : public Component {
77 }; 77 };
78 78
79 struct Collision { 79 struct Collision {
80 enum class Type {
81 wall,
82 wrap,
83 teleport,
84 reverse,
85 platform,
86 danger
87 };
88
80 int axis; 89 int axis;
81 int lower; 90 int lower;
82 int upper; 91 int upper;
83 int type; 92 Type type;
84 }; 93 };
85 94
86 void addCollision(int axis, int lower, int upper, Direction dir, int type); 95 void addCollision(int axis, int lower, int upper, Direction dir, Collision::Type type);
87 bool processCollision(Game& game, Entity& collider, Collision collision, Direction dir); 96 bool processCollision(Game& game, Entity& collider, Collision collision, Direction dir);
88 97
89 std::list<Collision> left_collisions; 98 std::list<Collision> left_collisions;
diff --git a/src/entity.h b/src/entity.h index 772c98b..04772e9 100644 --- a/src/entity.h +++ b/src/entity.h
@@ -22,7 +22,8 @@ class Message {
22 drop, 22 drop,
23 canDrop, 23 canDrop,
24 cantDrop, 24 cantDrop,
25 die 25 die,
26 stopDying
26 }; 27 };
27 28
28 Message(Type type) : type(type) {} 29 Message(Type type) : type(type) {}
diff --git a/src/game.cpp b/src/game.cpp index 50c01f5..b2de5e9 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -1,6 +1,7 @@
1#include "game.h" 1#include "game.h"
2#include "renderer.h" 2#include "renderer.h"
3#include "components.h" 3#include "components.h"
4#include "muxer.h"
4 5
5Game::Game() 6Game::Game()
6{ 7{
@@ -132,33 +133,27 @@ void Game::saveGame(const Map& map, std::pair<double, double> position)
132 save = {&map, position}; 133 save = {&map, position};
133} 134}
134 135
135void Game::loadGame(const Map& curMap) 136void Game::schedule(int frames, std::function<void ()>&& callback)
136{ 137{
137 if (&curMap == save.map) 138 scheduled.emplace_front(frames, callback);
138 {
139 entities.remove(player);
140 }
141
142 player = std::make_shared<Entity>();
143 player->position = save.position;
144 player->size = std::make_pair(10.0,12.0);
145
146 auto player_input = std::make_shared<UserMovementComponent>();
147 player->addComponent(player_input);
148
149 auto player_physics = std::make_shared<PlayerPhysicsComponent>();
150 player->addComponent(player_physics);
151
152 auto player_anim = std::make_shared<PlayerSpriteComponent>();
153 player->addComponent(player_anim);
154
155 if (&curMap != save.map)
156 {
157 loadMap(*(save.map));
158 }
159} 139}
160 140
161void Game::schedule(int frames, std::function<void ()>&& callback) 141void Game::playerDie(Entity& player, const Map& curMap)
162{ 142{
163 scheduled.emplace_front(frames, callback); 143 Message msg(Message::Type::die);
144 player.send(*this, msg);
145
146 playSound("../res/Hit_Hurt5.wav", 0.25);
147
148 schedule(FRAMES_PER_SECOND * 0.75, [&] () {
149 if (&curMap != save.map)
150 {
151 loadMap(*(save.map));
152 }
153
154 player.position = save.position;
155
156 Message msg2(Message::Type::stopDying);
157 player.send(*this, msg2);
158 });
164} 159}
diff --git a/src/game.h b/src/game.h index ca6c00b..4f2f3df 100644 --- a/src/game.h +++ b/src/game.h
@@ -30,8 +30,8 @@ class Game {
30 void loadMap(const Map& map); 30 void loadMap(const Map& map);
31 void detectCollision(Entity& collider, std::pair<double, double> old_position); 31 void detectCollision(Entity& collider, std::pair<double, double> old_position);
32 void saveGame(const Map& map, std::pair<double, double> position); 32 void saveGame(const Map& map, std::pair<double, double> position);
33 void loadGame(const Map& curMap);
34 void schedule(int frames, std::function<void ()>&& callback); 33 void schedule(int frames, std::function<void ()>&& callback);
34 void playerDie(Entity& player, const Map& curMap);
35 35
36 private: 36 private:
37 friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 37 friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);