1 // SuperTux - A Jump'n Run
2 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
3 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "control/joystickkeyboardcontroller.hpp"
19 #include "scripting/squirrel_util.hpp"
20 #include "sprite/sprite.hpp"
21 #include "sprite/sprite_manager.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/player_status.hpp"
24 #include "supertux/tile.hpp"
25 #include "worldmap/level.hpp"
26 #include "worldmap/tux.hpp"
30 static const float TUXSPEED = 200;
31 static const float map_message_TIME = 2.8f;
33 Tux::Tux(WorldMap* worldmap_) :
45 sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
50 input_direction = D_NONE;
60 Tux::draw(DrawingContext& context)
62 switch (worldmap->get_player_status()->bonus) {
64 sprite->set_action(moving ? "large-walking" : "large-stop");
67 sprite->set_action(moving ? "fire-walking" : "fire-stop");
70 sprite->set_action(moving ? "ice-walking" : "ice-stop");
73 sprite->set_action(moving ? "small-walking" : "small-stop");
76 log_debug << "Bonus type not handled in worldmap." << std::endl;
77 sprite->set_action("large-stop");
81 sprite->draw(context, get_pos(), LAYER_OBJECTS);
87 float x = tile_pos.x * 32;
88 float y = tile_pos.y * 32;
116 input_direction = D_NONE;
121 Tux::set_direction(Direction dir)
123 input_direction = dir;
127 Tux::set_ghost_mode(bool enabled)
129 ghost_mode = enabled;
133 Tux::get_ghost_mode()
139 Tux::tryStartWalking()
143 if (input_direction == D_NONE)
146 LevelTile* level = worldmap->at_level();
148 // We got a new direction, so lets start walking when possible
150 if ((!level || level->solved || level->perfect)
151 && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
152 tile_pos = next_tile;
154 direction = input_direction;
155 back_direction = reverse_dir(direction);
156 } else if (ghost_mode || (input_direction == back_direction)) {
158 direction = input_direction;
159 tile_pos = worldmap->get_next_tile(tile_pos, direction);
160 back_direction = reverse_dir(direction);
165 Tux::canWalk(int tile_data, Direction dir)
168 ((tile_data & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
169 (tile_data & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
170 (tile_data & Tile::WORLDMAP_EAST && dir == D_EAST) ||
171 (tile_data & Tile::WORLDMAP_WEST && dir == D_WEST));
175 Tux::tryContinueWalking(float elapsed_time)
181 offset += TUXSPEED * elapsed_time;
183 // Do nothing if we have not yet reached the next tile
189 SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
190 if(sprite_change != NULL) {
191 sprite = sprite_change->sprite->clone();
192 sprite_change->clear_stay_action();
195 // if this is a special_tile with passive_message, display it
196 SpecialTile* special_tile = worldmap->at_special_tile();
199 // direction and the apply_action_ are opposites, since they "see"
200 // directions in a different way
201 if((direction == D_NORTH && special_tile->apply_action_south) ||
202 (direction == D_SOUTH && special_tile->apply_action_north) ||
203 (direction == D_WEST && special_tile->apply_action_east) ||
204 (direction == D_EAST && special_tile->apply_action_west))
206 if(special_tile->passive_message) {
207 worldmap->passive_message = special_tile->map_message;
208 worldmap->passive_message_timer.start(map_message_TIME);
209 } else if(special_tile->script != "") {
211 std::istringstream in(special_tile->script);
212 worldmap->run_script(in, "specialtile");
213 } catch(std::exception& e) {
214 log_warning << "Couldn't execute special tile script: " << e.what()
221 // check if we are at a Teleporter
222 Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
224 // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
225 if ((worldmap->at_level())
226 || (worldmap->tile_data_at(tile_pos) & Tile::WORLDMAP_STOP)
227 || (special_tile && !special_tile->passive_message
228 && special_tile->script == "")
229 || (teleporter) || ghost_mode) {
230 if(special_tile && !special_tile->map_message.empty()
231 && !special_tile->passive_message)
232 worldmap->passive_message_timer.start(0);
237 // if user wants to change direction, try changing, else guess the direction in which to walk next
238 const int tile_data = worldmap->tile_data_at(tile_pos);
239 if ((direction != input_direction) && canWalk(tile_data, input_direction)) {
240 direction = input_direction;
241 back_direction = reverse_dir(direction);
243 Direction dir = D_NONE;
244 if (tile_data & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
246 else if (tile_data & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
248 else if (tile_data & Tile::WORLDMAP_EAST && back_direction != D_EAST)
250 else if (tile_data & Tile::WORLDMAP_WEST && back_direction != D_WEST)
254 // Should never be reached if tiledata is good
255 log_warning << "Could not determine where to walk next" << std::endl;
261 input_direction = direction;
262 back_direction = reverse_dir(direction);
265 // Walk automatically to the next tile
266 if(direction == D_NONE)
270 if (!ghost_mode && !worldmap->path_ok(direction, tile_pos, &next_tile)) {
271 log_debug << "Tilemap data is buggy" << std::endl;
276 SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
277 if(next_sprite != NULL && next_sprite->change_on_touch) {
278 sprite = next_sprite->sprite->clone();
279 next_sprite->clear_stay_action();
281 SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
282 if(last_sprite != NULL && next_sprite != NULL) {
283 log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
284 last_sprite->set_stay_action();
287 tile_pos = next_tile;
291 Tux::updateInputDirection()
293 Controller* controller = g_jk_controller->get_controller();
294 if(controller->hold(Controller::UP))
295 input_direction = D_NORTH;
296 else if(controller->hold(Controller::DOWN))
297 input_direction = D_SOUTH;
298 else if(controller->hold(Controller::LEFT))
299 input_direction = D_WEST;
300 else if(controller->hold(Controller::RIGHT))
301 input_direction = D_EAST;
305 Tux::update(float elapsed_time)
307 updateInputDirection();
309 tryContinueWalking(elapsed_time);
317 // check if we already touch a SpriteChange object
318 SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
319 if(sprite_change != NULL) {
320 sprite = sprite_change->sprite->clone();
321 sprite_change->clear_stay_action();
325 } // namespace WorldmapNS