Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / worldmap / tux.cpp
1 //  SuperTux -  A Jump'n Run
2 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
3 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "control/joystickkeyboardcontroller.hpp"
19 #include "scripting/squirrel_util.hpp"
20 #include "sprite/sprite.hpp"
21 #include "sprite/sprite_manager.hpp"
22 #include "supertux/main.hpp"
23 #include "supertux/player_status.hpp"
24 #include "supertux/tile.hpp"
25 #include "worldmap/level.hpp"
26 #include "worldmap/tux.hpp"
27
28 namespace WorldMapNS {
29
30 static const float TUXSPEED = 200;
31 static const float map_message_TIME = 2.8f;
32
33 Tux::Tux(WorldMap* worldmap_) :
34   back_direction(),
35   worldmap(worldmap_),
36   sprite(),
37   controller(),
38   input_direction(),
39   direction(),
40   tile_pos(),
41   offset(),
42   moving(),
43   ghost_mode()
44 {
45   sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
46
47   offset = 0;
48   moving = false;
49   direction = D_NONE;
50   input_direction = D_NONE;
51
52   ghost_mode = false;
53 }
54
55 Tux::~Tux()
56 {
57 }
58
59 void
60 Tux::draw(DrawingContext& context)
61 {
62   switch (player_status->bonus) {
63     case GROWUP_BONUS:
64       sprite->set_action(moving ? "large-walking" : "large-stop");
65       break;
66     case FIRE_BONUS:
67       sprite->set_action(moving ? "fire-walking" : "fire-stop");
68       break;
69     case NO_BONUS:
70       sprite->set_action(moving ? "small-walking" : "small-stop");
71       break;
72     default:
73       log_debug << "Bonus type not handled in worldmap." << std::endl;
74       sprite->set_action("large-stop");
75       break;
76   }
77
78   sprite->draw(context, get_pos(), LAYER_OBJECTS);
79 }
80
81 Vector
82 Tux::get_pos()
83 {
84   float x = tile_pos.x * 32;
85   float y = tile_pos.y * 32;
86
87   switch(direction)
88   {
89     case D_WEST:
90       x -= offset - 32;
91       break;
92     case D_EAST:
93       x += offset - 32;
94       break;
95     case D_NORTH:
96       y -= offset - 32;
97       break;
98     case D_SOUTH:
99       y += offset - 32;
100       break;
101     case D_NONE:
102       break;
103   }
104
105   return Vector(x, y);
106 }
107
108 void
109 Tux::stop()
110 {
111   offset = 0;
112   direction = D_NONE;
113   input_direction = D_NONE;
114   moving = false;
115 }
116
117 void
118 Tux::set_direction(Direction dir)
119 {
120   input_direction = dir;
121 }
122
123 void
124 Tux::set_ghost_mode(bool enabled)
125 {
126   ghost_mode = enabled;
127 }
128
129 bool
130 Tux::get_ghost_mode()
131 {
132   return ghost_mode;
133 }
134
135 void
136 Tux::tryStartWalking()
137 {
138   if (moving)
139     return;
140   if (input_direction == D_NONE)
141     return;
142
143   LevelTile* level = worldmap->at_level();
144
145   // We got a new direction, so lets start walking when possible
146   Vector next_tile;
147   if ((!level || level->solved)
148       && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
149     tile_pos = next_tile;
150     moving = true;
151     direction = input_direction;
152     back_direction = reverse_dir(direction);
153   } else if (ghost_mode || (input_direction == back_direction)) {
154     moving = true;
155     direction = input_direction;
156     tile_pos = worldmap->get_next_tile(tile_pos, direction);
157     back_direction = reverse_dir(direction);
158   }
159 }
160
161 bool
162 Tux::canWalk(int tile_data, Direction dir)
163 {
164   return ghost_mode || 
165     ((tile_data & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
166      (tile_data & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
167      (tile_data & Tile::WORLDMAP_EAST  && dir == D_EAST) ||
168      (tile_data & Tile::WORLDMAP_WEST  && dir == D_WEST));
169 }
170
171 void
172 Tux::tryContinueWalking(float elapsed_time)
173 {
174   if (!moving)
175     return;
176
177   // Let tux walk
178   offset += TUXSPEED * elapsed_time;
179
180   // Do nothing if we have not yet reached the next tile
181   if (offset <= 32)
182     return;
183
184   offset -= 32;
185
186   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
187   if(sprite_change != NULL) {
188     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
189     sprite_change->clear_stay_action();
190   }
191
192   // if this is a special_tile with passive_message, display it
193   SpecialTile* special_tile = worldmap->at_special_tile();
194   if(special_tile)
195   {
196     // direction and the apply_action_ are opposites, since they "see"
197     // directions in a different way
198     if((direction == D_NORTH && special_tile->apply_action_south) ||
199        (direction == D_SOUTH && special_tile->apply_action_north) ||
200        (direction == D_WEST && special_tile->apply_action_east) ||
201        (direction == D_EAST && special_tile->apply_action_west))
202     {
203       if(special_tile->passive_message) {
204         worldmap->passive_message = special_tile->map_message;
205         worldmap->passive_message_timer.start(map_message_TIME);
206       } else if(special_tile->script != "") {
207         try {
208           std::istringstream in(special_tile->script);
209           worldmap->run_script(in, "specialtile");
210         } catch(std::exception& e) {
211           log_warning << "Couldn't execute special tile script: " << e.what()
212                       << std::endl;
213         }
214       }
215     }
216   }
217
218   // check if we are at a Teleporter
219   Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
220
221   // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
222   if ((worldmap->at_level())
223       || (worldmap->tile_data_at(tile_pos) & Tile::WORLDMAP_STOP)
224       || (special_tile && !special_tile->passive_message
225           && special_tile->script == "")
226       || (teleporter) || ghost_mode) {
227     if(special_tile && !special_tile->map_message.empty()
228        && !special_tile->passive_message)
229       worldmap->passive_message_timer.start(0);
230     stop();
231     return;
232   }
233
234   // if user wants to change direction, try changing, else guess the direction in which to walk next
235   const int tile_data = worldmap->tile_data_at(tile_pos);
236   if ((direction != input_direction) && canWalk(tile_data, input_direction)) {
237     direction = input_direction;
238     back_direction = reverse_dir(direction);
239   } else {
240     Direction dir = D_NONE;
241     if (tile_data & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
242       dir = D_NORTH;
243     else if (tile_data & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
244       dir = D_SOUTH;
245     else if (tile_data & Tile::WORLDMAP_EAST && back_direction != D_EAST)
246       dir = D_EAST;
247     else if (tile_data & Tile::WORLDMAP_WEST && back_direction != D_WEST)
248       dir = D_WEST;
249
250     if (dir == D_NONE) {
251       // Should never be reached if tiledata is good
252       log_warning << "Could not determine where to walk next" << std::endl;
253       stop();
254       return;
255     }
256
257     direction = dir;
258     input_direction = direction;
259     back_direction = reverse_dir(direction);
260   }
261
262   // Walk automatically to the next tile
263   if(direction == D_NONE)
264     return;
265
266   Vector next_tile;
267   if (!ghost_mode && !worldmap->path_ok(direction, tile_pos, &next_tile)) {
268     log_debug << "Tilemap data is buggy" << std::endl;
269     stop();
270     return;
271   }
272
273   SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
274   if(next_sprite != NULL && next_sprite->change_on_touch) {
275     sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
276     next_sprite->clear_stay_action();
277   }
278   SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
279   if(last_sprite != NULL && next_sprite != NULL) {
280     log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
281     last_sprite->set_stay_action();
282   }
283
284   tile_pos = next_tile;
285 }
286
287 void
288 Tux::updateInputDirection()
289 {
290   if(g_main_controller->hold(Controller::UP))
291     input_direction = D_NORTH;
292   else if(g_main_controller->hold(Controller::DOWN))
293     input_direction = D_SOUTH;
294   else if(g_main_controller->hold(Controller::LEFT))
295     input_direction = D_WEST;
296   else if(g_main_controller->hold(Controller::RIGHT))
297     input_direction = D_EAST;
298 }
299
300 void
301 Tux::update(float elapsed_time)
302 {
303   updateInputDirection();
304   if (moving)
305     tryContinueWalking(elapsed_time);
306   else
307     tryStartWalking();
308 }
309
310 void
311 Tux::setup()
312 {
313   // check if we already touch a SpriteChange object
314   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
315   if(sprite_change != NULL) {
316     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
317     sprite_change->clear_stay_action();
318   }
319 }
320
321 } // namespace WorldmapNS
322
323 /* EOF */