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