Peoplemon  0.1.0
Peoplemon 3 game source documentation
World.cpp
Go to the documentation of this file.
1 #include <Core/Systems/World.hpp>
2 
3 #include <Core/Events/Maps.hpp>
5 #include <Core/Resources.hpp>
7 
8 namespace core
9 {
10 namespace system
11 {
12 namespace
13 {
14 using Serializer = bl::serial::json::Serializer<World>;
15 }
17 : owner(o) {}
18 
19 void World::init(bl::engine::Engine&) { bl::event::Dispatcher::subscribe(this); }
20 
21 void World::earlyCleanup() {
22  currentMap.release();
23  previousMap.release();
24 }
25 
26 bool World::switchMaps(const std::string& file, int spawn) {
27  if (file == "LastMap") {
28  if (!previousMap) {
29  if (prevMapFile.empty()) {
30  BL_LOG_ERROR << "No previous map to return to";
31  return false;
32  }
33  previousMap = MapManager::load(map::Map::getMapFile(prevMapFile));
34  if (!previousMap) return false;
35  }
36 
37  const bl::tmap::Position ppos = prevPlayerPos;
38  prevPlayerPos = owner.player().position();
39 
40  currentMap->exit(owner, previousMap->name());
41  owner.engine().ecs().destroyAllWorldEntities();
42  if (!previousMap->enter(owner, 0, currentMap->name(), ppos)) return false;
43  std::swap(currentMap, previousMap);
44  std::swap(prevMapFile, currentMapFile);
45  }
46  else {
47  // ensure we always have a whiteout location
48  if (owner.player().state().whiteoutMap.empty()) {
49  owner.player().state().whiteoutMap = file;
50  owner.player().state().whiteoutSpawn = spawn;
51  }
52 
53  previousMap = MapManager::load(map::Map::getMapFile(file));
54  if (!previousMap) return false;
55  prevMapFile = currentMapFile;
56  currentMapFile = file;
57 
58  if (currentMap) {
59  currentMap->exit(owner, previousMap->name());
60  prevPlayerPos = owner.player().position();
61  owner.engine().ecs().destroyAllWorldEntities();
62  }
63  std::swap(currentMap, previousMap);
64  if (!currentMap->enter(
65  owner, spawn, previousMap ? previousMap->name() : "NoPrevious", prevPlayerPos))
66  return false;
67  }
68 
69  return true;
70 }
71 
72 void World::whiteout(const std::string& map, int spawn) {
73  BL_LOG_INFO << "Whiting out to " << map << " (" << spawn << ")";
74  previousMap.release();
75  prevMapFile.clear();
76  bl::event::Dispatcher::dispatch<event::SwitchMapTriggered>({map, spawn});
77 }
78 
79 void World::setWhiteoutMap(unsigned int spawn) {
80  owner.player().state().whiteoutMap = currentMapFile;
81  owner.player().state().whiteoutSpawn = spawn;
82 }
83 
84 map::Map& World::activeMap() { return *currentMap; }
85 
86 const map::Map& World::activeMap() const { return *currentMap; }
87 
88 void World::update(std::mutex&, float dt, float, float, float) {
89  if (currentMap) { currentMap->update(dt); }
90 }
91 
93  if (save.saving) { playerPos = owner.player().position(); }
94  save.gameSave.world.currentMap = &currentMapFile;
95  save.gameSave.world.prevMap = &prevMapFile;
96  save.gameSave.world.playerPos = &playerPos;
97  save.gameSave.world.prevPlayerPos = &prevPlayerPos;
98 }
99 
101  currentMap = MapManager::load(map::Map::getMapFile(currentMapFile));
102  if (!currentMap) {
103  load.failMessage = "Failed to load map: " + currentMapFile;
104  return;
105  }
106  if (!currentMap->enter(owner, 0, prevMapFile, playerPos)) {
107  load.failMessage = "Failed to enter map: " + currentMapFile;
108  }
109 }
110 
111 } // namespace system
112 } // namespace core
Core classes and functionality for both the editor and game.
bl::serial::json::Serializer< Player > Serializer
Definition: Player.cpp:27
Fired when the game is saving or loading. Allows systems to hook in their data.
Definition: GameSave.hpp:22
bool saving
True when saving, false when loading.
Definition: GameSave.hpp:27
file::GameSave & gameSave
Game save being initialized.
Definition: GameSave.hpp:24
Fired when a game save is loaded. Fired after the load is complete.
Definition: GameSave.hpp:46
std::string failMessage
Modules can set this if they encounter a failure that should fail the entire load.
Definition: GameSave.hpp:48
struct core::file::GameSave::WorldDataPointers world
bl::tmap::Position * prevPlayerPos
Definition: GameSave.hpp:58
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
static std::string getMapFile(const std::string &partialFile)
Returns the full path to the map file from the given partial file. Accounts for missing extension.
Definition: Map.cpp:191
unsigned int whiteoutSpawn
Definition: State.hpp:53
std::string whiteoutMap
Definition: State.hpp:52
player::State & state()
Returns the state of the player.
Definition: Player.cpp:154
const bl::tmap::Position & position() const
Returns the current position of the player.
Definition: Player.cpp:57
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Definition: Systems.cpp:35
Player & player()
Returns the player system.
Definition: Systems.cpp:59
World(Systems &systems)
Creates the world system.
Definition: World.cpp:16
void setWhiteoutMap(unsigned int spawn)
Sets the respawn point to the given spawn in the current map.
Definition: World.cpp:79
void whiteout(const std::string &newMap, int spawnId)
Switches to the new map and resets the last map to empty.
Definition: World.cpp:72
bool switchMaps(const std::string &newMap, int spawnId)
Switches the current map to the map in the given file.
Definition: World.cpp:26
map::Map & activeMap()
Returns a reference to the active map.
Definition: World.cpp:84
virtual void observe(const event::GameSaveInitializing &save) override
Adds saved world data to the save file.
Definition: World.cpp:92