From 2ba051d36dd4d81b099ffd148eb0a318c3be9f01 Mon Sep 17 00:00:00 2001 From: Vankata453 <78196474+Vankata453@users.noreply.github.com> Date: Thu, 13 Jul 2023 01:48:32 +0300 Subject: [PATCH] WorldMap no longer inherits Level [ci skip] --- src/supertux/game_object_factory.cpp | 2 +- src/supertux/level.cpp | 4 +- src/supertux/level.hpp | 6 +- src/supertux/level_parser.cpp | 28 +++------ src/supertux/level_parser.hpp | 18 +++--- src/supertux/sector.cpp | 16 ++++- src/supertux/sector.hpp | 7 +++ src/supertux/sector_base.cpp | 3 +- src/supertux/sector_base.hpp | 10 +-- src/supertux/sector_parser.cpp | 26 +++----- src/worldmap/level_tile.cpp | 18 ++++-- src/worldmap/world_select.cpp | 1 - src/worldmap/worldmap.cpp | 63 ++++++++++++------- src/worldmap/worldmap.hpp | 18 +++--- src/worldmap/worldmap_sector.cpp | 17 ++--- src/worldmap/worldmap_sector.hpp | 6 +- ..._parser.cpp => worldmap_sector_parser.cpp} | 43 +++---------- ..._parser.hpp => worldmap_sector_parser.hpp} | 27 +------- 18 files changed, 146 insertions(+), 167 deletions(-) rename src/worldmap/{worldmap_parser.cpp => worldmap_sector_parser.cpp} (69%) rename src/worldmap/{worldmap_parser.hpp => worldmap_sector_parser.hpp} (65%) diff --git a/src/supertux/game_object_factory.cpp b/src/supertux/game_object_factory.cpp index 6d1b6ed80ff..cbcfd321052 100644 --- a/src/supertux/game_object_factory.cpp +++ b/src/supertux/game_object_factory.cpp @@ -312,7 +312,7 @@ GameObjectFactory::init_factories() add_factory("tilemap", TileMap::display_name(), [](const ReaderMapping& reader) { auto tileset = TileManager::current()->get_tileset(Level::current()->get_tileset()); return std::make_unique(tileset, reader); - }, OBJ_PARAM_WORLDMAP); + }); } std::unique_ptr diff --git a/src/supertux/level.cpp b/src/supertux/level.cpp index 0920b113102..2da05490ef3 100644 --- a/src/supertux/level.cpp +++ b/src/supertux/level.cpp @@ -32,7 +32,7 @@ Level* Level::s_current = nullptr; -Level::Level(bool worldmap, bool temporary) : +Level::Level(bool worldmap) : m_is_worldmap(worldmap), m_name("noname"), m_author("SuperTux Player"), @@ -51,7 +51,7 @@ Level::Level(bool worldmap, bool temporary) : m_icon_locked(), m_wmselect_bkg() { - if (!temporary) s_current = this; + s_current = this; } Level::~Level() diff --git a/src/supertux/level.hpp b/src/supertux/level.hpp index b3fcc9d7c22..67ec6f6c4e8 100644 --- a/src/supertux/level.hpp +++ b/src/supertux/level.hpp @@ -26,7 +26,7 @@ class Writer; /** Represents a collection of Sectors running in a single GameSession. Each Sector in turn contains GameObjects, e.g. Badguys and Players. */ -class Level +class Level final { friend class LevelParser; @@ -37,8 +37,8 @@ class Level static Level* s_current; public: - explicit Level(bool m_is_worldmap, bool temporary = false); - virtual ~Level(); + explicit Level(bool m_is_worldmap); + ~Level(); // saves to a levelfile void save(const std::string& filename, bool retry = false); diff --git a/src/supertux/level_parser.cpp b/src/supertux/level_parser.cpp index 6efb144f767..cf4a41a0ce4 100644 --- a/src/supertux/level_parser.cpp +++ b/src/supertux/level_parser.cpp @@ -64,11 +64,11 @@ LevelParser::from_stream(std::istream& stream, const std::string& context, bool } std::unique_ptr -LevelParser::from_file(const std::string& filename, bool worldmap, bool editable, bool info_only, bool temporary) +LevelParser::from_file(const std::string& filename, bool worldmap, bool editable) { - auto level = std::make_unique(worldmap, temporary); + auto level = std::make_unique(worldmap); LevelParser parser(*level, worldmap, editable); - parser.load(filename, info_only); + parser.load(filename); return level; } @@ -117,7 +117,7 @@ LevelParser::from_nothing_worldmap(const std::string& basedir, const std::string LevelParser::LevelParser(Level& level, bool worldmap, bool editable) : m_level(level), - m_is_worldmap(worldmap), + m_worldmap(worldmap), m_editable(editable) { } @@ -130,13 +130,13 @@ LevelParser::load(std::istream& stream, const std::string& context) } void -LevelParser::load(const std::string& filepath, bool info_only) +LevelParser::load(const std::string& filepath) { m_level.m_filename = filepath; register_translation_directory(filepath); try { auto doc = ReaderDocument::from_file(filepath); - load(doc, info_only); + load(doc); } catch(std::exception& e) { std::stringstream msg; msg << "Problem when reading level '" << filepath << "': " << e.what(); @@ -145,7 +145,7 @@ LevelParser::load(const std::string& filepath, bool info_only) } void -LevelParser::load(const ReaderDocument& doc, bool info_only) +LevelParser::load(const ReaderDocument& doc) { auto root = doc.get_root(); @@ -173,14 +173,13 @@ LevelParser::load(const ReaderDocument& doc, bool info_only) level.get("icon-locked", m_level.m_icon_locked); level.get("bkg", m_level.m_wmselect_bkg); - if (info_only) return; // Read only the general information about the level. - auto iter = level.get_iter(); while (iter.next()) { if (iter.get_key() == "sector") { - add_sector(iter.as_mapping()); + auto sector = SectorParser::from_reader(m_level, iter.as_mapping(), m_editable); + m_level.add_sector(std::move(sector)); } } @@ -213,18 +212,11 @@ LevelParser::create(const std::string& filepath, const std::string& levelname) m_level.m_filename = filepath; m_level.m_name = levelname; m_level.m_license = "CC-BY-SA 4.0 International"; - m_level.m_tileset = m_is_worldmap ? "images/ice_world.strf" : "images/tiles.strf"; + m_level.m_tileset = m_worldmap ? "images/ice_world.strf" : "images/tiles.strf"; auto sector = SectorParser::from_nothing(m_level); sector->set_name("main"); m_level.add_sector(std::move(sector)); } - -void -LevelParser::add_sector(const ReaderMapping& reader) -{ - m_level.add_sector(SectorParser::from_reader(m_level, reader, m_editable)); -} - /* EOF */ diff --git a/src/supertux/level_parser.hpp b/src/supertux/level_parser.hpp index 0fa6b20f515..5403376102a 100644 --- a/src/supertux/level_parser.hpp +++ b/src/supertux/level_parser.hpp @@ -25,32 +25,28 @@ class Level; class ReaderDocument; class ReaderMapping; -class LevelParser +class LevelParser final { public: static std::unique_ptr from_stream(std::istream& stream, const std::string& context, bool worldmap, bool editable); - static std::unique_ptr from_file(const std::string& filename, bool worldmap, bool editable, bool info_only = false, bool temporary = false); + static std::unique_ptr from_file(const std::string& filename, bool worldmap, bool editable); static std::unique_ptr from_nothing(const std::string& basedir); static std::unique_ptr from_nothing_worldmap(const std::string& basedir, const std::string& name); static std::string get_level_name(const std::string& filename); -protected: +private: LevelParser(Level& level, bool worldmap, bool editable); - virtual ~LevelParser() {} - void load(const ReaderDocument& doc, bool info_only = false); + void load(const ReaderDocument& doc); void load(std::istream& stream, const std::string& context); - void load(const std::string& filepath, bool info_only = false); + void load(const std::string& filepath); void load_old_format(const ReaderMapping& reader); void create(const std::string& filepath, const std::string& levelname); - /** Adds a sector to the Level. */ - virtual void add_sector(const ReaderMapping& reader); - -protected: +private: Level& m_level; - bool m_is_worldmap; + bool m_worldmap; bool m_editable; private: diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 2cd7b0dd4cb..06e120b4e19 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -55,6 +55,7 @@ #include "supertux/resources.hpp" #include "supertux/savegame.hpp" #include "supertux/tile.hpp" +#include "supertux/tile_manager.hpp" #include "util/file_system.hpp" #include "util/writer.hpp" #include "video/video_system.hpp" @@ -70,7 +71,8 @@ PlayerStatus dummy_player_status(1); Sector::Sector(Level& parent) : - Base::Sector(parent, "sector"), + Base::Sector("sector"), + m_level(parent), m_fully_constructed(false), m_foremost_layer(), m_gravity(10.0f), @@ -345,6 +347,18 @@ Sector::get_foremost_layer() const return m_foremost_layer; } +TileSet* +Sector::get_tileset() const +{ + return TileManager::current()->get_tileset(m_level.get_tileset()); +} + +bool +Sector::in_worldmap() const +{ + return m_level.is_worldmap(); +} + void Sector::update(float dt_sec) { diff --git a/src/supertux/sector.hpp b/src/supertux/sector.hpp index 0c01c41d348..aacbb49e6a7 100644 --- a/src/supertux/sector.hpp +++ b/src/supertux/sector.hpp @@ -38,6 +38,7 @@ class CollisionSystem; class CollisionGroundMovementManager; class DisplayEffect; class DrawingContext; +class Level; class MovingObject; class Player; class ReaderMapping; @@ -68,6 +69,10 @@ class Sector final : public Base::Sector void finish_construction(bool editable) override; + Level& get_level() const { return m_level; } + TileSet* get_tileset() const override; + bool in_worldmap() const override; + /** activates this sector (change music, initialize player class, ...) */ void activate(const std::string& spawnpoint); void activate(const Vector& player_pos); @@ -148,6 +153,8 @@ class Sector final : public Base::Sector void convert_tiles2gameobject(); private: + Level& m_level; // Parent level + bool m_fully_constructed; int m_foremost_layer; diff --git a/src/supertux/sector_base.cpp b/src/supertux/sector_base.cpp index 0b242bb2a95..306f7722ffd 100644 --- a/src/supertux/sector_base.cpp +++ b/src/supertux/sector_base.cpp @@ -20,8 +20,7 @@ namespace Base { -Sector::Sector(Level& parent, const std::string& type) : - m_level(parent), +Sector::Sector(const std::string& type) : m_name(), m_init_script(), m_squirrel_environment(new SquirrelEnvironment(SquirrelVirtualMachine::current()->get_vm(), type)) diff --git a/src/supertux/sector_base.hpp b/src/supertux/sector_base.hpp index a8c893aba03..20b47db63c6 100644 --- a/src/supertux/sector_base.hpp +++ b/src/supertux/sector_base.hpp @@ -22,6 +22,7 @@ #include "squirrel/squirrel_environment.hpp" class Level; +class TileSet; namespace Base { @@ -29,7 +30,7 @@ namespace Base { class Sector : public GameObjectManager { public: - Sector(Level& parent, const std::string& type); + Sector(const std::string& type); /** Needs to be called after parsing to finish the construction of the Sector before using it. */ @@ -38,17 +39,16 @@ class Sector : public GameObjectManager virtual void draw(DrawingContext& context) = 0; virtual void update(float dt_sec) = 0; - Level& get_level() const { return m_level; } + virtual TileSet* get_tileset() const = 0; + virtual bool in_worldmap() const = 0; - void set_name(const std::string& name_) { m_name = name_; } + void set_name(const std::string& name) { m_name = name; } const std::string& get_name() const { return m_name; } void set_init_script(const std::string& init_script) { m_init_script = init_script; } void run_script(const std::string& script, const std::string& sourcename); protected: - Level& m_level; // Parent level - std::string m_name; std::string m_init_script; diff --git a/src/supertux/sector_parser.cpp b/src/supertux/sector_parser.cpp index af2031913a4..45229011c39 100644 --- a/src/supertux/sector_parser.cpp +++ b/src/supertux/sector_parser.cpp @@ -39,7 +39,6 @@ #include "supertux/level.hpp" #include "supertux/sector.hpp" #include "supertux/tile.hpp" -#include "supertux/tile_manager.hpp" #include "util/reader_collection.hpp" #include "util/reader_mapping.hpp" #include "worldmap/spawn_point.hpp" @@ -258,8 +257,7 @@ SectorParser::parse_old_format(const ReaderMapping& reader) std::vector tiles; if (reader.get("interactive-tm", tiles) || reader.get("tilemap", tiles)) { - auto* tileset = TileManager::current()->get_tileset(m_sector.get_level().get_tileset()); - auto& tilemap = m_sector.add(tileset); + auto& tilemap = m_sector.add(m_sector.get_tileset()); tilemap.set(width, height, tiles, LAYER_TILES, true); // replace tile id 112 (old invisible tile) with 1311 (new invisible tile) @@ -275,15 +273,13 @@ SectorParser::parse_old_format(const ReaderMapping& reader) } if (reader.get("background-tm", tiles)) { - auto* tileset = TileManager::current()->get_tileset(m_sector.get_level().get_tileset()); - auto& tilemap = m_sector.add(tileset); + auto& tilemap = m_sector.add(m_sector.get_tileset()); tilemap.set(width, height, tiles, LAYER_BACKGROUNDTILES, false); if (height < 19) tilemap.resize(width, 19); } if (reader.get("foreground-tm", tiles)) { - auto* tileset = TileManager::current()->get_tileset(m_sector.get_level().get_tileset()); - auto& tilemap = m_sector.add(tileset); + auto& tilemap = m_sector.add(m_sector.get_tileset()); tilemap.set(width, height, tiles, LAYER_FOREGROUNDTILES, false); // fill additional space in foreground with tiles of ID 2035 (lightmap/black) @@ -337,20 +333,18 @@ SectorParser::parse_old_format(const ReaderMapping& reader) void SectorParser::create_sector() { - auto tileset = TileManager::current()->get_tileset(m_sector.get_level().get_tileset()); - bool worldmap = m_sector.get_level().is_worldmap(); - if (!worldmap) + if (!m_sector.in_worldmap()) { auto& background = m_sector.add(); background.set_image(DEFAULT_BG); background.set_speed(0.5); - auto& bkgrd = m_sector.add(tileset); + auto& bkgrd = m_sector.add(m_sector.get_tileset()); bkgrd.resize(100, 35); bkgrd.set_layer(-100); bkgrd.set_solid(false); - auto& frgrd = m_sector.add(tileset); + auto& frgrd = m_sector.add(m_sector.get_tileset()); frgrd.resize(100, 35); frgrd.set_layer(100); frgrd.set_solid(false); @@ -362,14 +356,14 @@ SectorParser::create_sector() } else { - auto& water = m_sector.add(tileset); + auto& water = m_sector.add(m_sector.get_tileset()); water.resize(100, 35, 1); water.set_layer(-100); water.set_solid(false); } - auto& intact = m_sector.add(tileset); - if (worldmap) { + auto& intact = m_sector.add(m_sector.get_tileset()); + if (m_sector.in_worldmap()) { intact.resize(100, 100, 0); } else { intact.resize(100, 35, 0); @@ -377,7 +371,7 @@ SectorParser::create_sector() intact.set_layer(0); intact.set_solid(true); - if (worldmap) { + if (m_sector.in_worldmap()) { m_sector.add("main", Vector(4, 4)); } else { m_sector.add("main", Vector(64, 480)); diff --git a/src/worldmap/level_tile.cpp b/src/worldmap/level_tile.cpp index 1ada89da3d7..9bccae009a2 100644 --- a/src/worldmap/level_tile.cpp +++ b/src/worldmap/level_tile.cpp @@ -25,6 +25,7 @@ #include "util/file_system.hpp" #include "util/gettext.hpp" #include "util/log.hpp" +#include "util/reader_document.hpp" #include "util/reader_mapping.hpp" #include "worldmap/worldmap.hpp" @@ -68,7 +69,8 @@ LevelTile::LevelTile(const ReaderMapping& mapping) : return; } - load_level_information(); + if (in_worldmap()) + load_level_information(); } LevelTile::~LevelTile() @@ -96,12 +98,16 @@ LevelTile::load_level_information() try { - // Read general level info. - auto level = LevelParser::from_file(filename, false, false, true, true); + auto doc = ReaderDocument::from_file(filename); + auto root = doc.get_root(); - // Set properties to the ones of the level. - m_title = level->m_name; - m_target_time = level->m_target_time; + if (root.get_name() != "supertux-level") + throw std::runtime_error("'" + filename + "': file is not a supertux-level file."); + + auto mapping = root.get_mapping(); + + mapping.get("name", m_title); + mapping.get("target-time", m_target_time); } catch (std::exception& err) { diff --git a/src/worldmap/world_select.cpp b/src/worldmap/world_select.cpp index d489ff6df45..2d5dd30fd26 100644 --- a/src/worldmap/world_select.cpp +++ b/src/worldmap/world_select.cpp @@ -29,7 +29,6 @@ #include "video/compositor.hpp" #include "video/drawing_context.hpp" #include "video/surface.hpp" -#include "worldmap/worldmap_parser.hpp" #include "worldmap/worldmap.hpp" namespace worldmap { diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index 6de0f27906c..3549d8cd057 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -20,32 +20,39 @@ #include "audio/sound_manager.hpp" #include "gui/menu_manager.hpp" +#include "physfs/util.hpp" #include "supertux/fadetoblack.hpp" #include "supertux/game_manager.hpp" #include "supertux/gameconfig.hpp" +#include "supertux/menu/menu_storage.hpp" #include "supertux/player_status.hpp" #include "supertux/screen_manager.hpp" -#include "supertux/menu/menu_storage.hpp" +#include "supertux/tile_manager.hpp" +#include "util/file_system.hpp" #include "util/log.hpp" +#include "util/reader.hpp" +#include "util/reader_document.hpp" +#include "util/reader_mapping.hpp" #include "video/drawing_context.hpp" #include "worldmap/direction.hpp" #include "worldmap/level_tile.hpp" #include "worldmap/tux.hpp" #include "worldmap/world_select.hpp" -#include "worldmap/worldmap_parser.hpp" #include "worldmap/worldmap_screen.hpp" #include "worldmap/worldmap_sector.hpp" +#include "worldmap/worldmap_sector_parser.hpp" #include "worldmap/worldmap_state.hpp" namespace worldmap { WorldMap::WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint) : - Level(true), m_sector(), - m_worldmap_sectors(), + m_sectors(), m_savegame(savegame), - m_map_filename(filename), - m_levels_path(), + m_tileset(), + m_name(), + m_map_filename(physfsutil::realpath(filename)), + m_levels_path(FileSystem::dirname(m_map_filename)), m_next_worldmap(), m_passive_message(), m_passive_message_timer(), @@ -55,14 +62,30 @@ WorldMap::WorldMap(const std::string& filename, Savegame& savegame, const std::s { SoundManager::current()->preload("sounds/warp.wav"); - // Load worldmap objects. - WorldMapParser parser(*this); - parser.load(filename); -} + /** Parse worldmap */ + register_translation_directory(m_map_filename); + auto doc = ReaderDocument::from_file(m_map_filename); + auto root = doc.get_root(); -WorldMap::~WorldMap() -{ - m_worldmap_sectors.clear(); + if (root.get_name() != "supertux-level") + throw std::runtime_error("file isn't a supertux-level file."); + + auto mapping = root.get_mapping(); + + mapping.get("name", m_name); + + std::string tileset_name; + if (mapping.get("tileset", tileset_name)) + m_tileset = TileManager::current()->get_tileset(tileset_name); + else + m_tileset = TileManager::current()->get_tileset("images/ice_world.strf"); + + auto iter = mapping.get_iter(); + while (iter.next()) + { + if (iter.get_key() == "sector") + add_sector(WorldMapSectorParser::from_reader(*this, iter.as_mapping())); + } } @@ -73,7 +96,6 @@ WorldMap::setup() load_state(); m_sector->setup(); - m_sector->finish_setup(); m_in_world_select = false; } @@ -173,7 +195,7 @@ size_t WorldMap::level_count() const { size_t count = 0; - for (auto& sector : m_worldmap_sectors) + for (auto& sector : m_sectors) { count += sector->level_count(); } @@ -184,7 +206,7 @@ size_t WorldMap::solved_level_count() const { size_t count = 0; - for (auto& sector : m_worldmap_sectors) + for (auto& sector : m_sectors) { count += sector->solved_level_count(); } @@ -236,7 +258,7 @@ WorldMap::set_passive_message(const std::string& message, float time) WorldMapSector* WorldMap::get_sector(const std::string& name) const { - for (auto& sector : m_worldmap_sectors) + for (auto& sector : m_sectors) { if (sector->get_name() == name) return sector.get(); @@ -247,17 +269,17 @@ WorldMap::get_sector(const std::string& name) const WorldMapSector* WorldMap::get_sector(int index) const { - if (index < 0 || index > static_cast(m_worldmap_sectors.size()) - 1) + if (index < 0 || index > static_cast(m_sectors.size()) - 1) return nullptr; - return m_worldmap_sectors.at(index).get(); + return m_sectors.at(index).get(); } void WorldMap::add_sector(std::unique_ptr sector) { - m_worldmap_sectors.push_back(std::move(sector)); + m_sectors.push_back(std::move(sector)); } void @@ -282,7 +304,6 @@ WorldMap::set_sector(const std::string& name, const std::string& spawnpoint, if (perform_full_setup) load_state(); m_sector->setup(); - m_sector->finish_setup(); // If a spawnpoint has been provided, move to it. if (!spawnpoint.empty()) diff --git a/src/worldmap/worldmap.hpp b/src/worldmap/worldmap.hpp index 971923df447..e7e791eb693 100644 --- a/src/worldmap/worldmap.hpp +++ b/src/worldmap/worldmap.hpp @@ -19,23 +19,19 @@ #ifndef HEADER_SUPERTUX_WORLDMAP_WORLDMAP_HPP #define HEADER_SUPERTUX_WORLDMAP_WORLDMAP_HPP -#include "supertux/level.hpp" #include "util/currenton.hpp" #include "control/controller.hpp" #include "supertux/savegame.hpp" +#include "supertux/timer.hpp" #include "worldmap/worldmap_sector.hpp" class TileSet; namespace worldmap { -class WorldMapSector; - -class WorldMap final : public Level, - public Currenton +class WorldMap final : public Currenton { - friend class WorldMapParser; friend class WorldMapSector; friend class WorldMapState; @@ -44,11 +40,8 @@ class WorldMap final : public Level, static Color s_message_color; static Color s_teleporter_message_color; - static WorldMap* current() { return Currenton::current(); } - public: WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint = ""); - ~WorldMap() override; void setup(); void leave(); @@ -93,14 +86,17 @@ class WorldMap final : public Level, private: WorldMapSector* m_sector; /* The currently active sector. */ - std::vector > m_worldmap_sectors; + std::vector > m_sectors; Savegame& m_savegame; + TileSet* m_tileset; + std::string m_name; std::string m_map_filename; std::string m_levels_path; - std::unique_ptr m_next_worldmap; /* A worldmap, scheduled to change to next frame. */ + /* A worldmap, scheduled to change to next frame. */ + std::unique_ptr m_next_worldmap; /** Passive map message variables */ std::string m_passive_message; diff --git a/src/worldmap/worldmap_sector.cpp b/src/worldmap/worldmap_sector.cpp index ccc11350469..e680bb94c36 100644 --- a/src/worldmap/worldmap_sector.cpp +++ b/src/worldmap/worldmap_sector.cpp @@ -32,6 +32,7 @@ #include "supertux/game_manager.hpp" #include "supertux/game_session.hpp" #include "supertux/gameconfig.hpp" +#include "supertux/level.hpp" #include "supertux/player_status_hud.hpp" #include "supertux/resources.hpp" #include "supertux/screen_manager.hpp" @@ -43,6 +44,7 @@ #include "worldmap/spawn_point.hpp" #include "worldmap/special_tile.hpp" #include "worldmap/teleporter.hpp" +#include "worldmap/worldmap.hpp" namespace worldmap { @@ -57,7 +59,7 @@ WorldMapSector::current() WorldMapSector::WorldMapSector(WorldMap& parent) : - Base::Sector(parent, "worldmap"), + Base::Sector("worldmap"), m_parent(parent), m_camera(new Camera), m_tux(&add(&parent)), @@ -127,14 +129,7 @@ WorldMapSector::setup() // register worldmap_table as "worldmap" in scripting m_squirrel_environment->expose_self(); - m_squirrel_environment->expose("settings", std::make_unique(this)); -} - -void -WorldMapSector::finish_setup() -{ - BIND_WORLDMAP_SECTOR(*this); /** Force spawnpoint, if the property is set. **/ if (!m_force_spawnpoint.empty()) @@ -597,6 +592,12 @@ WorldMapSector::before_object_remove(GameObject& object) } +TileSet* +WorldMapSector::get_tileset() const +{ + return m_parent.m_tileset; +} + Vector WorldMapSector::get_tux_pos() const { diff --git a/src/worldmap/worldmap_sector.hpp b/src/worldmap/worldmap_sector.hpp index 1488782c09f..9627b5e4752 100644 --- a/src/worldmap/worldmap_sector.hpp +++ b/src/worldmap/worldmap_sector.hpp @@ -21,9 +21,7 @@ #include "supertux/sector_base.hpp" -#include "supertux/timer.hpp" #include "worldmap/tux.hpp" -#include "worldmap/worldmap.hpp" namespace worldmap { @@ -48,7 +46,6 @@ class WorldMapSector final : public Base::Sector void finish_construction(bool) override; void setup(); - void finish_setup(); void leave(); void draw(DrawingContext& context) override; @@ -111,6 +108,9 @@ class WorldMapSector final : public Base::Sector /** Sets the initial spawnpoint on worldmap setup */ void set_initial_spawnpoint(const std::string& spawnpoint_name); + bool in_worldmap() const override { return true; } + + TileSet* get_tileset() const override; WorldMap& get_worldmap() const { return m_parent; } Camera& get_camera() const { return *m_camera; } Tux& get_tux() const { return *m_tux; } diff --git a/src/worldmap/worldmap_parser.cpp b/src/worldmap/worldmap_sector_parser.cpp similarity index 69% rename from src/worldmap/worldmap_parser.cpp rename to src/worldmap/worldmap_sector_parser.cpp index 372db8cf0f6..1c4f92673b3 100644 --- a/src/worldmap/worldmap_parser.cpp +++ b/src/worldmap/worldmap_sector_parser.cpp @@ -1,6 +1,5 @@ // SuperTux -// Copyright (C) 2018 Ingo Ruhnke -// 2023 Vankata453 +// Copyright (C) 2023 Vankata453 // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -15,45 +14,16 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#include "worldmap/worldmap_parser.hpp" +#include "worldmap/worldmap_sector_parser.hpp" -#include "physfs/util.hpp" +#include "object/tilemap.hpp" #include "supertux/d_scope.hpp" #include "supertux/game_object_factory.hpp" -#include "util/file_system.hpp" #include "worldmap/spawn_point.hpp" -#include "worldmap/worldmap.hpp" +#include "worldmap/worldmap_sector.hpp" namespace worldmap { -WorldMapParser::WorldMapParser(WorldMap& worldmap) : - LevelParser(worldmap, true, false) -{ -} - -WorldMap& -WorldMapParser::get_worldmap() const -{ - return static_cast(m_level); -} - -void -WorldMapParser::load(const std::string& filepath) -{ - get_worldmap().m_map_filename = physfsutil::realpath(filepath); - get_worldmap().m_levels_path = FileSystem::dirname(get_worldmap().m_map_filename); - - LevelParser::load(filepath); -} - -void -WorldMapParser::add_sector(const ReaderMapping& reader) -{ - get_worldmap().add_sector(WorldMapSectorParser::from_reader(get_worldmap(), reader)); -} - - - std::unique_ptr WorldMapSectorParser::from_reader(WorldMap& worldmap, const ReaderMapping& reader) { @@ -84,6 +54,11 @@ WorldMapSectorParser::parse_object_additional(const std::string& name, const Rea get_sector().m_spawnpoints.push_back(std::make_unique(reader)); return true; } + else if (name == "tilemap") // Custom rule for adding tilemaps on worldmaps + { + get_sector().add(get_sector().get_tileset(), reader); + return true; + } // Proceed adding the object only if it's flagged as allowed for worldmaps return !GameObjectFactory::instance().has_params(name, ObjectFactory::RegisteredObjectParam::OBJ_PARAM_WORLDMAP); diff --git a/src/worldmap/worldmap_parser.hpp b/src/worldmap/worldmap_sector_parser.hpp similarity index 65% rename from src/worldmap/worldmap_parser.hpp rename to src/worldmap/worldmap_sector_parser.hpp index a13ac654292..4460faf0bfd 100644 --- a/src/worldmap/worldmap_parser.hpp +++ b/src/worldmap/worldmap_sector_parser.hpp @@ -1,7 +1,5 @@ // SuperTux -// Copyright (C) 2004-2018 Ingo Ruhnke -// 2006 Christoph Sommer -// 2023 Vankata453 +// Copyright (C) 2023 Vankata453 // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -16,10 +14,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef HEADER_SUPERTUX_WORLDMAP_WORLDMAP_PARSER_HPP -#define HEADER_SUPERTUX_WORLDMAP_WORLDMAP_PARSER_HPP +#ifndef HEADER_SUPERTUX_WORLDMAP_WORLDMAP_SECTOR_PARSER_HPP +#define HEADER_SUPERTUX_WORLDMAP_WORLDMAP_SECTOR_PARSER_HPP -#include "supertux/level_parser.hpp" #include "supertux/sector_parser.hpp" namespace worldmap { @@ -27,24 +24,6 @@ namespace worldmap { class WorldMap; class WorldMapSector; -class WorldMapParser final : public LevelParser -{ -public: - WorldMapParser(WorldMap& worldmap); - - void load(const std::string& filepath); - -private: - WorldMap& get_worldmap() const; - - void add_sector(const ReaderMapping& reader) override; - -private: - WorldMapParser(const WorldMapParser&) = delete; - WorldMapParser& operator=(const WorldMapParser&) = delete; -}; - - class WorldMapSectorParser final : public SectorParser { public: