7fa35267f5114ac6f159aafec4a75eb583f19610
[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
53 Tux::~Tux()
54 {
55 }
56
57 void
58 Tux::draw(DrawingContext& context)
59 {
60   switch (player_status->bonus) {
61     case GROWUP_BONUS:
62       sprite->set_action(moving ? "large-walking" : "large-stop");
63       break;
64     case FIRE_BONUS:
65       sprite->set_action(moving ? "fire-walking" : "fire-stop");
66       break;
67     case NO_BONUS:
68       sprite->set_action(moving ? "small-walking" : "small-stop");
69       break;
70     default:
71       log_debug << "Bonus type not handled in worldmap." << std::endl;
72       sprite->set_action("large-stop");
73       break;
74   }
75
76   sprite->draw(context, get_pos(), LAYER_OBJECTS);
77 }
78
79
80 Vector
81 Tux::get_pos()
82 {
83   float x = tile_pos.x * 32;
84   float y = tile_pos.y * 32;
85
86   switch(direction)
87     {
88     case D_WEST:
89       x -= offset - 32;
90       break;
91     case D_EAST:
92       x += offset - 32;
93       break;
94     case D_NORTH:
95       y -= offset - 32;
96       break;
97     case D_SOUTH:
98       y += offset - 32;
99       break;
100     case D_NONE:
101       break;
102     }
103
104   return Vector(x, y);
105 }
106
107 void
108 Tux::stop()
109 {
110   offset = 0;
111   direction = D_NONE;
112   input_direction = D_NONE;
113   moving = false;
114 }
115
116 void
117 Tux::set_direction(Direction dir)
118 {
119   input_direction = dir;
120 }
121
122 void
123 Tux::tryStartWalking()
124 {
125   if (moving)
126     return;
127   if (input_direction == D_NONE)
128     return;
129
130   LevelTile* level = worldmap->at_level();
131
132   // We got a new direction, so lets start walking when possible
133   Vector next_tile;
134   if ((!level || level->solved)
135       && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
136     tile_pos = next_tile;
137     moving = true;
138     direction = input_direction;
139     back_direction = reverse_dir(direction);
140   } else if (input_direction == back_direction) {
141     moving = true;
142     direction = input_direction;
143     tile_pos = worldmap->get_next_tile(tile_pos, direction);
144     back_direction = reverse_dir(direction);
145   }
146 }
147
148 bool
149 Tux::canWalk(int tile_data, Direction dir)
150 {
151   return ((tile_data & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
152           (tile_data & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
153           (tile_data & Tile::WORLDMAP_EAST && dir == D_EAST) ||
154           (tile_data & Tile::WORLDMAP_WEST && dir == D_WEST));
155 }
156
157 void
158 Tux::tryContinueWalking(float elapsed_time)
159 {
160   if (!moving)
161     return;
162
163   // Let tux walk
164   offset += TUXSPEED * elapsed_time;
165
166   // Do nothing if we have not yet reached the next tile
167   if (offset <= 32)
168     return;
169
170   offset -= 32;
171
172   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
173   if(sprite_change != NULL) {
174     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
175     sprite_change->clear_stay_action();
176   }
177
178   // if this is a special_tile with passive_message, display it
179   SpecialTile* special_tile = worldmap->at_special_tile();
180   if(special_tile)
181   {
182     // direction and the apply_action_ are opposites, since they "see"
183     // directions in a different way
184     if((direction == D_NORTH && special_tile->apply_action_south) ||
185                     (direction == D_SOUTH && special_tile->apply_action_north) ||
186                     (direction == D_WEST && special_tile->apply_action_east) ||
187                     (direction == D_EAST && special_tile->apply_action_west))
188     {
189       if(special_tile->passive_message) {
190         worldmap->passive_message = special_tile->map_message;
191         worldmap->passive_message_timer.start(map_message_TIME);
192       } else if(special_tile->script != "") {
193         try {
194           std::istringstream in(special_tile->script);
195           worldmap->run_script(in, "specialtile");
196         } catch(std::exception& e) {
197           log_warning << "Couldn't execute special tile script: " << e.what()
198                       << std::endl;
199         }
200       }
201     }
202   }
203
204   // check if we are at a Teleporter
205   Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
206
207   // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
208   if ((worldmap->at_level())
209       || (worldmap->tile_data_at(tile_pos) & Tile::WORLDMAP_STOP)
210       || (special_tile && !special_tile->passive_message
211                        && special_tile->script == "")
212       || (teleporter)) {
213     if(special_tile && !special_tile->map_message.empty()
214         && !special_tile->passive_message)
215       worldmap->passive_message_timer.start(0);
216     stop();
217     return;
218   }
219
220   // if user wants to change direction, try changing, else guess the direction in which to walk next
221   const int tile_data = worldmap->tile_data_at(tile_pos);
222   if ((direction != input_direction) && canWalk(tile_data, input_direction)) {
223     direction = input_direction;
224     back_direction = reverse_dir(direction);
225   } else {
226     Direction dir = D_NONE;
227     if (tile_data & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
228       dir = D_NORTH;
229     else if (tile_data & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
230       dir = D_SOUTH;
231     else if (tile_data & Tile::WORLDMAP_EAST && back_direction != D_EAST)
232       dir = D_EAST;
233     else if (tile_data & Tile::WORLDMAP_WEST && back_direction != D_WEST)
234       dir = D_WEST;
235
236     if (dir == D_NONE) {
237       // Should never be reached if tiledata is good
238       log_warning << "Could not determine where to walk next" << std::endl;
239       stop();
240       return;
241     }
242
243     direction = dir;
244     input_direction = direction;
245     back_direction = reverse_dir(direction);
246   }
247
248   // Walk automatically to the next tile
249   if(direction == D_NONE)
250     return;
251
252   Vector next_tile;
253   if (!worldmap->path_ok(direction, tile_pos, &next_tile)) {
254     log_debug << "Tilemap data is buggy" << std::endl;
255     stop();
256     return;
257   }
258
259   SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
260   if(next_sprite != NULL && next_sprite->change_on_touch) {
261     sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
262     next_sprite->clear_stay_action();
263   }
264   SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
265   if(last_sprite != NULL && next_sprite != NULL) {
266     log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
267     last_sprite->set_stay_action();
268   }
269
270   tile_pos = next_tile;
271 }
272
273 void
274 Tux::updateInputDirection()
275 {
276   if(main_controller->hold(Controller::UP))
277     input_direction = D_NORTH;
278   else if(main_controller->hold(Controller::DOWN))
279     input_direction = D_SOUTH;
280   else if(main_controller->hold(Controller::LEFT))
281     input_direction = D_WEST;
282   else if(main_controller->hold(Controller::RIGHT))
283     input_direction = D_EAST;
284 }
285
286 void
287 Tux::update(float elapsed_time)
288 {
289   updateInputDirection();
290   if (moving)
291     tryContinueWalking(elapsed_time);
292   else
293     tryStartWalking();
294 }
295
296 void
297 Tux::setup()
298 {
299   // check if we already touch a SpriteChange object
300   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
301   if(sprite_change != NULL) {
302     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
303     sprite_change->clear_stay_action();
304   }
305 }
306
307 }