9817493099b25028480c9a9c3f1ad0d6be90a402
[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
32 namespace lisp {
33 class Lisp;
34 }
35
36 class Level;
37 class TileManager;
38 class Tile;
39
40 /**
41  * This class is reponsible for drawing the level tiles
42  */
43 class TileMap : public GameObject, public Serializable
44 {
45 public:
46   TileMap();
47   TileMap(const lisp::Lisp& reader, TileManager* tile_manager = 0);
48   TileMap(std::string name, int z_pos, bool solid_, size_t width_, size_t height_);
49   virtual ~TileMap();
50
51   virtual void write(lisp::Writer& writer);
52
53   virtual void update(float elapsed_time);
54   virtual void draw(DrawingContext& context);
55
56   void set(int width, int height, const std::vector<unsigned int>& vec,
57       int z_pos, bool solid);
58
59   /** resizes the tilemap to a new width and height (tries to not destroy the
60    * existing map)
61    */
62   void resize(int newwidth, int newheight);
63
64   size_t get_width() const
65   { return width; }
66
67   size_t get_height() const
68   { return height; }
69   
70   float get_x_offset() const
71   { return x_offset; }
72   
73   float get_y_offset() const
74   { return y_offset; }
75
76   void set_x_offset(float x_offset)
77   { this->x_offset = x_offset; }
78
79   void set_y_offset(float y_offset)
80   { this->y_offset = y_offset; }
81
82   int get_layer() const
83   { return z_pos; }
84   
85   bool is_solid() const
86   { return solid; }
87
88   /// returns tile in row y and column y (of the tilemap)
89   const Tile* get_tile(int x, int y) const;
90   /// returns tile at position pos (in world coordinates)
91   const Tile* get_tile_at(const Vector& pos) const;
92
93   void change(int x, int y, uint32_t newtile);
94
95   void change_at(const Vector& pos, uint32_t newtile);
96
97   /// changes all tiles with the given ID
98   void change_all(uint32_t oldtile, uint32_t newtile);
99
100   TileManager* get_tilemanager() const
101   {
102     return tilemanager;
103   }
104
105   void set_drawing_effect(DrawingEffect effect)
106   {
107     drawing_effect = effect;
108   }
109
110   DrawingEffect get_drawing_effect()
111   {
112     return drawing_effect;
113   }
114
115 private:
116   typedef std::vector<uint32_t> Tiles;
117   Tiles tiles;
118   
119 private:
120   TileManager* tilemanager;
121   std::string name;
122   bool solid;
123   float speed;
124   int width, height;
125   int z_pos;
126   float x_offset;
127   float y_offset;
128
129   DrawingEffect drawing_effect;
130 };
131
132 #endif /*SUPERTUX_TILEMAP_H*/
133