Peoplemon  0.1.0
Peoplemon 3 game source documentation
Map.hpp
Go to the documentation of this file.
1 #ifndef CORE_MAPS_MAP_HPP
2 #define CORE_MAPS_MAP_HPP
3 
7 #include <Core/Maps/Event.hpp>
8 #include <Core/Maps/Item.hpp>
9 #include <Core/Maps/LayerSet.hpp>
13 #include <Core/Maps/Spawn.hpp>
14 #include <Core/Maps/Tileset.hpp>
15 #include <Core/Maps/Town.hpp>
16 #include <Core/Maps/Weather.hpp>
17 
18 #include <BLIB/Audio.hpp>
19 #include <BLIB/ECS.hpp>
20 #include <BLIB/Events.hpp>
21 #include <BLIB/Resources.hpp>
22 #include <BLIB/Scripts.hpp>
23 #include <BLIB/Serialization.hpp>
24 #include <functional>
25 #include <unordered_map>
26 #include <vector>
27 
34 namespace core
35 {
36 namespace system
37 {
38 class Systems;
39 }
40 
42 namespace map
43 {
49 class Map : public bl::event::Listener<event::EntityMoved> {
50 public:
54  Map();
55 
59  virtual ~Map();
60 
68  static std::string getMapFile(const std::string& partialFile);
69 
78  bool loadDev(std::istream& input);
79 
88  bool loadProd(bl::serial::binary::InputStream& input);
89 
97  bool save(const std::string& file);
98 
106  bool saveBundle(bl::serial::binary::OutputStream& output,
107  bl::resource::bundle::FileHandlerContext& ctx) const;
108 
119  bool enter(system::Systems& systems, std::uint16_t spawnId, const std::string& prevMap,
120  const bl::tmap::Position& prevPlayerPos);
121 
128  void exit(system::Systems& systems, const std::string& newMap);
129 
136 
140  const std::string& name() const;
141 
146 
151 
155  const sf::Vector2i& sizeTiles() const;
156 
160  sf::Vector2f sizePixels() const;
161 
165  std::uint8_t levelCount() const;
166 
172  void update(float dt);
173 
180  bool contains(const bl::tmap::Position& position) const;
181 
190  bl::tmap::Position adjacentTile(const bl::tmap::Position& pos, bl::tmap::Direction dir) const;
191 
200  bool movePossible(const bl::tmap::Position& position, bl::tmap::Direction dir) const;
201 
209  bool isLedgeHop(const bl::tmap::Position& position, bl::tmap::Direction dir) const;
210 
216  virtual void observe(const event::EntityMoved& moveEvent) override;
217 
224  bool interact(bl::ecs::Entity interactor, const bl::tmap::Position& interactPos);
225 
232  const CatchRegion* getCatchRegion(const bl::tmap::Position& position) const;
233 
237  static const std::vector<Town>& FlyMapTowns();
238 
242  bool canFlyFromHere() const;
243 
250  const bl::tmap::Position* getSpawnPosition(unsigned int spawnId) const;
251 
258  const std::string& getLocationName(const bl::tmap::Position& pos) const;
259 
269  float getDepthForPosition(unsigned int level, unsigned int y, int layer = -1) const;
270 
275  float getMinDepth() const;
276 
280  bl::rc::SceneRef getScene() { return scene; }
281 
285  bl::rc::lgt::Scene2DLighting& getSceneLighting() {
286  return static_cast<bl::rc::scene::Scene2D*>(scene.get())->getLighting();
287  }
288 
295  void setupEntityPosition(bl::ecs::Entity entity);
296 
297 protected:
298  std::string nameField;
299  std::string loadScriptField;
300  std::string unloadScriptField;
301  std::string playlistField;
303  std::vector<LayerSet> levels;
304  std::string tilesetField;
305  std::unordered_map<std::uint16_t, Spawn> spawns;
306  std::vector<CharacterSpawn> characterField;
307  std::vector<Item> itemsField;
308  std::vector<Event> eventsField;
310  std::vector<CatchRegion> catchRegionsField;
311  bl::ctr::Vector2D<LevelTransition> transitionField;
312  std::vector<Town> towns;
313  bl::ctr::Vector2D<std::uint8_t> townTiles;
314 
318  sf::Vector2i size;
319  bl::resource::Ref<Tileset> tileset;
321  std::unique_ptr<bl::script::Script> onEnterScript;
322  std::unique_ptr<bl::script::Script> onExitScript;
323  bl::ctr::Grid<const Event*> eventRegions;
325  bl::audio::AudioSystem::Handle playlistHandle;
326  bool activated; // for weather continuity
327 
328  bl::rc::SceneRef scene;
329  std::list<RenderLevel> renderLevels;
330 
331  void clear();
332  void finishLoad();
333  void triggerAnimation(const bl::tmap::Position& position);
334  Town* getTown(const glm::i32vec2& pos);
335  void enterTown(Town* town);
336 
337  void prepareRender();
338  void cleanupRender();
339  void setupLevel(unsigned int level);
340  void setupLayer(unsigned int level, unsigned int layer);
341  void setupTile(unsigned int level, unsigned int layer, const sf::Vector2u& pos);
342 
343  static std::vector<Town> flymapTowns;
344  static void loadFlymapTowns();
345 
346  friend struct bl::serial::SerializableObject<Map>;
347 };
348 
349 } // namespace map
350 } // namespace core
351 
352 namespace bl
353 {
354 namespace serial
355 {
356 template<>
357 struct SerializableObject<core::map::Map> : public SerializableObjectBase {
358  using M = core::map::Map;
359 
360  SerializableField<1, M, std::string> nameField;
361  SerializableField<2, M, std::string> loadScriptField;
362  SerializableField<3, M, std::string> unloadScriptField;
363  SerializableField<4, M, std::string> playlistField;
364  SerializableField<5, M, core::map::Weather::Type> weatherField;
365  SerializableField<6, M, std::vector<core::map::LayerSet>> levels;
366  SerializableField<7, M, std::string> tilesetField;
367  SerializableField<8, M, std::unordered_map<std::uint16_t, core::map::Spawn>> spawnField;
368  SerializableField<9, M, std::vector<core::map::CharacterSpawn>> characterField;
369  SerializableField<10, M, std::vector<core::map::Item>> itemsField;
370  SerializableField<11, M, std::vector<core::map::Event>> eventsField;
371  SerializableField<12, M, core::map::LightingSystem> lighting; // 13 was catch zones
372  SerializableField<14, M, bl::ctr::Vector2D<core::map::LevelTransition>> transitionField;
373  SerializableField<15, M, std::vector<core::map::CatchRegion>> catchRegionsField;
374  SerializableField<16, M, std::vector<core::map::Town>> townsField;
375  SerializableField<17, M, bl::ctr::Vector2D<std::uint8_t>> townTiles;
376 
378  : SerializableObjectBase("Map")
379  , nameField("name", *this, &M::nameField, SerializableFieldBase::Required{})
380  , loadScriptField("loadScript", *this, &M::loadScriptField, SerializableFieldBase::Required{})
381  , unloadScriptField("unloadScript", *this, &M::unloadScriptField,
382  SerializableFieldBase::Required{})
383  , playlistField("playlist", *this, &M::playlistField, SerializableFieldBase::Required{})
384  , weatherField("weather", *this, &M::weatherField, SerializableFieldBase::Required{})
385  , levels("levels", *this, &M::levels, SerializableFieldBase::Required{})
386  , tilesetField("tileset", *this, &M::tilesetField, SerializableFieldBase::Required{})
387  , spawnField("spawns", *this, &M::spawns, SerializableFieldBase::Required{})
388  , characterField("characters", *this, &M::characterField, SerializableFieldBase::Required{})
389  , itemsField("items", *this, &M::itemsField, SerializableFieldBase::Required{})
390  , eventsField("events", *this, &M::eventsField, SerializableFieldBase::Required{})
391  , lighting("lighting", *this, &M::lighting, SerializableFieldBase::Required{})
392  , transitionField("transitions", *this, &M::transitionField, SerializableFieldBase::Required{})
393  , catchRegionsField("catchZones", *this, &M::catchRegionsField,
394  SerializableFieldBase::Required{})
395  , townsField("towns", *this, &M::towns, SerializableFieldBase::Required{})
396  , townTiles("townTiles", *this, &M::townTiles, SerializableFieldBase::Required{}) {}
397 };
398 
399 } // namespace serial
400 } // namespace bl
401 
402 #endif
Core classes and functionality for both the editor and game.
Fired after an entity begins moving from one position to another.
Definition: EntityMoved.hpp:18
Represents a class of catchable peoplemon.
Definition: CatchRegion.hpp:18
System for handling lighting in Maps. Manages all the lights and performs rendering....
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
float getMinDepth() const
Returns the maximum depth possible for this map. Maximum is always 0.f. Minimum is negative,...
Definition: Map.cpp:775
virtual void observe(const event::EntityMoved &moveEvent) override
Event listener for moving entities. Used to trigger map events.
Definition: Map.cpp:385
bool enter(system::Systems &systems, std::uint16_t spawnId, const std::string &prevMap, const bl::tmap::Position &prevPlayerPos)
Initializes runtime data structures and spawns entities into the game. Also runs the on-load script.
Definition: Map.cpp:32
bl::ctr::Grid< const Event * > eventRegions
Definition: Map.hpp:323
bl::tmap::Position adjacentTile(const bl::tmap::Position &pos, bl::tmap::Direction dir) const
Returns the adjacent position to the given position when moving in the given direction....
Definition: Map.cpp:276
std::vector< Item > itemsField
Definition: Map.hpp:307
bool isLedgeHop(const bl::tmap::Position &position, bl::tmap::Direction dir) const
Test whether the given movement will be a ledge hop or not.
Definition: Map.cpp:378
std::unordered_map< std::uint16_t, Spawn > spawns
Definition: Map.hpp:305
LightingSystem lighting
Definition: Map.hpp:309
std::string nameField
Definition: Map.hpp:298
const CatchRegion * getCatchRegion(const bl::tmap::Position &position) const
Returns the catch region at the given position if the position is on a catch tile.
Definition: Map.cpp:567
std::vector< LayerSet > levels
Definition: Map.hpp:303
bool isWorldMap
Definition: Map.hpp:324
bool loadDev(std::istream &input)
Loads the map from the given file. Will try to determine if the extension or path need to be added in...
Definition: Map.cpp:203
Town * getTown(const glm::i32vec2 &pos)
Definition: Map.cpp:541
void setupCamera(system::Systems &systems)
Configures the game camera for the player in the map.
Definition: Map.cpp:160
const std::string & getLocationName(const bl::tmap::Position &pos) const
Returns the name of the town or route at the given position.
Definition: Map.cpp:610
virtual ~Map()
Destroy the Map.
Definition: Map.cpp:27
std::string unloadScriptField
Definition: Map.hpp:300
void enterTown(Town *town)
Definition: Map.cpp:549
bool save(const std::string &file)
Saves the map to the given file. The path should be relative to the maps directory and include the ....
Definition: Map.cpp:229
static std::vector< Town > flymapTowns
Definition: Map.hpp:343
std::vector< Event > eventsField
Definition: Map.hpp:308
bool saveBundle(bl::serial::binary::OutputStream &output, bl::resource::bundle::FileHandlerContext &ctx) const
Saves the data from this object to the given bundle and registers dependency files.
Definition: Map.cpp:246
Weather weather
Definition: Map.hpp:320
float getDepthForPosition(unsigned int level, unsigned int y, int layer=-1) const
Computes the depth to use at the given y position, taking into account the map size,...
Definition: Map.cpp:741
Map()
Creates an empty Map.
Definition: Map.cpp:21
std::string loadScriptField
Definition: Map.hpp:299
Weather::Type weatherField
Definition: Map.hpp:302
LightingSystem & lightingSystem()
Returns a reference to the lighting system in this map.
Definition: Map.cpp:184
const bl::tmap::Position * getSpawnPosition(unsigned int spawnId) const
Returns the position of the given player spawn, or nullptr if not found.
Definition: Map.cpp:605
bool canFlyFromHere() const
Returns whether or not the player can fly from this map.
Definition: Map.cpp:603
sf::Vector2i size
Definition: Map.hpp:318
void finishLoad()
Definition: Map.cpp:215
bl::resource::Ref< Tileset > tileset
Definition: Map.hpp:319
bool contains(const bl::tmap::Position &position) const
Returns whether or not the map contains the given position.
Definition: Map.cpp:271
void prepareRender()
Definition: Map.cpp:614
bool interact(bl::ecs::Entity interactor, const bl::tmap::Position &interactPos)
Lets entities interact with the map itself. This is called by the Interaction system.
Definition: Map.cpp:454
std::unique_ptr< bl::script::Script > onEnterScript
Definition: Map.hpp:321
std::uint8_t levelCount() const
Returns the number of levels in the map.
Definition: Map.cpp:180
void setupTile(unsigned int level, unsigned int layer, const sf::Vector2u &pos)
Definition: Map.cpp:641
std::unique_ptr< bl::script::Script > onExitScript
Definition: Map.hpp:322
static const std::vector< Town > & FlyMapTowns()
Returns the set of towns that can be flown to.
Definition: Map.cpp:587
void exit(system::Systems &systems, const std::string &newMap)
Removes spawned entities and runs the on-unload script.
Definition: Map.cpp:136
std::vector< Town > towns
Definition: Map.hpp:312
bl::ctr::Vector2D< std::uint8_t > townTiles
Definition: Map.hpp:313
const sf::Vector2i & sizeTiles() const
Returns the size of the map in tiles.
Definition: Map.cpp:173
std::vector< CharacterSpawn > characterField
Definition: Map.hpp:306
std::string tilesetField
Definition: Map.hpp:304
Town defaultTown
Definition: Map.hpp:316
void clear()
Definition: Map.cpp:523
bl::ctr::Vector2D< LevelTransition > transitionField
Definition: Map.hpp:311
void triggerAnimation(const bl::tmap::Position &position)
Definition: Map.cpp:439
bl::audio::AudioSystem::Handle playlistHandle
Definition: Map.hpp:325
void setupLayer(unsigned int level, unsigned int layer)
Definition: Map.cpp:635
bl::rc::lgt::Scene2DLighting & getSceneLighting()
Returns the scene lighting for this map. Only valid after enter() is called.
Definition: Map.hpp:285
bl::rc::SceneRef getScene()
Returns the scene for this map.
Definition: Map.hpp:280
std::string playlistField
Definition: Map.hpp:301
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
Weather & weatherSystem()
Returns a reference to the weather system in this map.
Definition: Map.cpp:182
bool loadProd(bl::serial::binary::InputStream &input)
Loads the map from the given file. Will try to determine if the extension or path need to be added in...
Definition: Map.cpp:209
void setupEntityPosition(bl::ecs::Entity entity)
Performs the final setup of the position components for the given entity. Must already have a bl::tma...
Definition: Map.cpp:792
bl::rc::SceneRef scene
Definition: Map.hpp:328
bool activated
Definition: Map.hpp:326
Town * currentTown
Definition: Map.hpp:317
std::vector< CatchRegion > catchRegionsField
Definition: Map.hpp:310
static void loadFlymapTowns()
Definition: Map.cpp:592
void cleanupRender()
Definition: Map.cpp:779
void update(float dt)
Updates internal logic over the elapsed time.
Definition: Map.cpp:186
system::Systems * systems
Definition: Map.hpp:315
bool movePossible(const bl::tmap::Position &position, bl::tmap::Direction dir) const
Returns whether or not a particular movement is possible. Does not take into account entities blockin...
Definition: Map.cpp:329
void setupLevel(unsigned int level)
Definition: Map.cpp:624
const std::string & name() const
Returns the name of the map.
Definition: Map.cpp:171
sf::Vector2f sizePixels() const
Returns the size of the map in pixels.
Definition: Map.cpp:175
std::list< RenderLevel > renderLevels
Definition: Map.hpp:329
SerializableField< 6, M, std::vector< core::map::LayerSet > > levels
Definition: Map.hpp:365
SerializableField< 14, M, bl::ctr::Vector2D< core::map::LevelTransition > > transitionField
Definition: Map.hpp:372
SerializableField< 16, M, std::vector< core::map::Town > > townsField
Definition: Map.hpp:374
SerializableField< 1, M, std::string > nameField
Definition: Map.hpp:360
SerializableField< 9, M, std::vector< core::map::CharacterSpawn > > characterField
Definition: Map.hpp:368
SerializableField< 2, M, std::string > loadScriptField
Definition: Map.hpp:361
SerializableField< 4, M, std::string > playlistField
Definition: Map.hpp:363
SerializableField< 17, M, bl::ctr::Vector2D< std::uint8_t > > townTiles
Definition: Map.hpp:375
SerializableField< 15, M, std::vector< core::map::CatchRegion > > catchRegionsField
Definition: Map.hpp:373
SerializableField< 11, M, std::vector< core::map::Event > > eventsField
Definition: Map.hpp:370
SerializableField< 10, M, std::vector< core::map::Item > > itemsField
Definition: Map.hpp:369
SerializableField< 8, M, std::unordered_map< std::uint16_t, core::map::Spawn > > spawnField
Definition: Map.hpp:367
SerializableField< 3, M, std::string > unloadScriptField
Definition: Map.hpp:362
SerializableField< 7, M, std::string > tilesetField
Definition: Map.hpp:366
SerializableField< 5, M, core::map::Weather::Type > weatherField
Definition: Map.hpp:364
SerializableField< 12, M, core::map::LightingSystem > lighting
Definition: Map.hpp:371
Represents a town, route, or region within a map. Maps may have many towns. Individual tiles are asso...
Definition: Town.hpp:22
Parent weather system for maps. Manages active weather.
Definition: Weather.hpp:32
Type
The type of weather.
Definition: Weather.hpp:38
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47