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