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