Skip to content

Commit

Permalink
Add a option "Test from here" to Firefly and Spawnpoint.
Browse files Browse the repository at this point in the history
Makes it much more convenient to test longer levels.
  • Loading branch information
divVerent committed Jun 8, 2020
1 parent 1b99e37 commit a0c8a93
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Editor::update(float dt_sec, const Controller& controller)
if (m_test_request) {
m_test_request = false;
MouseCursor::current()->set_icon(nullptr);
test_level();
test_level(m_test_pos);
return;
}

Expand Down Expand Up @@ -232,7 +232,7 @@ Editor::get_level_directory() const
}

void
Editor::test_level()
Editor::test_level(const boost::optional<std::pair<std::string, Vector>>& test_pos)
{
Tile::draw_editor_images = false;
Compositor::s_render_lighting = true;
Expand All @@ -252,7 +252,7 @@ Editor::test_level()
m_level->save(m_test_levelfile);
if (!m_level->is_worldmap())
{
GameManager::current()->start_level(*current_world, backup_filename);
GameManager::current()->start_level(*current_world, backup_filename, test_pos);
}
else
{
Expand Down Expand Up @@ -585,7 +585,7 @@ Editor::event(const SDL_Event& ev)
if (ev.type == SDL_KEYDOWN &&
ev.key.keysym.sym == SDLK_t &&
ev.key.keysym.mod & KMOD_CTRL) {
test_level();
test_level(boost::none);
}

if (ev.type == SDL_KEYDOWN &&
Expand Down
3 changes: 2 additions & 1 deletion src/editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Editor final : public Screen,
void reload_level();
void quit_editor();
void save_level();
void test_level();
void test_level(const boost::optional<std::pair<std::string, Vector>>& test_pos);
void update_keyboard(const Controller& controller);

protected:
Expand All @@ -155,6 +155,7 @@ class Editor final : public Screen,
bool m_deactivate_request;
bool m_save_request;
bool m_test_request;
boost::optional<std::pair<std::string, Vector>> m_test_pos;

std::unique_ptr<Savegame> m_savegame;

Expand Down
9 changes: 9 additions & 0 deletions src/editor/object_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ ObjectMenu::menu_action(MenuItem& item)
m_object->remove_me();
break;

case MNID_TEST_FROM_HERE: {
const MovingObject *here = dynamic_cast<const MovingObject *>(m_object);
m_editor.m_test_pos = std::make_pair(m_editor.get_sector()->get_name(),
here->get_pos());
m_editor.m_test_request = true;
MenuManager::instance().pop_menu();
break;
}

default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/editor/object_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class ObjectMenu final : public Menu
{
public:
enum {
MNID_REMOVE
MNID_REMOVE,
MNID_TEST_FROM_HERE
};

public:
Expand Down
17 changes: 17 additions & 0 deletions src/editor/object_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,21 @@ RemoveObjectOption::add_to_menu(Menu& menu) const
menu.add_entry(ObjectMenu::MNID_REMOVE, get_text());
}

TestFromHereOption::TestFromHereOption() :
ObjectOption(_("Test from here"), "", 0)
{
}

std::string
TestFromHereOption::to_string() const
{
return {};
}

void
TestFromHereOption::add_to_menu(Menu& menu) const
{
menu.add_entry(ObjectMenu::MNID_TEST_FROM_HERE, get_text());
}

/* EOF */
14 changes: 14 additions & 0 deletions src/editor/object_option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ class RemoveObjectOption : public ObjectOption
RemoveObjectOption& operator=(const RemoveObjectOption&) = delete;
};

class TestFromHereOption : public ObjectOption
{
public:
TestFromHereOption();

virtual void save(Writer& write) const override {}
virtual std::string to_string() const override;
virtual void add_to_menu(Menu& menu) const override;

private:
TestFromHereOption(const TestFromHereOption&) = delete;
TestFromHereOption& operator=(const TestFromHereOption&) = delete;
};

#endif

/* EOF */
6 changes: 6 additions & 0 deletions src/editor/object_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ ObjectSettings::add_sexp(const std::string& text, const std::string& key, sexp::
add_option(std::make_unique<SExpObjectOption>(text, key, value, flags));
}

void
ObjectSettings::add_test_from_here()
{
add_option(std::make_unique<TestFromHereOption>());
}

void
ObjectSettings::reorder(const std::vector<std::string>& order)
{
Expand Down
1 change: 1 addition & 0 deletions src/editor/object_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class ObjectSettings final
unsigned int flags = 0);
void add_sexp(const std::string& text, const std::string& key,
sexp::Value& value, unsigned int flags = 0);
void add_test_from_here();

const std::vector<std::unique_ptr<ObjectOption> >& get_options() const { return m_options; }

Expand Down
8 changes: 8 additions & 0 deletions src/object/firefly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,12 @@ Firefly::collision(GameObject& other, const CollisionHit& )
return ABORT_MOVE;
}

ObjectSettings
Firefly::get_settings()
{
ObjectSettings result = MovingObject::get_settings();
result.add_test_from_here();
return result;
}

/* EOF */
1 change: 1 addition & 0 deletions src/object/firefly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Firefly final : public MovingSprite
virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual std::string get_class() const override { return "firefly"; }
virtual std::string get_display_name() const override { return _("Checkpoint"); }
virtual ObjectSettings get_settings() override;

private:
SpritePtr m_sprite_light;
Expand Down
8 changes: 8 additions & 0 deletions src/object/spawnpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ SpawnPointMarker::draw(DrawingContext& context)
}
}

