42334bceb5bb8d5b6b6a397431882d0207fbc6de
[supertux.git] / src / object / tilemap.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_TILEMAP_H
21 #define SUPERTUX_TILEMAP_H
22
23 #include <vector>
24 #include <stdint.h>
25 #include <string>
26
27 #include "game_object.hpp"
28 #include "serializable.hpp"
29 #include "math/vector.hpp"
30 #include "video/drawing_context.hpp"
31 #include "object/path.hpp"
32 #include "object/path_walker.hpp"
33 #include "script_interface.hpp"
34
35 namespace lisp {
36 class Lisp;
37 }
38
39 class Level;
40 class TileManager;
41 class Tile;
42 class TileSet;
43
44 /**
45  * This class is responsible for drawing the level tiles
46  */
47 class TileMap : public GameObject, public Serializable, public ScriptInterface
48 {
49 public:
50   TileMap(const TileSet *tileset);
51   TileMap(const lisp::Lisp& reader);
52   TileMap(const TileSet *tileset, std::string name, int z_pos, bool solid_,
53           size_t width_, size_t height_);
54   virtual ~TileMap();
55
56   virtual void write(lisp::Writer& writer);
57
58   virtual void update(float elapsed_time);
59   virtual void draw(DrawingContext& context);
60
61   /** Move tilemap until at given node, then stop */
62   void goto_node(int node_no);
63
64   /** Start moving tilemap */
65   void start_moving();
66
67   /** Stop tilemap at next node */
68   void stop_moving();
69
70   virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
71   virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
72
73   void set(int width, int height, const std::vector<unsigned int>& vec,
74       int z_pos, bool solid);
75
76   /** resizes the tilemap to a new width and height (tries to not destroy the
77    * existing map)
78    */
79   void resize(int newwidth, int newheight, int fill_id = 0);
80
81   size_t get_width() const
82   { return width; }
83
84   size_t get_height() const
85   { return height; }
86
87   float get_x_offset() const
88   { return x_offset; }
89
90   float get_y_offset() const
91   { return y_offset; }
92
93   const Vector& get_movement() const
94   {
95     return movement;
96   }
97
98   void set_x_offset(float x_offset)
99   { this->x_offset = x_offset; }
100
101   void set_y_offset(float y_offset)
102   { this->y_offset = y_offset; }
103
104   int get_layer() const
105   { return z_pos; }
106
107   bool is_solid() const
108   { return solid; }
109
110   /**
111    * Changes Tilemap's solidity, i.e. whether to consider it when doing collision detection.
112    */
113   void set_solid(bool solid = true);
114
115   /// returns tile in row y and column y (of the tilemap)
116   const Tile* get_tile(int x, int y) const;
117   /// returns tile at position pos (in world coordinates)
118   const Tile* get_tile_at(const Vector& pos) const;
119   /// returns tile in row y and column y (of the tilemap)
120   uint32_t get_tile_id(int x, int y) const;
121   /// returns tile at position pos (in world coordinates)
122   uint32_t get_tile_id_at(const Vector& pos) const;
123
124   void change(int x, int y, uint32_t newtile);
125
126   void change_at(const Vector& pos, uint32_t newtile);
127
128   /// changes all tiles with the given ID
129   void change_all(uint32_t oldtile, uint32_t newtile);
130
131   void set_drawing_effect(DrawingEffect effect)
132   {
133     drawing_effect = effect;
134   }
135
136   DrawingEffect get_drawing_effect()
137   {
138     return drawing_effect;
139   }
140
141   /**
142    * Start fading the tilemap to opacity given by @c alpha.
143    * Destination opacity will be reached after @c seconds seconds. Also influences solidity.
144    */
145   void fade(float alpha, float seconds = 0);
146
147   /**
148    * Instantly switch tilemap's opacity to @c alpha. Also influences solidity.
149    */
150   void set_alpha(float alpha);
151
152   /**
153    * Return tilemap's opacity. Note that while the tilemap is fading in or out, this will return the current alpha value, not the target alpha.
154    */
155   float get_alpha();
156
157 private:
158   const TileSet *tileset;
159
160   typedef std::vector<uint32_t> Tiles;
161   Tiles tiles;
162
163   bool solid;
164   float speed_x;
165   float speed_y;
166   int width, height;
167   int z_pos;
168   float x_offset;
169   float y_offset;
170   Vector movement; /**< The movement that happened last frame */
171
172   DrawingEffect drawing_effect;
173   float alpha; /**< requested tilemap opacity */
174   float current_alpha; /**< current tilemap opacity */
175   float remaining_fade_time; /**< seconds until requested tilemap opacity is reached */
176
177   std::auto_ptr<Path> path;
178   std::auto_ptr<PathWalker> walker;
179
180   DrawingContext::Target draw_target; /**< set to LIGHTMAP to draw to lightmap */
181 };
182
183 #endif /*SUPERTUX_TILEMAP_H*/