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