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.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "video/drawing_context.hpp"
26 #include "player_status.hpp"
27 #include "worldmap.hpp"
28 #include "worldmap/level.hpp"
29 #include "special_tile.hpp"
30 #include "sprite_change.hpp"
31 #include "control/joystickkeyboardcontroller.hpp"
32 #include "scripting/squirrel_util.hpp"
39 static const float TUXSPEED = 200;
40 static const float map_message_TIME = 2.8f;
42 Tux::Tux(WorldMap* worldmap_)
45 sprite.reset(sprite_manager->create("images/worldmap/common/tux.sprite"));
50 input_direction = D_NONE;
60 Tux::draw(DrawingContext& context)
62 switch (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 ? "small-walking" : "small-stop");
73 log_debug << "Bonus type not handled in worldmap." << std::endl;
74 sprite->set_action("large-stop");
78 sprite->draw(context, get_pos(), LAYER_OBJECTS);
85 float x = tile_pos.x * 32;
86 float y = tile_pos.y * 32;
114 input_direction = D_NONE;
119 Tux::set_direction(Direction dir)
121 input_direction = dir;
125 Tux::set_ghost_mode(bool enabled)
127 ghost_mode = enabled;
131 Tux::get_ghost_mode()
137 Tux::tryStartWalking()
141 if (input_direction == D_NONE)
144 LevelTile* level = worldmap->at_level();
146 // We got a new direction, so lets start walking when possible
148 if ((!level || level->solved)
149 && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
150 tile_pos = next_tile;
152 direction = input_direction;
153 back_direction = reverse_dir(direction);
154 } else if (ghost_mode || (input_direction == back_direction)) {
156 direction = input_direction;
157 tile_pos = worldmap->get_next_tile(tile_pos, direction);
158 back_direction = reverse_dir(direction);
163 Tux::canWalk(int tile_data, Direction dir)
166 ((tile_data & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
167 (tile_data & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
168 (tile_data & Tile::WORLDMAP_EAST && dir == D_EAST) ||
169 (tile_data & Tile::WORLDMAP_WEST && dir == D_WEST));
173 Tux::tryContinueWalking(float elapsed_time)
179 offset += TUXSPEED * elapsed_time;
181 // Do nothing if we have not yet reached the next tile
187 SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
188 if(sprite_change != NULL) {
189 sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
190 sprite_change->clear_stay_action();
193 // if this is a special_tile with passive_message, display it
194 SpecialTile* special_tile = worldmap->at_special_tile();
197 // direction and the apply_action_ are opposites, since they "see"
198 // directions in a different way
199 if((direction == D_NORTH && special_tile->apply_action_south) ||
200 (direction == D_SOUTH && special_tile->apply_action_north) ||
201 (direction == D_WEST && special_tile->apply_action_east) ||
202 (direction == D_EAST && special_tile->apply_action_west))
204 if(special_tile->passive_message) {
205 worldmap->passive_message = special_tile->map_message;
206 worldmap->passive_message_timer.start(map_message_TIME);
207 } else if(special_tile->script != "") {
209 std::istringstream in(special_tile->script);
210 worldmap->run_script(in, "specialtile");
211 } catch(std::exception& e) {
212 log_warning << "Couldn't execute special tile script: " << e.what()
219 // check if we are at a Teleporter
220 Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
222 // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
223 if ((worldmap->at_level())
224 || (worldmap->tile_data_at(tile_pos) & Tile::WORLDMAP_STOP)
225 || (special_tile && !special_tile->passive_message
226 && special_tile->script == "")
227 || (teleporter) || ghost_mode) {
228 if(special_tile && !special_tile->map_message.empty()
229 && !special_tile->passive_message)
230 worldmap->passive_message_timer.start(0);
235 // if user wants to change direction, try changing, else guess the direction in which to walk next
236 const int tile_data = worldmap->tile_data_at(tile_pos);
237 if ((direction != input_direction) && canWalk(tile_data, input_direction)) {
238 direction = input_direction;
239 back_direction = reverse_dir(direction);
241 Direction dir = D_NONE;
242 if (tile_data & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
244 else if (tile_data & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
246 else if (tile_data & Tile::WORLDMAP_EAST && back_direction != D_EAST)
248 else if (tile_data & Tile::WORLDMAP_WEST && back_direction != D_WEST)
252 // Should never be reached if tiledata is good
253 log_warning << "Could not determine where to walk next" << std::endl;
259 input_direction = direction;
260 back_direction = reverse_dir(direction);
263 // Walk automatically to the next tile
264 if(direction == D_NONE)
268 if (!ghost_mode && !worldmap->path_ok(direction, tile_pos, &next_tile)) {
269 log_debug << "Tilemap data is buggy" << std::endl;
274 SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
275 if(next_sprite != NULL && next_sprite->change_on_touch) {
276 sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
277 next_sprite->clear_stay_action();
279 SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
280 if(last_sprite != NULL && next_sprite != NULL) {
281 log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
282 last_sprite->set_stay_action();
285 tile_pos = next_tile;
289 Tux::updateInputDirection()
291 if(main_controller->hold(Controller::UP))
292 input_direction = D_NORTH;
293 else if(main_controller->hold(Controller::DOWN))
294 input_direction = D_SOUTH;
295 else if(main_controller->hold(Controller::LEFT))
296 input_direction = D_WEST;
297 else if(main_controller->hold(Controller::RIGHT))
298 input_direction = D_EAST;
302 Tux::update(float elapsed_time)
304 updateInputDirection();
306 tryContinueWalking(elapsed_time);
314 // check if we already touch a SpriteChange object
315 SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
316 if(sprite_change != NULL) {
317 sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
318 sprite_change->clear_stay_action();