ObjectSettings
SpawnPointMarker::get_settings()
{
ObjectSettings result = MovingObject::get_settings();
result.add_test_from_here();
return result;
}

/* EOF */
1 change: 1 addition & 0 deletions src/object/spawnpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SpawnPointMarker final : public MovingObject

virtual std::string get_class() const override { return "spawnpoint"; }
virtual std::string get_display_name() const override { return _("Spawnpoint"); }
virtual ObjectSettings get_settings() override;

private:
SurfacePtr m_surface;
Expand Down
6 changes: 4 additions & 2 deletions src/supertux/game_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ GameManager::GameManager() :
}

void
GameManager::start_level(const World& world, const std::string& level_filename)
GameManager::start_level(const World& world, const std::string& level_filename,
const boost::optional<std::pair<std::string, Vector>>& start_pos)
{
m_savegame = Savegame::from_file(world.get_savegame_filename());

auto screen = std::make_unique<LevelsetScreen>(world.get_basedir(),
level_filename,
*m_savegame);
*m_savegame,
start_pos);
ScreenManager::current()->push_screen(std::move(screen));
}

Expand Down
5 changes: 4 additions & 1 deletion src/supertux/game_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_MANAGER_HPP
#define HEADER_SUPERTUX_SUPERTUX_GAME_MANAGER_HPP

#include <boost/optional.hpp>
#include <memory>
#include <string>
#include "math/vector.hpp"
#include "util/currenton.hpp"

class Savegame;
Expand All @@ -30,7 +32,8 @@ class GameManager final : public Currenton<GameManager>
GameManager();

void start_worldmap(const World& world, const std::string& spawnpoint = "", const std::string& worldmap_filename = "");
void start_level(const World& world, const std::string& level_filename);
void start_level(const World& world, const std::string& level_filename,
const boost::optional<std::pair<std::string, Vector>>& start_pos = boost::none);

