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