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