f86515da3fed06a8c30321ad0837f7329ad890f3
[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
43 /**
44  * This class is reponsible for drawing the level tiles
45  */
46 class TileMap : public GameObject, public Serializable, public ScriptInterface
47 {
48 public:
49   TileMap();
50   TileMap(const lisp::Lisp& reader, TileManager* tile_manager = 0);
51   TileMap(std::string name, int z_pos, bool solid_, size_t width_, size_t height_);
52   virtual ~TileMap();
53
54   virtual void write(lisp::Writer& writer);
55
56   virtual void update(float elapsed_time);
57   virtual void draw(DrawingContext& context);
58
59   /** Move tilemap until at given node, then stop */
60   void goto_node(int node_no);
61
62   /** Start moving tilemap */
63   void start_moving();
64
65   /** Stop tilemap at next node */
66   void stop_moving();
67   
68   virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
69   virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
70
71   void set(int width, int height, const std::vector<unsigned int>& vec,
72       int z_pos, bool solid);
73
74   /** resizes the tilemap to a new width and height (tries to not destroy the
75    * existing map)
76    */
77   void resize(int newwidth, int newheight);
78
79   size_t get_width() const
80   { return width; }
81
82   size_t get_height() const
83   { return height; }
84
85   float get_x_offset() const
86   { return x_offset; }
87
88   float get_y_offset() const
89   { return y_offset; }
90
91   void set_x_offset(float x_offset)
92   { this->x_offset = x_offset; }
93
94   void set_y_offset(float y_offset)
95   { this->y_offset = y_offset; }
96
97   int get_layer() const
98   { return z_pos; }
99
100   bool is_solid() const
101   { return solid; }
102
103   /**
104    * Changes Tilemap's solidity, i.e. whether to consider it when doing collision detection.
105    */
106   void set_solid(bool solid = true);
107
108   /// returns tile in row y and column y (of the tilemap)
109   const Tile* get_tile(int x, int y) const;
110   /// returns tile at position pos (in world coordinates)
111   const Tile* get_tile_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   TileManager* get_tilemanager() const
121   {
122     return tilemanager;
123   }
124
125   void set_drawing_effect(DrawingEffect effect)
126   {
127     drawing_effect = effect;
128   }
129
130   DrawingEffect get_drawing_effect()
131   {
132     return drawing_effect;
133   }
134
135   /**
136    * Start fading the tilemap to opacity given by @c alpha.
137    * Destination opacity will be reached after @c seconds seconds.
138    */
139   void fade(float alpha, float seconds = 0);
140
141 private:
142   typedef std::vector<uint32_t> Tiles;
143   Tiles tiles;
144
145 private:
146   TileManager* tilemanager;
147   bool solid;
148   float speed;
149   int width, height;
150   int z_pos;
151   float x_offset;
152   float y_offset;
153
154   DrawingEffect drawing_effect;
155   float alpha; /**< requested tilemap opacity */
156   float current_alpha; /**< current tilemap opacity */
157   float remaining_fade_time; /**< seconds until requested tilemap opacity is reached */
158   
159   std::auto_ptr<Path> path;
160   std::auto_ptr<PathWalker> walker;
161
162   DrawingContext::Target draw_target; /**< set to LIGHTMAP to draw to lightmap */
163 };
164
165 #endif /*SUPERTUX_TILEMAP_H*/