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