summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/components.cpp2
-rw-r--r--src/entityfactory.cpp6
-rw-r--r--src/entityfactory.h2
-rw-r--r--src/game.cpp16
-rw-r--r--src/game.h5
-rw-r--r--src/map.cpp2
6 files changed, 18 insertions, 15 deletions
diff --git a/src/components.cpp b/src/components.cpp index 954ac57..512fbed 100644 --- a/src/components.cpp +++ b/src/components.cpp
@@ -596,7 +596,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli
596 collider.send(game, msg); 596 collider.send(game, msg);
597 } else if (collision.type == Collision::Type::danger) 597 } else if (collision.type == Collision::Type::danger)
598 { 598 {
599 game.playerDie(collider, map); 599 game.playerDie();
600 } 600 }
601 601
602 return false; 602 return false;
diff --git a/src/entityfactory.cpp b/src/entityfactory.cpp index bf137e2..47a1463 100644 --- a/src/entityfactory.cpp +++ b/src/entityfactory.cpp
@@ -15,7 +15,7 @@ struct EntityData {
15 15
16static std::map<std::string, EntityData> factories; 16static std::map<std::string, EntityData> factories;
17 17
18std::shared_ptr<Entity> EntityFactory::createNamedEntity(const std::string name, const Map& map) 18std::shared_ptr<Entity> EntityFactory::createNamedEntity(const std::string name)
19{ 19{
20 auto it = factories.find(name); 20 auto it = factories.find(name);
21 EntityData data = factories[name]; 21 EntityData data = factories[name];
@@ -81,10 +81,10 @@ std::shared_ptr<Entity> EntityFactory::createNamedEntity(const std::string name,
81 { 81 {
82 if (!strcmp(data.action, "save")) 82 if (!strcmp(data.action, "save"))
83 { 83 {
84 auto component = std::make_shared<SimpleColliderComponent>([&] (Game& game, Entity& collider) { 84 auto component = std::make_shared<SimpleColliderComponent>([&] (Game& game, Entity&) {
85 playSound("../res/Pickup_Coin23.wav", 0.25); 85 playSound("../res/Pickup_Coin23.wav", 0.25);
86 86
87 game.saveGame(map, collider.position); 87 game.saveGame();
88 }); 88 });
89 entity->addComponent(component); 89 entity->addComponent(component);
90 } 90 }
diff --git a/src/entityfactory.h b/src/entityfactory.h index 7b8f399..870d6d5 100644 --- a/src/entityfactory.h +++ b/src/entityfactory.h
@@ -8,7 +8,7 @@ class Map;
8 8
9class EntityFactory { 9class EntityFactory {
10 public: 10 public:
11 static std::shared_ptr<Entity> createNamedEntity(const std::string name, const Map& map); 11 static std::shared_ptr<Entity> createNamedEntity(const std::string name);
12}; 12};
13 13
14#endif 14#endif
diff --git a/src/game.cpp b/src/game.cpp index dd8f956..6e79f75 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -123,6 +123,8 @@ void Game::loadMap(const Map& map)
123 nextEntities.push_back(player); 123 nextEntities.push_back(player);
124 124
125 newWorld = true; 125 newWorld = true;
126
127 currentMap = &map;
126} 128}
127 129
128void Game::detectCollision(Entity& collider, std::pair<double, double> old_position) 130void Game::detectCollision(Entity& collider, std::pair<double, double> old_position)
@@ -133,9 +135,9 @@ void Game::detectCollision(Entity& collider, std::pair<double, double> old_posit
133 } 135 }
134} 136}
135 137
136void Game::saveGame(const Map& map, std::pair<double, double> position) 138void Game::saveGame()
137{ 139{
138 save = {&map, position}; 140 save = {currentMap, player->position};
139} 141}
140 142
141void Game::schedule(double time, std::function<void ()> callback) 143void Game::schedule(double time, std::function<void ()> callback)
@@ -143,19 +145,19 @@ void Game::schedule(double time, std::function<void ()> callback)
143 scheduled.emplace_front(time, std::move(callback)); 145 scheduled.emplace_front(time, std::move(callback));
144} 146}
145 147
146void Game::playerDie(Entity& player, const Map& curMap) 148void Game::playerDie()
147{ 149{
148 player.send(*this, Message::Type::die); 150 player->send(*this, Message::Type::die);
149 151
150 playSound("../res/Hit_Hurt5.wav", 0.25); 152 playSound("../res/Hit_Hurt5.wav", 0.25);
151 153
152 schedule(0.75, [&] () { 154 schedule(0.75, [&] () {
153 if (curMap != *save.map) 155 if (*currentMap != *save.map)
154 { 156 {
155 loadMap(*save.map); 157 loadMap(*save.map);
156 } 158 }
157 159
158 player.position = save.position; 160 player->position = save.position;
159 player.send(*this, Message::Type::stopDying); 161 player->send(*this, Message::Type::stopDying);
160 }); 162 });
161} 163}
diff --git a/src/game.h b/src/game.h index 065aca8..bc31912 100644 --- a/src/game.h +++ b/src/game.h
@@ -30,9 +30,9 @@ class Game {
30 void execute(GLFWwindow* window); 30 void execute(GLFWwindow* window);
31 void loadMap(const Map& map); 31 void loadMap(const Map& map);
32 void detectCollision(Entity& collider, std::pair<double, double> old_position); 32 void detectCollision(Entity& collider, std::pair<double, double> old_position);
33 void saveGame(const Map& map, std::pair<double, double> position); 33 void saveGame();
34 void schedule(double time, std::function<void ()> callback); 34 void schedule(double time, std::function<void ()> callback);
35 void playerDie(Entity& player, const Map& curMap); 35 void playerDie();
36 36
37 private: 37 private:
38 friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); 38 friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
@@ -41,6 +41,7 @@ class Game {
41 std::list<std::shared_ptr<Entity>> nextEntities; 41 std::list<std::shared_ptr<Entity>> nextEntities;
42 bool newWorld; 42 bool newWorld;
43 std::shared_ptr<Entity> player; 43 std::shared_ptr<Entity> player;
44 const Map* currentMap;
44 Savefile save; 45 Savefile save;
45 std::list<std::pair<double, std::function<void ()>>> scheduled; 46 std::list<std::pair<double, std::function<void ()>>> scheduled;
46 bool shouldQuit = false; 47 bool shouldQuit = false;
diff --git a/src/map.cpp b/src/map.cpp index 5201cf2..73eb2b4 100644 --- a/src/map.cpp +++ b/src/map.cpp
@@ -176,7 +176,7 @@ void Map::createEntities(std::list<std::shared_ptr<Entity>>& entities) const
176{ 176{
177 for (auto data : this->entities) 177 for (auto data : this->entities)
178 { 178 {
179 auto entity = EntityFactory::createNamedEntity(data.name, *this); 179 auto entity = EntityFactory::createNamedEntity(data.name);
180 entity->position = data.position; 180 entity->position = data.position;
181 181
182 entities.push_back(entity); 182 entities.push_back(entity);