Skip to content

Commit

Permalink
Moved Levelset into it's own class, pass WorldState around instead of…
Browse files Browse the repository at this point in the history
… PlayerState, removed .../world.nut support

Some code is currently disabled with "#ifdef GRUMBEL"
  • Loading branch information
Grumbel committed Aug 14, 2014
1 parent c81b842 commit 8bd7a82
Show file tree
Hide file tree
Showing 19 changed files with 263 additions and 217 deletions.
28 changes: 19 additions & 9 deletions src/supertux/game_manager.cpp
Expand Up @@ -28,11 +28,14 @@
#include "supertux/screen_fade.hpp"
#include "supertux/screen_manager.hpp"
#include "supertux/world.hpp"
#include "supertux/world_state.hpp"
#include "util/file_system.hpp"
#include "util/log.hpp"
#include "worldmap/worldmap.hpp"

GameManager::GameManager() :
m_world()
m_world(),
m_world_state()
{
}

Expand All @@ -41,25 +44,32 @@ GameManager::~GameManager()
}

void
GameManager::start_level(std::unique_ptr<World> world, int index)
GameManager::start_level(const std::string& level_filename)
{
/*
m_world = std::move(world);
m_world_state.reset(new WorldState);
m_world_state->load(m_world->get_savegame_filename());
std::unique_ptr<Screen> screen(new GameSession(m_world->get_level_filename(index),
m_world->get_player_status()));
std::unique_ptr<Screen> screen(new GameSession(level_filename,
&m_world_state));
g_screen_manager->push_screen(std::move(screen));
*/
}

