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"
27 #include "worldmap/level.hpp"
28 #include "special_tile.hpp"
29 #include "sprite_change.hpp"
30 #include "control/joystickkeyboardcontroller.hpp"
36 static const float TUXSPEED = 200;
37 static const float map_message_TIME = 2.8;
39 Tux::Tux(WorldMap* worldmap_)
42 sprite.reset(sprite_manager->create("images/worldmap/common/tux.sprite"));
47 input_direction = D_NONE;
55 Tux::draw(DrawingContext& context)
57 switch (player_status->bonus) {
59 sprite->set_action(moving ? "large-walking" : "large-stop");
62 sprite->set_action(moving ? "fire-walking" : "fire-stop");
65 sprite->set_action(moving ? "small-walking" : "small-stop");
68 log_debug << "Bonus type not handled in worldmap." << std::endl;
69 sprite->set_action("large-stop");
73 sprite->draw(context, get_pos(), LAYER_OBJECTS);
80 float x = tile_pos.x * 32;
81 float y = tile_pos.y * 32;
109 input_direction = D_NONE;
114 Tux::set_direction(Direction dir)
116 input_direction = dir;
120 Tux::tryStartWalking()
124 if (input_direction == D_NONE)
127 LevelTile* level = worldmap->at_level();
129 // We got a new direction, so lets start walking when possible
131 if ((!level || level->solved)
132 && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
133 tile_pos = next_tile;
135 direction = input_direction;
136 back_direction = reverse_dir(direction);
137 } else if (input_direction == back_direction) {
139 direction = input_direction;
140 tile_pos = worldmap->get_next_tile(tile_pos, direction);
141 back_direction = reverse_dir(direction);
146 Tux::canWalk(const Tile* tile, Direction dir)
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));
155 Tux::tryContinueWalking(float elapsed_time)
161 offset += TUXSPEED * elapsed_time;
163 // Do nothing if we have not yet reached the next tile
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;
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)
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))
186 worldmap->passive_message = special_tile->map_message;
187 worldmap->passive_message_timer.start(map_message_TIME);
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);
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);
210 Direction dir = D_NONE;
211 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
213 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
215 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST)
217 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST)
221 // Should never be reached if tiledata is good
222 log_warning << "Could not determine where to walk next" << std::endl;
228 input_direction = direction;
229 back_direction = reverse_dir(direction);
232 // Walk automatically to the next tile
233 if(direction == D_NONE)
237 if (!worldmap->path_ok(direction, tile_pos, &next_tile)) {
238 log_warning << "Tilemap data is buggy" << std::endl;
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;
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;
254 tile_pos = next_tile;
258 Tux::updateInputDirection()
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;
271 Tux::update(float elapsed_time)
273 updateInputDirection();
275 tryContinueWalking(elapsed_time);