Skip to content

Commit

Permalink
WorldMap no longer inherits Level
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
Vankata453 committed Jul 12, 2023
1 parent 544f175 commit 2ba051d
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/supertux/game_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TileMap>(tileset, reader);
}, OBJ_PARAM_WORLDMAP);
});
}

std::unique_ptr<GameObject>
Expand Down
4 changes: 2 additions & 2 deletions src/supertux/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/supertux/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
28 changes: 10 additions & 18 deletions src/supertux/level_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ LevelParser::from_stream(std::istream& stream, const std::string& context, bool
}

std::unique_ptr<Level>
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<Level>(worldmap, temporary);
auto level = std::make_unique<Level>(worldmap);
LevelParser parser(*level, worldmap, editable);
parser.load(filename, info_only);
parser.load(filename);
return level;
}

Expand Down Expand Up @@ -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)
{
}
Expand All @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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 */
18 changes: 7 additions & 11 deletions src/supertux/level_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,28 @@ class Level;
class ReaderDocument;
class ReaderMapping;

class LevelParser
class LevelParser final
{
public:
static std::unique_ptr<Level> from_stream(std::istream& stream, const std::string& context, bool worldmap, bool editable);
static std::unique_ptr<Level> from_file(const std::string& filename, bool worldmap, bool editable, bool info_only = false, bool temporary = false);
static std::unique_ptr<Level> from_file(const std::string& filename, bool worldmap, bool editable);
static std::unique_ptr<Level> from_nothing(const std::string& basedir);
static std::unique_ptr<Level> 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:
Expand Down
16 changes: 15 additions & 1 deletion src/supertux/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand Down Expand Up @@ -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)
{
Expand Down
7 changes: 7 additions & 0 deletions src/supertux/sector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CollisionSystem;
class CollisionGroundMovementManager;
class DisplayEffect;
class DrawingContext;
class Level;
class MovingObject;
class Player;
class ReaderMapping;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/supertux/sector_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions src/supertux/sector_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
#include "squirrel/squirrel_environment.hpp"

class Level;
class TileSet;

namespace Base {

/** A base for sector classes. Contains main properties and functions. */
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. */
Expand All @@ -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;

Expand Down
26 changes: 10 additions & 16 deletions src/supertux/sector_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -258,8 +257,7 @@ SectorParser::parse_old_format(const ReaderMapping& reader)
std::vector<unsigned int> 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<TileMap>(tileset);
auto& tilemap = m_sector.add<TileMap>(m_sector.get_tileset());
tilemap.set(width, height, tiles, LAYER_TILES, true);

// replace tile id 112 (old invisible tile) with 1311 (new invisible tile)
Expand All @@ -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<TileMap>(tileset);
auto& tilemap = m_sector.add<TileMap>(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<TileMap>(tileset);
auto& tilemap = m_sector.add<TileMap>(m_sector.get_tileset());
tilemap.set(width, height, tiles, LAYER_FOREGROUNDTILES, false);

// fill additional space in foreground with tiles of ID 2035 (lightmap/black)
Expand Down Expand Up @@ -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>();
background.set_image(DEFAULT_BG);
background.set_speed(0.5);

auto& bkgrd = m_sector.add<TileMap>(tileset);
auto& bkgrd = m_sector.add<TileMap>(m_sector.get_tileset());
bkgrd.resize(100, 35);
bkgrd.set_layer(-100);
bkgrd.set_solid(false);

auto& frgrd = m_sector.add<TileMap>(tileset);
auto& frgrd = m_sector.add<TileMap>(m_sector.get_tileset());
frgrd.resize(100, 35);
frgrd.set_layer(100);
frgrd.set_solid(false);
Expand All @@ -362,22 +356,22 @@ SectorParser::create_sector()
}
else
{
auto& water = m_sector.add<TileMap>(tileset);
auto& water = m_sector.add<TileMap>(m_sector.get_tileset());
water.resize(100, 35, 1);
water.set_layer(-100);
water.set_solid(false);
}

auto& intact = m_sector.add<TileMap>(tileset);
if (worldmap) {
auto& intact = m_sector.add<TileMap>(m_sector.get_tileset());
if (m_sector.in_worldmap()) {
intact.resize(100, 100, 0);
} else {
intact.resize(100, 35, 0);
}
intact.set_layer(0);
intact.set_solid(true);

if (worldmap) {
if (m_sector.in_worldmap()) {
m_sector.add<worldmap::SpawnPointObject>("main", Vector(4, 4));
} else {
m_sector.add<SpawnPointMarker>("main", Vector(64, 480));
Expand Down
Loading

0 comments on commit 2ba051d

Please sign in to comment.