more fixes
[supertux.git] / src / tilemap.h
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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
25 #include "special/game_object.h"
26 #include "serializable.h"
27 #include "math/vector.h"
28
29 using namespace SuperTux;
30
31 namespace SuperTux {
32 class LispReader;
33 }
34
35 class Level;
36 class TileManager;
37 class Tile;
38
39 struct TileId
40 {
41   TileId() : id(0), hidden(0) {}
42   explicit TileId(unsigned int i, bool hidden_ = false) : id(i), hidden(hidden_) {}
43
44   unsigned id     :31;
45   unsigned hidden :1;
46 };
47
48 /**
49  * This class is reponsible for drawing the level tiles
50  */
51 class TileMap : public GameObject, public Serializable
52 {
53 public:
54   TileMap();
55   TileMap(LispReader& reader);
56   TileMap(int layer_, bool solid_, size_t width_, size_t height_);
57   virtual ~TileMap();
58
59   virtual void write(LispWriter& writer);
60
61   virtual void action(float elapsed_time);
62   virtual void draw(DrawingContext& context);
63
64   void set(int width, int height, const std::vector<unsigned int>& vec,
65       int layer, bool solid);
66
67   /** resizes the tilemap to a new width and height (tries to not destroy the
68    * existing map)
69    */
70   void resize(int newwidth, int newheight);
71
72   /** Flip the all tile map vertically. The purpose of this is to let
73       player to play the same level in a different way :) */
74   void do_vertical_flip();
75
76   size_t get_width() const
77   { return width; }
78
79   size_t get_height() const
80   { return height; }
81
82   int get_layer() const
83   { return layer; }
84   
85   bool is_solid() const
86   { return solid; }
87
88   TileId& get_tile_id_at(const Vector& pos);
89
90   /// returns tile in row y and column y (of the tilemap)
91   Tile* get_tile(int x, int y) const;
92   /// returns tile at position pos (in world coordinates)
93   Tile* get_tile_at(const Vector& pos) const;
94
95   void change(int x, int y, unsigned int newtile);
96
97   void change_at(const Vector& pos, unsigned int newtile);
98
99 private:
100   std::vector<TileId> tiles;
101   
102 private:
103   TileManager* tilemanager;
104   bool solid;
105   float speed;
106   int width, height;
107   int layer;
108
109   bool vertical_flip;
110 };
111
112 #endif /*SUPERTUX_TILEMAP_H*/
113