bool load_next_worldmap();
void set_next_worldmap(const std::string& worldmap, const std::string &spawnpoint);
Expand Down
17 changes: 16 additions & 1 deletion src/supertux/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ GameSession::GameSession(const std::string& levelfile_, Savegame& savegame, Stat
m_levelfile(levelfile_),
m_start_sector("main"),
m_start_spawnpoint("main"),
m_start_pos(),
m_reset_sector(),
m_reset_pos(),
m_newsector(),
Expand Down Expand Up @@ -134,7 +135,11 @@ GameSession::restart_level(bool after_death)
if (!m_currentsector)
throw std::runtime_error("Couldn't find main sector");
m_play_time = 0;
m_currentsector->activate(m_start_spawnpoint);
if (m_start_spawnpoint.empty()) {
m_currentsector->activate(m_start_pos);
} else {
m_currentsector->activate(m_start_spawnpoint);
}
}
} catch(std::exception& e) {
log_fatal << "Couldn't start level: " << e.what() << std::endl;
Expand Down Expand Up @@ -465,6 +470,16 @@ GameSession::set_start_point(const std::string& sector,
{
m_start_sector = sector;
m_start_spawnpoint = spawnpoint;
m_start_pos = Vector();
}

void
GameSession::set_start_pos(const std::string& sector,
const Vector& pos)
{
m_start_sector = sector;
m_start_spawnpoint = "";
m_start_pos = pos;
}

void
Expand Down
3 changes: 3 additions & 0 deletions src/supertux/game_session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class GameSession final : public Screen,
void reset_level();
void set_start_point(const std::string& sectorname,
const std::string& spawnpointname);
void set_start_pos(const std::string& sectorname, const Vector& pos);
void set_reset_point(const std::string& sectorname, const Vector& pos);
std::string get_reset_point_sectorname() const { return m_reset_sector; }

Expand Down Expand Up @@ -115,8 +116,10 @@ class GameSession final : public Screen,
std::string m_levelfile;

// spawn point (the point where tux respawns at startup). Usually both "main".
// If m_start_spawnpoint is set, m_start_pos shall not, and vice versa.
std::string m_start_sector;
std::string m_start_spawnpoint;
Vector m_start_pos;

// reset point (the point where tux respawns if he dies)
std::string m_reset_sector;
Expand Down
10 changes: 8 additions & 2 deletions src/supertux/levelset_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#include "util/log.hpp"

LevelsetScreen::LevelsetScreen(const std::string& basedir, const std::string& level_filename,
Savegame& savegame) :
Savegame& savegame,
const boost::optional<std::pair<std::string, Vector>>& start_pos) :
m_basedir(basedir),
m_level_filename(level_filename),
m_savegame(savegame),
m_level_started(false),
m_solved(false)
m_solved(false),
m_start_pos(start_pos)
{
Levelset levelset(basedir);
for (int i = 0; i < levelset.get_num_levels(); ++i)
Expand Down Expand Up @@ -83,6 +85,10 @@ LevelsetScreen::setup()
} else {
auto screen = std::make_unique<GameSession>(FileSystem::join(m_basedir, m_level_filename),
m_savegame);
if (m_start_pos) {
screen->set_start_pos(m_start_pos->first, m_start_pos->second);
screen->restart_level();
}
ScreenManager::current()->push_screen(std::move(screen));
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/supertux/levelset_screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#ifndef HEADER_SUPERTUX_SUPERTUX_LEVELSET_SCREEN_HPP
#define HEADER_SUPERTUX_SUPERTUX_LEVELSET_SCREEN_HPP

#include <boost/optional.hpp>
#include <string>

#include "math/vector.hpp"
#include "supertux/screen.hpp"
#include "util/currenton.hpp"

Expand All @@ -35,7 +37,8 @@ class LevelsetScreen final : public Screen,
bool m_solved;

public:
LevelsetScreen(const std::string& basedir, const std::string& level_filename, Savegame& savegame);
LevelsetScreen(const std::string& basedir, const std::string& level_filename, Savegame& savegame,
const boost::optional<std::pair<std::string, Vector>>& start_pos);

virtual void draw(Compositor& compositor) override;
virtual void update(float dt_sec, const Controller& controller) override;
Expand All @@ -46,6 +49,8 @@ class LevelsetScreen final : public Screen,
void finished_level(bool win);

private:
boost::optional<std::pair<std::string, Vector>> m_start_pos;

LevelsetScreen(const LevelsetScreen&) = delete;
LevelsetScreen& operator=(const LevelsetScreen&) = delete;
};
Expand Down
1 change: 1 addition & 0 deletions src/supertux/menu/editor_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ EditorMenu::menu_action(MenuItem& item)
{
editor->check_save_prerequisites([editor]() {
MenuManager::instance().clear_menu_stack();
editor->m_test_pos = boost::none;
editor->m_test_request = true;
});
}
Expand Down

0 comments on commit a0c8a93

Please sign in to comment.