void
GameManager::start_game(std::unique_ptr<World> world)
{
m_world = std::move(world);

MenuManager::instance().clear_menu_stack();

try
{
m_world->run();
m_world = std::move(world);
m_world_state.reset(new WorldState);

m_world_state->load(m_world->get_savegame_filename());

g_screen_manager->push_screen(std::unique_ptr<Screen>(
new worldmap::WorldMap(m_world->get_worldmap_filename(),
*m_world_state)));
}
catch(std::exception& e)
{
Expand Down
4 changes: 3 additions & 1 deletion src/supertux/game_manager.hpp
Expand Up @@ -19,6 +19,7 @@

#include <memory>

#include "supertux/world_state.hpp"
#include "util/currenton.hpp"

class World;
Expand All @@ -27,13 +28,14 @@ class GameManager : public Currenton<GameManager>
{
private:
std::unique_ptr<World> m_world;
std::unique_ptr<WorldState> m_world_state;

public:
GameManager();
~GameManager();

void start_game(std::unique_ptr<World> world);
void start_level(std::unique_ptr<World> world, int index);
void start_level(const std::string& level_filename);

std::string get_level_name(const std::string& levelfile) const;

Expand Down
11 changes: 6 additions & 5 deletions src/supertux/game_session.cpp
Expand Up @@ -40,11 +40,12 @@
#include "supertux/screen_fade.hpp"
#include "supertux/screen_manager.hpp"
#include "supertux/sector.hpp"
#include "supertux/world_state.hpp"
#include "util/file_system.hpp"
#include "util/gettext.hpp"
#include "worldmap/worldmap.hpp"

GameSession::GameSession(const std::string& levelfile_, PlayerStatus* player_status, Statistics* statistics) :
GameSession::GameSession(const std::string& levelfile_, WorldState& world_state, Statistics* statistics) :
level(),
statistics_backdrop(Surface::create("images/engine/menu/score-backdrop.png")),
scripts(),
Expand All @@ -60,7 +61,7 @@ GameSession::GameSession(const std::string& levelfile_, PlayerStatus* player_sta
newsector(),
newspawnpoint(),
best_level_statistics(statistics),
player_status(player_status),
m_world_state(world_state),
capture_demo_stream(0),
capture_file(),
playback_demo_stream(0),
Expand All @@ -80,7 +81,7 @@ GameSession::GameSession(const std::string& levelfile_, PlayerStatus* player_sta
int
GameSession::restart_level()
{
PlayerStatus* currentStatus = get_player_status();
PlayerStatus* currentStatus = m_world_state.get_player_status();
coins_at_start = currentStatus->coins;
bonus_at_start = currentStatus->bonus;
max_fire_bullets_at_start = currentStatus->max_fire_bullets;
Expand Down Expand Up @@ -256,7 +257,7 @@ GameSession::abort_level()
MenuManager::instance().clear_menu_stack();
g_screen_manager->pop_screen();
currentsector->player->set_bonus(bonus_at_start);
PlayerStatus *currentStatus = get_player_status();
PlayerStatus *currentStatus = m_world_state.get_player_status();
currentStatus->coins = coins_at_start;
currentStatus->max_fire_bullets = max_fire_bullets_at_start;
currentStatus->max_ice_bullets = max_ice_bullets_at_start;
Expand Down Expand Up @@ -574,7 +575,7 @@ GameSession::start_sequence(const std::string& sequencename)
void
GameSession::drawstatus(DrawingContext& context)
{
player_status->draw(context);
m_world_state.get_player_status()->draw(context);

// draw level stats while end_sequence is running
if (end_sequence) {
Expand Down
18 changes: 9 additions & 9 deletions src/supertux/game_session.hpp
Expand Up @@ -27,13 +27,14 @@
#include "util/currenton.hpp"
#include "video/surface.hpp"

class CodeController;
class DrawingContext;
class Level;
class Menu;
class PlayerStatus;
class Sector;
class Statistics;
class PlayerStatus;
class DrawingContext;
class CodeController;
class Menu;
class WorldState;

/**
* Screen that runs a Level, where Players run and jump through Sectors.
Expand All @@ -42,7 +43,7 @@ class GameSession : public Screen,
public Currenton<GameSession>
{
public:
GameSession(const std::string& levelfile, PlayerStatus* player_status, Statistics* statistics = NULL);
GameSession(const std::string& levelfile, WorldState& world_state, Statistics* statistics = NULL);
~GameSession();

void record_demo(const std::string& filename);
Expand All @@ -69,9 +70,6 @@ class GameSession : public Screen,
Level* get_current_level()
{ return level.get(); }

PlayerStatus* get_player_status()
{ return player_status; }

void start_sequence(const std::string& sequencename);

/**
Expand All @@ -95,6 +93,8 @@ class GameSession : public Screen,
*/
void force_ghost_mode();

WorldState& get_world_state() { return m_world_state; }

private:
void check_end_conditions();
void process_events();
Expand Down Expand Up @@ -134,7 +134,7 @@ class GameSession : public Screen,
std::string newspawnpoint;

Statistics* best_level_statistics;
PlayerStatus* player_status;
WorldState& m_world_state;

std::ostream* capture_demo_stream;
std::string capture_file;
Expand Down
54 changes: 54 additions & 0 deletions src/supertux/levelset.cpp
@@ -0,0 +1,54 @@
// SuperTux
// Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
//
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "supertux/levelset.hpp"

#include <physfs.h>
#include <algorithm>

#include "util/log.hpp"
#include "util/string_util.hpp"

Levelset::Levelset(const std::string& basedir) :
m_basedir(basedir),
m_levels()
{
char** files = PHYSFS_enumerateFiles(m_basedir.c_str());
if (!files)
{
log_warning << "Couldn't read subset dir '" << m_basedir << "'" << std::endl;
return;
}

for(const char* const* filename = files; *filename != 0; ++filename)
{
if(StringUtil::has_suffix(*filename, ".stl"))
{
m_levels.push_back(*filename);
}
}
PHYSFS_freeList(files);

std::sort(m_levels.begin(), m_levels.end(), StringUtil::numeric_less);
}

int
Levelset::get_num_levels() const
{
return static_cast<int>(m_levels.size());
}

/* EOF */
41 changes: 41 additions & 0 deletions src/supertux/levelset.hpp
@@ -0,0 +1,41 @@
// SuperTux
// Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
//
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_SUPERTUX_LEVELSET_HPP
#define HEADER_SUPERTUX_SUPERTUX_LEVELSET_HPP

#include <string>
#include <vector>

class Levelset
{
private:
std::string m_basedir;
std::vector<std::string> m_levels;

public:
Levelset(const std::string& basedir);

int get_num_levels() const;

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

#endif

/* EOF */
8 changes: 4 additions & 4 deletions src/supertux/main.cpp
Expand Up @@ -358,7 +358,7 @@ Main::run(int argc, char** argv)

timelog(0);

const std::unique_ptr<PlayerStatus> default_playerstatus(new PlayerStatus());
const std::unique_ptr<WorldState> default_world_state(new WorldState);

GameManager game_manager;
g_screen_manager = new ScreenManager();
Expand All @@ -381,10 +381,10 @@ Main::run(int argc, char** argv)
g_config->start_level.compare(g_config->start_level.size() - 5, 5, ".stwm") == 0) {
g_screen_manager->push_screen(std::unique_ptr<Screen>(
new worldmap::WorldMap(
FileSystem::basename(g_config->start_level), default_playerstatus.get())));
FileSystem::basename(g_config->start_level), *default_world_state)));
} else {
std::unique_ptr<GameSession> session (
new GameSession(FileSystem::basename(g_config->start_level), default_playerstatus.get()));
new GameSession(FileSystem::basename(g_config->start_level), *default_world_state));

g_config->random_seed =session->get_demo_random_seed(g_config->start_demo);
init_rand();//initialise generator with seed from session
Expand All @@ -397,7 +397,7 @@ Main::run(int argc, char** argv)
g_screen_manager->push_screen(std::move(session));
}
} else {
g_screen_manager->push_screen(std::unique_ptr<Screen>(new TitleScreen(default_playerstatus.get())));
g_screen_manager->push_screen(std::unique_ptr<Screen>(new TitleScreen(*default_world_state)));
}

g_screen_manager->run(context);
Expand Down
4 changes: 3 additions & 1 deletion src/supertux/menu/contrib_menu.cpp
Expand Up @@ -54,10 +54,12 @@ ContribMenu::ContribMenu() :

if (!world->hide_from_contribs())
{
#ifdef GRUMBEL
world->load_state();
#endif

std::ostringstream title;
title << world->get_title() << " (" << world->get_num_solved_levels() << "/" << world->get_num_levels() << ")";
title << world->get_title(); // << " (" << world->get_num_solved_levels() << "/" << world->get_num_levels() << ")";
add_entry(i++, title.str());
m_contrib_worlds.push_back(std::move(world));
}
Expand Down
4 changes: 4 additions & 0 deletions src/supertux/menu/contrib_world_menu.cpp
Expand Up @@ -32,13 +32,15 @@ ContribWorldMenu::ContribWorldMenu(std::unique_ptr<World> world) :
add_label(m_world->get_title());
add_hl();

#ifdef GRUMBEL
for (int i = 0; i < m_world->get_num_levels(); ++i)
{
/** get level's title */
std::string filename = m_world->get_level_filename(i);
std::string title = GameManager::current()->get_level_name(filename);
add_entry(i, title);
}
#endif

add_hl();
add_back(_("Back"));
Expand All @@ -52,7 +54,9 @@ ContribWorldMenu::check_menu()
if (get_item_by_id(index).kind == MN_ACTION)
{
sound_manager->stop_music();
#ifdef GRUMBEL
GameManager::current()->start_level(std::move(m_world), index);
#endif
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/supertux/sector.cpp
Expand Up @@ -54,6 +54,7 @@
#include "supertux/level.hpp"
#include "supertux/object_factory.hpp"
#include "supertux/player_status.hpp"
#include "supertux/world_state.hpp"
#include "supertux/spawn_point.hpp"
#include "supertux/tile.hpp"
#include "trigger/sequence_trigger.hpp"
Expand Down Expand Up @@ -85,7 +86,7 @@ Sector::Sector(Level* parent) :
camera(0),
effect(0)
{
add_object(new Player(GameSession::current()->get_player_status(), "Tux"));
add_object(new Player(GameSession::current()->get_world_state().get_player_status(), "Tux"));
add_object(new DisplayEffect("Effect"));
add_object(new TextObject("Text"));

Expand Down
4 changes: 2 additions & 2 deletions src/supertux/title_screen.cpp
Expand Up @@ -40,14 +40,14 @@
#include <sstream>
#include <version.h>

TitleScreen::TitleScreen(PlayerStatus* player_status) :
TitleScreen::TitleScreen(WorldState& world_state) :
frame(),
controller(),
titlesession(),
copyright_text()
{
controller.reset(new CodeController());
titlesession.reset(new GameSession("levels/misc/menu.stl", player_status));
titlesession.reset(new GameSession("levels/misc/menu.stl", world_state));

Player* player = titlesession->get_current_sector()->player;
player->set_controller(controller.get());
Expand Down

0 comments on commit 8bd7a82

Please sign in to comment.