1 // $Id: worldmap.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $
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>
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.
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.
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.
23 #include "sprite/sprite_manager.hpp"
24 #include "video/drawing_context.hpp"
25 #include "player_status.hpp"
26 #include "worldmap.hpp"
28 #include "special_tile.hpp"
29 #include "control/joystickkeyboardcontroller.hpp"
35 static const float TUXSPEED = 200;
36 static const float map_message_TIME = 2.8;
38 Tux::Tux(WorldMap* worldmap_)
41 sprite.reset(sprite_manager->create("images/worldmap/common/tux.sprite"));
46 input_direction = D_NONE;
54 Tux::draw(DrawingContext& context)
56 switch (player_status->bonus) {
58 sprite->set_action(moving ? "large-walking" : "large-stop");
61 sprite->set_action(moving ? "fire-walking" : "fire-stop");
64 sprite->set_action(moving ? "small-walking" : "small-stop");
67 log_debug << "Bonus type not handled in worldmap." << std::endl;
68 sprite->set_action("large-stop");
72 sprite->draw(context, get_pos(), LAYER_OBJECTS);
79 float x = tile_pos.x * 32;
80 float y = tile_pos.y * 32;
108 input_direction = D_NONE;
113 Tux::set_direction(Direction dir)
115 input_direction = dir;
119 Tux::tryStartWalking()
123 if (input_direction == D_NONE)
126 Level* level = worldmap->at_level();
128 // We got a new direction, so lets start walking when possible
130 if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
132 tile_pos = next_tile;
134 direction = input_direction;
135 back_direction = reverse_dir(direction);
137 else if (input_direction == back_direction)
140 direction = input_direction;
141 tile_pos = worldmap->get_next_tile(tile_pos, direction);
142 back_direction = reverse_dir(direction);
148 Tux::canWalk(const Tile* tile, Direction dir)
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));
157 Tux::tryContinueWalking(float elapsed_time)
162 offset += TUXSPEED * elapsed_time;
164 // Do nothing if we have not yet reached the next tile
165 if (offset <= 32) return;
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)
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))
180 worldmap->passive_message = special_tile->map_message;
181 worldmap->passive_message_timer.start(map_message_TIME);
187 // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
188 if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
190 if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
195 // if user wants to change direction, try changing, else guess the direction in which to walk next
196 const Tile* tile = worldmap->at(tile_pos);
197 if (direction != input_direction)
199 if(canWalk(tile, input_direction))
201 direction = input_direction;
202 back_direction = reverse_dir(direction);
207 Direction dir = D_NONE;
208 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
209 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
210 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
211 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
215 // Should never be reached if tiledata is good
216 log_warning << "Could not determine where to walk next" << std::endl;
222 input_direction = direction;
223 back_direction = reverse_dir(direction);
226 // Walk automatically to the next tile
227 if(direction != D_NONE)
230 if (worldmap->path_ok(direction, tile_pos, &next_tile))
232 tile_pos = next_tile;
236 log_warning << "Tilemap data is buggy" << std::endl;
243 Tux::updateInputDirection()
245 if(main_controller->hold(Controller::UP))
246 input_direction = D_NORTH;
247 else if(main_controller->hold(Controller::DOWN))
248 input_direction = D_SOUTH;
249 else if(main_controller->hold(Controller::LEFT))
250 input_direction = D_WEST;
251 else if(main_controller->hold(Controller::RIGHT))
252 input_direction = D_EAST;
256 Tux::update(float elapsed_time)
258 updateInputDirection();
259 if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();