galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
World.cpp
Go to the documentation of this file.
1
7
8#include "galaxy/core/ServiceLocator.hpp"
10
11#include "World.hpp"
12
13namespace galaxy
14{
15 namespace map
16 {
18 : m_current {nullptr}
19 , m_loaded {false}
20 {
21 }
22
23 World::World(const std::string& file)
24 : m_current {nullptr}
25 , m_loaded {false}
26 {
27 if (!load(file))
28 {
29 GALAXY_LOG(GALAXY_ERROR, "Failed to load map from constructor. File is '{0}'.", file);
30 }
31 }
32
34 {
35 }
36
37 bool World::load(const std::string& file)
38 {
39 auto result = false;
40
41 if (!file.empty())
42 {
43 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
44 auto data = fs.read_binary(file);
45 if (!data.empty())
46 {
47 try
48 {
49 m_file = file;
50 m_project.loadFromMemory(data.data(), data.size());
51 result = m_loaded = true;
52 }
53 catch (const std::exception& e)
54 {
55 GALAXY_LOG(GALAXY_ERROR, "Failed to load map '{0}' because {1}.", file, e.what());
56 }
57 }
58 }
59
60 return result;
61 }
62
64 {
65 if (m_project.allWorlds().size() > 1)
66 {
67 GALAXY_LOG(GALAXY_WARNING, "Galaxy does not support LDTK multi-worlds.");
68 }
69
70 for (auto& level : m_project.getWorld().allLevels())
71 {
72 auto& map = m_maps[level.name];
73 map.load(level);
74 }
75 }
76
78 {
79 m_loaded = false;
80 m_file = "";
81 m_current = nullptr;
82 m_maps.clear();
83 }
84
85 void World::set_active(const std::string& map)
86 {
87 if (m_maps.contains(map))
88 {
89 m_current = &m_maps[map];
90 }
91 else
92 {
93 GALAXY_LOG(GALAXY_WARNING, "Map '{0}' does not exist.", map);
94 }
95 }
96
98 {
99 return m_current;
100 }
101
102 const std::string& World::name() const
103 {
104 return m_project.getWorld().getName();
105 }
106
107 const std::string& World::file() const
108 {
109 return m_file;
110 }
111
112 bool World::loaded() const
113 {
114 return m_loaded;
115 }
116
117 ankerl::unordered_dense::map<std::string, map::Map>& World::maps()
118 {
119 return m_maps;
120 }
121
122 } // namespace map
123} // namespace galaxy
#define GALAXY_WARNING
Definition Log.hpp:24
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
An LDTK level.
Definition Map.hpp:26
bool load(const std::string &file)
Load a world.
Definition World.cpp:37
ldtk::Project m_project
LDTK project data.
Definition World.hpp:117
const std::string & name() const
Get world name.
Definition World.cpp:102
World()
Constructor.
Definition World.cpp:17
map::Map * m_current
Current map.
Definition World.hpp:127
ankerl::unordered_dense::map< std::string, map::Map > & maps()
Get maps.
Definition World.cpp:117
~World()
Destructor.
Definition World.cpp:33
void set_active(const std::string &map)
Set currently active map.
Definition World.cpp:85
ankerl::unordered_dense::map< std::string, map::Map > m_maps
List of ldtk levels.
Definition World.hpp:122
bool loaded() const
Check if load() was called.
Definition World.cpp:112
std::string m_file
File.
Definition World.hpp:112
map::Map * get_active() const
Get currently active map.
Definition World.cpp:97
void parse()
Parse world and create entities.
Definition World.cpp:63
const std::string & file() const
Get file.
Definition World.cpp:107
bool m_loaded
Loaded flag.
Definition World.hpp:132
void clear()
Clear all map and world data.
Definition World.cpp:77
Timer.hpp galaxy.
Definition Async.hpp:17