enemies fall down again, small improvements to tile manager
[supertux.git] / src / tile.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <cmath>
23 #include <cassert>
24 #include <iostream>
25 #include <stdexcept>
26
27 #include "app/globals.h"
28 #include "tile.h"
29 #include "scene.h"
30 #include "utils/lispreader.h"
31 #include "math/vector.h"
32 #include "video/drawing_context.h"
33
34 /** Dirty little helper to create a surface from a snipped of lisp:
35  *
36  *  "filename"
37  *  (region "filename" x y w h)
38  */
39 static
40 Surface* create_surface(lisp_object_t* cur)
41 {
42   if (lisp_string_p(cur))
43     {
44       return new Surface(datadir + "/images/tilesets/" + lisp_string(cur),
45                          true);
46     }
47   else if (lisp_cons_p(cur) && lisp_symbol_p(lisp_car(cur)))
48     {
49       lisp_object_t* sym  = lisp_car(cur);
50       lisp_object_t* data = lisp_cdr(cur);
51       
52       if (strcmp(lisp_symbol(sym), "region") == 0)
53         {
54           if (lisp_list_length(data) == 5) // (image-region filename x y w h)
55             {
56               return new Surface(datadir + "/images/tilesets/" + lisp_string(lisp_car(data)), 
57                                  lisp_integer(lisp_list_nth(data, 1)),
58                                  lisp_integer(lisp_list_nth(data, 2)),
59                                  lisp_integer(lisp_list_nth(data, 3)),
60                                  lisp_integer(lisp_list_nth(data, 4)),
61                                  true);
62             }
63           else
64             {
65               std::cout << "Tile: Type mispatch, should be '(region \"somestring\" x y w h)'" << std::endl;
66               return 0;
67             }
68         }
69       else
70         {
71           std::cout << "Tile: Unhandled tag: " << lisp_symbol(sym) << std::endl;
72           return 0;
73         }
74     }
75
76   std::cout << "Tile: unhandled element" << std::endl;
77   return 0;  
78 }
79
80 /** Create a vector of surfaces (aka Sprite) from a piece of lisp:
81     ((image "bla.png") (image-region "bla.png") ...)
82  */
83 static 
84 std::vector<Surface*> create_surfaces(lisp_object_t* cur)
85 {
86   std::vector<Surface*> surfs;
87
88   while(cur)
89     {
90       Surface* surface = create_surface(lisp_car(cur));
91       if (surface)
92         surfs.push_back(surface); 
93       else
94         std::cout << "Tile: Couldn't create image" << std::endl;
95         
96       cur = lisp_cdr(cur);
97     }
98   
99   return surfs;
100 }
101
102 Tile::Tile()
103   : id(0), attributes(0), data(0), next_tile(0), anim_speed(25)
104 {
105 }
106
107 Tile::~Tile()
108 {
109   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
110       ++i) {
111     delete *i;
112   }
113   for(std::vector<Surface*>::iterator i = editor_images.begin();
114       i != editor_images.end(); ++i) {
115     delete *i;                                                                
116   }
117 }
118
119 void
120 Tile::read(LispReader& reader)
121 {
122   if(!reader.read_uint("id", id)) {
123     throw std::runtime_error("Missing tile-id.");
124   }
125   
126   bool value;
127   if(reader.read_bool("solid", value) && value)
128     attributes |= SOLID;
129   if(reader.read_bool("unisolid", value) && value)
130     attributes |= UNISOLID | SOLID;
131   if(reader.read_bool("brick", value) && value)
132     attributes |= BRICK;
133   if(reader.read_bool("ice", value) && value)
134     attributes |= ICE;
135   if(reader.read_bool("water", value) && value)
136     attributes |= WATER;
137   if(reader.read_bool("spike", value) && value)
138     attributes |= SPIKE;
139   if(reader.read_bool("fullbox", value) && value)
140     attributes |= FULLBOX;
141   if(reader.read_bool("distro", value) && value)
142     attributes |= COIN;
143   if(reader.read_bool("coin", value) && value)
144     attributes |= COIN;
145   if(reader.read_bool("goal", value) && value)
146     attributes |= GOAL;
147
148   reader.read_int("data", data);
149   reader.read_int("anim-speed", anim_speed);
150   reader.read_int("next-tile", next_tile);
151
152   if(reader.read_int("slope-type", data)) {
153     attributes |= SOLID | SLOPE;
154   }
155
156   images        = create_surfaces(reader.read_lisp("images"));
157   editor_images = create_surfaces(reader.read_lisp("editor-images"));
158 }
159
160 void
161 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
162 {
163   if(images.size() > 1) {
164     size_t frame = ((global_frame_counter*25) / anim_speed) % images.size();
165     context.draw_surface(images[frame], pos, layer);
166   } else if (images.size() == 1) {
167     context.draw_surface(images[0], pos, layer);
168   }
169 }
170