- Added a new script command to display text files
[supertux.git] / src / worldmap.h
index eab9e7e..fa71bbc 100644 (file)
 //  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 SUPERTUX_WORLDMAP_H
 #define SUPERTUX_WORLDMAP_H
 
 #include <vector>
 #include <string>
 
-#include <SDL_mixer.h>
+#include "math/vector.h"
+#include "audio/musicref.h"
+#include "video/screen.h"
+#include "lisp/lisp.h"
+#include "control/controller.h"
+#include "statistics.h"
+#include "timer.h"
+#include "tile_manager.h"
+#include "game_object.h"
+
+class Sprite;
+class Menu;
+class SpawnPoint;
+class GameObject;
+class TileMap;
+extern Menu* worldmap_menu;
 
 namespace WorldMapNS {
 
-struct Point
-{
-  Point() : x(0), y(0) {}
-
-  Point(const Point& pos)
-    : x(pos.x), y(pos.y) {}
-
-  Point& operator=(const Point& pos)
-  { x = pos.x;
-    y = pos.y; 
-    return *this; }
-
-  Point(int x_, int y_)
-    : x(x_), y(y_) {}
-
-  int x;
-  int y;
+enum WorldMapMenuIDs {
+  MNID_RETURNWORLDMAP,
+  MNID_QUITWORLDMAP
 };
 
-struct Tile
-{
-  Surface* sprite;
-
-  // Directions in which Tux is allowed to walk from this tile
-  bool north;
-  bool east;
-  bool south;
-  bool west;
-
-  /** Stop on this tile or walk over it? */
-  bool stop;
+// For one way tiles
+enum {
+  BOTH_WAYS,
+  NORTH_SOUTH_WAY,
+  SOUTH_NORTH_WAY,
+  EAST_WEST_WAY,
+  WEST_EAST_WAY
 };
 
-class TileManager
-{
-private:
-  typedef std::vector<Tile*> Tiles;
-  Tiles tiles;
-  static TileManager* instance_ ;
-
- TileManager();
-public:
-  static TileManager* instance() { return instance_ ? instance_ : instance_ = new TileManager(); }
-
-  void load();
-  Tile* get(int i);
-};
-
-enum Direction { NONE, WEST, EAST, NORTH, SOUTH };
+enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
 
 std::string direction_to_string(Direction d);
 Direction   string_to_direction(const std::string& d);
@@ -83,17 +63,18 @@ Direction reverse_dir(Direction d);
 
 class WorldMap;
 
-class Tux
+class Tux : public GameObject
 {
 public:
   Direction back_direction;
 private:
   WorldMap* worldmap;
-  Surface* sprite;
+  Sprite* tux_sprite;
+  Controller* controller;
 
   Direction input_direction;
   Direction direction;
-  Point tile_pos;
+  Vector tile_pos;
   /** Length by which tux is away from its current tile, length is in
       input_direction direction */
   float offset;
@@ -102,16 +83,17 @@ private:
   void stop();
 public: 
   Tux(WorldMap* worldmap_);
+  ~Tux();
   
-  void draw(const Point& offset);
-  void update(float delta);
+  void draw(DrawingContext& context);
+  void update(float elapsed_time);
 
-  void set_direction(Direction d) { input_direction = d; }
+  void set_direction(Direction dir);
 
   bool is_moving() const { return moving; }
-  Point get_pos();
-  Point get_tile_pos() const { return tile_pos; } 
-  void  set_tile_pos(Point p) { tile_pos = p; } 
+  Vector get_pos();
+  Vector get_tile_pos() const { return tile_pos; } 
+  void  set_tile_pos(Vector p) { tile_pos = p; } 
 };
 
 /** */
@@ -122,26 +104,72 @@ private:
 
   bool quit;
 
-  Surface* level_sprite;
   Surface* leveldot_green;
   Surface* leveldot_red;
+  Surface* messagedot;
+  Surface* teleporterdot;
 
   std::string name;
   std::string music;
 
-  std::vector<int> tilemap;
-  int width;
-  int height;
+  typedef std::vector<GameObject*> GameObjects;
+  GameObjects game_objects;
+  TileMap* solids;
+  
+  TileManager* tile_manager;
 
 public:
+  struct SpecialTile
+  {
+    Vector pos;
+
+    /** Optional flags: */
+
+    /** Position to swap to player */
+    Vector teleport_dest;
+
+    /** Message to show in the Map */
+    std::string map_message;
+    bool passive_message;
+
+    /** Hide special tile */
+    bool invisible;
+
+    /** Only applies actions (ie. passive messages) when going to that direction */
+    bool apply_action_north;
+    bool apply_action_east;
+    bool apply_action_south;
+    bool apply_action_west;
+  };
+
   struct Level
   {
-    int x;
-    int y;
+    Vector pos;
+
     std::string name;
     std::string title;
     bool solved;
 
+    /** Statistics for level tiles */
+    Statistics statistics;
+
+    /** Optional flags: */
+
+    /** Check if this level should be vertically flipped */
+    bool vertical_flip;
+
+    /** Script that is run when the level is successfully finished */
+    std::string extro_script;
+
+    /** Go to this world */
+    std::string next_worldmap;
+
+    /** Quit the worldmap */
+    bool quit_worldmap;
+
+    /** If false, disables the auto walking after finishing a level */
+    bool auto_path;
+
     // Directions which are walkable from this level
     bool north;
     bool east;
@@ -149,21 +177,38 @@ public:
     bool west;
   };
 
+  /** Variables to deal with the passive map messages */
+  Timer passive_message_timer;
+  std::string passive_message;
+
 private:
+  std::string map_filename;
+  std::string levels_path;
+
+  typedef std::vector<SpecialTile> SpecialTiles;
+  SpecialTiles special_tiles;
   typedef std::vector<Level> Levels;
   Levels levels;
+  typedef std::vector<SpawnPoint*> SpawnPoints;
+  SpawnPoints spawn_points;
 
-  Mix_Music* song;
-
-  Direction input_direction;
-  bool enter_level;
+  MusicRef song;
 
-  Point offset;
+  Vector offset;
   std::string savegame_file;
+  
+  std::string intro_script;
+  bool intro_displayed;
+
+  void get_level_title(Level& level);
+
+  void draw_status(DrawingContext& context);
 
-  void get_level_title(Levels::pointer level);
+  // to avoid calculating total stats all the time. This way only
+  // when need, it is calculated.
+  Statistics total_stats;
+  void calculate_total_stats();
 
-  void draw_status();
 public:
   WorldMap();
   ~WorldMap();
@@ -175,24 +220,43 @@ public:
   
   void get_input();
 
+  void add_object(GameObject* object);
+  void clear_objects();
+
   /** Update Tux position */
-  void update();
+  void update(float delta);
 
   /** Draw one frame */
-  void draw(const Point& offset);
+  void draw(DrawingContext& context);
+
+  Vector get_next_tile(Vector pos, Direction direction);
+  const Tile* at(Vector pos);
 
-  Point get_next_tile(Point pos, Direction direction);
-  Tile* at(Point pos);
   WorldMap::Level* at_level();
+  WorldMap::SpecialTile* at_special_tile();
 
   /** Check if it is possible to walk from \a pos into \a direction,
       if possible, write the new position to \a new_pos */
-  bool path_ok(Direction direction, Point pos, Point* new_pos);
+  bool path_ok(Direction direction, Vector pos, Vector* new_pos);
 
+  /* Save map to slot */
   void savegame(const std::string& filename);
+  /* Load map from slot
+     You should call set_map_filename() before this */
   void loadgame(const std::string& filename);
+  /* Load map directly from file */
+  void loadmap(const std::string& filename);
+
+  const std::string& get_world_title() const
+  { return name; }
+    
+  void set_map_filename(std::string filename)
+  { map_filename = filename; }
+
 private:
   void on_escape_press();
+  void parse_special_tile(const lisp::Lisp* lisp);
+  void parse_level_tile(const lisp::Lisp* lisp);
 };
 
 } // namespace WorldMapNS