From: Christoph Sommer Date: Mon, 17 Apr 2006 14:53:41 +0000 (+0000) Subject: Removed a global variable X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=52de79ad8301a395a5a2999ecbdf31731c0b65f8;p=supertux.git Removed a global variable SVN-Revision: 3355 --- diff --git a/src/badguy/badguy.cpp b/src/badguy/badguy.cpp index d993c0fc4..b9ae2332a 100644 --- a/src/badguy/badguy.cpp +++ b/src/badguy/badguy.cpp @@ -214,7 +214,7 @@ BadGuy::kill_squished(Player& player) physic.set_velocity_y(0); set_state(STATE_SQUISHED); set_group(COLGROUP_MOVING_ONLY_STATIC); - global_stats.add_points(BADGUYS_KILLED_STAT, 1); + Sector::current()->get_level()->stats.add_points(BADGUYS_KILLED_STAT, 1); player.bounce(*this); } @@ -222,7 +222,7 @@ void BadGuy::kill_fall() { sound_manager->play("sounds/fall.wav", get_pos()); - global_stats.add_points(BADGUYS_KILLED_STAT, 1); + Sector::current()->get_level()->stats.add_points(BADGUYS_KILLED_STAT, 1); physic.set_velocity_y(0); physic.enable_gravity(true); set_state(STATE_FALLING); diff --git a/src/game_session.cpp b/src/game_session.cpp index 30e115edc..edac2d94d 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -116,9 +116,9 @@ GameSession::restart_level(bool fromBeginning) level.reset(new Level); level->load(levelfile); - global_stats.reset(); - global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins()); - global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys()); + level->stats.reset(); + level->stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins()); + level->stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys()); // get time int time = 0; @@ -136,7 +136,7 @@ GameSession::restart_level(bool fromBeginning) time += int(lt->get_level_time()); } } - global_stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time); + level->stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time); if (fromBeginning) reset_sector=""; if(reset_sector != "") { @@ -456,7 +456,7 @@ GameSession::finish(bool win) if(win) { if(WorldMap::current()) - WorldMap::current()->finished_level(levelfile); + WorldMap::current()->finished_level(level.get()); } main_loop->exit_screen(); @@ -562,7 +562,7 @@ GameSession::start_sequence(const std::string& sequencename) } } } - global_stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime))); + level->stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime))); if(sequencename == "fireworks") { currentsector->add_object(new Fireworks()); @@ -586,7 +586,7 @@ GameSession::drawstatus(DrawingContext& context) // draw level stats while end_sequence is running if (end_sequence) { - global_stats.draw_endseq_panel(context, best_level_statistics, statistics_backdrop.get()); + level->stats.draw_endseq_panel(context, best_level_statistics, statistics_backdrop.get()); } } diff --git a/src/level.cpp b/src/level.cpp index 1da7530ec..0517c6a6b 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -87,7 +87,7 @@ Level::load(const std::string& filepath) } else if(token == "author") { iter.value()->get(author); } else if(token == "sector") { - Sector* sector = new Sector; + Sector* sector = new Sector(this); sector->parse(*(iter.lisp())); add_sector(sector); } else { @@ -109,7 +109,7 @@ Level::load_old_format(const lisp::Lisp& reader) reader.get("name", name); reader.get("author", author); - Sector* sector = new Sector; + Sector* sector = new Sector(this); sector->parse_old_format(reader); add_sector(sector); } diff --git a/src/level.hpp b/src/level.hpp index 1f988c84f..f91ab3c23 100644 --- a/src/level.hpp +++ b/src/level.hpp @@ -23,6 +23,7 @@ #include #include +#include "statistics.hpp" class Sector; @@ -37,6 +38,7 @@ public: std::string author; typedef std::vector Sectors; Sectors sectors; + Statistics stats; public: Level(); diff --git a/src/object/coin.cpp b/src/object/coin.cpp index 84a6d5176..7fbebc7eb 100644 --- a/src/object/coin.cpp +++ b/src/object/coin.cpp @@ -68,7 +68,7 @@ Coin::collect() { Sector::current()->player->get_status()->add_coins(1); Sector::current()->add_object(new BouncyCoin(get_pos())); - global_stats.add_points(COINS_COLLECTED_STAT, 1); + Sector::current()->get_level()->stats.add_points(COINS_COLLECTED_STAT, 1); remove_me(); } diff --git a/src/sector.cpp b/src/sector.cpp index d92fc37c4..0d8bcc13b 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -70,8 +70,8 @@ Sector* Sector::_current = 0; bool Sector::show_collrects = false; bool Sector::draw_solids_only = false; -Sector::Sector() - : currentmusic(LEVEL_MUSIC), gravity(10), +Sector::Sector(Level* parent) + : level(parent), currentmusic(LEVEL_MUSIC), gravity(10), player(0), solids(0), camera(0) { add_object(new Player(player_status)); @@ -120,6 +120,12 @@ Sector::~Sector() delete *i; } +Level* +Sector::get_level() +{ + return level; +} + GameObject* Sector::parse_object(const std::string& name, const lisp::Lisp& reader) { diff --git a/src/sector.hpp b/src/sector.hpp index 9b72e7d1f..12a168293 100644 --- a/src/sector.hpp +++ b/src/sector.hpp @@ -28,6 +28,7 @@ #include "script_manager.hpp" #include "math/vector.hpp" #include "video/drawing_context.hpp" +#include "level.hpp" namespace lisp { class Lisp; @@ -60,9 +61,12 @@ enum MusicType { class Sector { public: - Sector(); + Sector(Level* parent); ~Sector(); + /// get parent level + Level* get_level(); + /// read sector from lisp file void parse(const lisp::Lisp& lisp); void parse_old_format(const lisp::Lisp& lisp); @@ -134,6 +138,7 @@ public: typedef std::vector SpawnPoints; private: + Level* level; /**< Parent level containing this sector */ uint32_t collision_tile_attributes(const Rect& dest) const; void before_object_remove(GameObject* object); diff --git a/src/statistics.cpp b/src/statistics.cpp index 98becba6f..4e32a1b7c 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -27,8 +27,6 @@ #include "main.hpp" #include "statistics.hpp" -Statistics global_stats; - std::string stat_name_to_string(int stat_enum) { diff --git a/src/statistics.hpp b/src/statistics.hpp index 2db4864d1..47c9e6c87 100644 --- a/src/statistics.hpp +++ b/src/statistics.hpp @@ -83,6 +83,4 @@ private: int display_stat; }; -extern Statistics global_stats; - #endif /*SUPERTUX_STATISTICS_H*/ diff --git a/src/worldmap/level.cpp b/src/worldmap/level.cpp index e6c7429a6..67a1ba91a 100644 --- a/src/worldmap/level.cpp +++ b/src/worldmap/level.cpp @@ -21,7 +21,7 @@ #include #include -#include "level.hpp" +#include "worldmap/level.hpp" #include "sprite/sprite_manager.hpp" #include "sprite/sprite.hpp" #include "video/drawing_context.hpp" @@ -29,7 +29,7 @@ namespace WorldMapNS { -Level::Level(const std::string& basedir, const lisp::Lisp* lisp) +LevelTile::LevelTile(const std::string& basedir, const lisp::Lisp* lisp) : solved(false), auto_path(true) { lisp->get("x", pos.x); @@ -50,18 +50,18 @@ Level::Level(const std::string& basedir, const lisp::Lisp* lisp) } } -Level::~Level() +LevelTile::~LevelTile() { } void -Level::draw(DrawingContext& context) +LevelTile::draw(DrawingContext& context) { sprite->draw(context, pos*32 + Vector(16, 16), LAYER_OBJECTS - 1); } void -Level::update(float ) +LevelTile::update(float ) { } diff --git a/src/worldmap/level.hpp b/src/worldmap/level.hpp index 8e79f3edd..13e09415b 100644 --- a/src/worldmap/level.hpp +++ b/src/worldmap/level.hpp @@ -17,8 +17,8 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#ifndef __WORLDMAP_LEVEL_HPP__ -#define __WORLDMAP_LEVEL_HPP__ +#ifndef __LEVEL_TILE_HPP__ +#define __LEVEL_TILE_HPP__ #include #include @@ -31,11 +31,11 @@ class Sprite; namespace WorldMapNS { -class Level : public GameObject +class LevelTile : public GameObject { public: - Level(const std::string& basedir, const lisp::Lisp* lisp); - virtual ~Level(); + LevelTile(const std::string& basedir, const lisp::Lisp* lisp); + virtual ~LevelTile(); virtual void draw(DrawingContext& context); virtual void update(float elapsed_time); diff --git a/src/worldmap/tux.cpp b/src/worldmap/tux.cpp index b660149df..de138089e 100644 --- a/src/worldmap/tux.cpp +++ b/src/worldmap/tux.cpp @@ -24,7 +24,7 @@ #include "video/drawing_context.hpp" #include "player_status.hpp" #include "worldmap.hpp" -#include "level.hpp" +#include "worldmap/level.hpp" #include "special_tile.hpp" #include "sprite_change.hpp" #include "control/joystickkeyboardcontroller.hpp" @@ -124,7 +124,7 @@ Tux::tryStartWalking() if (input_direction == D_NONE) return; - Level* level = worldmap->at_level(); + LevelTile* level = worldmap->at_level(); // We got a new direction, so lets start walking when possible Vector next_tile; diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index 39c2526f3..60158b856 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -211,7 +211,7 @@ WorldMap::load(const std::string& filename) SpawnPoint* sp = new SpawnPoint(iter.lisp()); spawn_points.push_back(sp); } else if(iter.item() == "level") { - Level* level = new Level(levels_path, iter.lisp()); + LevelTile* level = new LevelTile(levels_path, iter.lisp()); levels.push_back(level); game_objects.push_back(level); } else if(iter.item() == "special-tile") { @@ -251,7 +251,7 @@ WorldMap::load(const std::string& filename) } void -WorldMap::get_level_title(Level& level) +WorldMap::get_level_title(LevelTile& level) { /** get special_tile's title */ level.title = ""; @@ -274,8 +274,8 @@ WorldMap::get_level_title(Level& level) void WorldMap::calculate_total_stats() { total_stats.reset(); - for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { - Level* level = *i; + for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { + LevelTile* level = *i; if (level->solved) { total_stats += level->statistics; } @@ -354,18 +354,17 @@ WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos) } void -WorldMap::finished_level(const std::string& filename) +WorldMap::finished_level(Level* gamelevel) { - // TODO calculate level from filename? - (void) filename; - Level* level = at_level(); + // TODO use Level* parameter here? + LevelTile* level = at_level(); bool old_level_state = level->solved; level->solved = true; level->sprite->set_action("solved"); // deal with statistics - level->statistics.merge(global_stats); + level->statistics.merge(gamelevel->stats); calculate_total_stats(); save_state(); @@ -496,7 +495,7 @@ WorldMap::update(float delta) } /* Check level action */ - Level* level = at_level(); + LevelTile* level = at_level(); if (!level) { log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl; return; @@ -527,11 +526,11 @@ WorldMap::at(Vector p) return solids->get_tile((int) p.x, (int) p.y); } -Level* +LevelTile* WorldMap::at_level() { - for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { - Level* level = *i; + for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { + LevelTile* level = *i; if (level->pos == tux->get_tile_pos()) return level; } @@ -590,8 +589,8 @@ WorldMap::draw_status(DrawingContext& context) player_status->draw(context); if (!tux->is_moving()) { - for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { - Level* level = *i; + for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { + LevelTile* level = *i; if (level->pos == tux->get_tile_pos()) { if(level->title == "") @@ -784,8 +783,8 @@ WorldMap::save_state() sq_pushstring(vm, "levels", -1); sq_newtable(vm); - for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { - Level* level = *i; + for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { + LevelTile* level = *i; if (level->solved) { sq_pushstring(vm, level->name.c_str(), -1); @@ -852,8 +851,8 @@ WorldMap::load_state() if(SQ_FAILED(sq_get(vm, -2))) throw Scripting::SquirrelError(vm, "Couldn't get levels"); - for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { - Level* level = *i; + for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { + LevelTile* level = *i; sq_pushstring(vm, level->name.c_str(), -1); if(SQ_SUCCEEDED(sq_get(vm, -2))) { level->solved = read_bool(vm, "solved"); @@ -880,8 +879,8 @@ size_t WorldMap::solved_level_count() { size_t count = 0; - for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) { - Level* level = *i; + for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { + LevelTile* level = *i; if(level->solved) count++; diff --git a/src/worldmap/worldmap.hpp b/src/worldmap/worldmap.hpp index 9a089cb0d..e8f651aed 100644 --- a/src/worldmap/worldmap.hpp +++ b/src/worldmap/worldmap.hpp @@ -33,6 +33,7 @@ #include "tile_manager.hpp" #include "game_object.hpp" #include "console.hpp" +#include "../level.hpp" class Sprite; class Menu; @@ -43,7 +44,7 @@ class TileMap; namespace WorldMapNS { class Tux; -class Level; +class LevelTile; class SpecialTile; class SpriteChange; @@ -96,8 +97,8 @@ private: typedef std::vector SpecialTiles; SpecialTiles special_tiles; - typedef std::vector Levels; - Levels levels; + typedef std::vector LevelTiles; + LevelTiles levels; typedef std::vector SpriteChanges; SpriteChanges sprite_changes; typedef std::vector SpawnPoints; @@ -131,9 +132,9 @@ public: * gets called from the GameSession when a level has been successfully * finished */ - void finished_level(const std::string& filename); + void finished_level(Level* level); - Level* at_level(); + LevelTile* at_level(); SpecialTile* at_special_tile(); SpriteChange* at_sprite_change(const Vector& pos); @@ -155,7 +156,7 @@ public: { return name; } private: - void get_level_title(Level& level); + void get_level_title(LevelTile& level); void draw_status(DrawingContext& context); void calculate_total_stats();