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"
31 #include "scripting/squirrel_util.hpp"
37 static const float TUXSPEED = 200;
38 static const float map_message_TIME = 2.8f;
40 Tux::Tux(WorldMap* worldmap_)
43 sprite.reset(sprite_manager->create("images/worldmap/common/tux.sprite"));
48 input_direction = D_NONE;
56 Tux::draw(DrawingContext& context)
58 switch (player_status->bonus) {
60 sprite->set_action(moving ? "large-walking" : "large-stop");
63 sprite->set_action(moving ? "fire-walking" : "fire-stop");
66 sprite->set_action(moving ? "small-walking" : "small-stop");
69 log_debug << "Bonus type not handled in worldmap." << std::endl;
70 sprite->set_action("large-stop");
74 sprite->draw(context, get_pos(), LAYER_OBJECTS);
81 float x = tile_pos.x * 32;
82 float y = tile_pos.y * 32;
110 input_direction = D_NONE;
115 Tux::set_direction(Direction dir)
117 input_direction = dir;
121 Tux::tryStartWalking()
125 if (input_direction == D_NONE)
128 LevelTile* level = worldmap->at_level();
130 // We got a new direction, so lets start walking when possible
132 if ((!level || level->solved)
133 && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
134 tile_pos = next_tile;
136 direction = input_direction;
137 back_direction = reverse_dir(direction);
138 } 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);
147 Tux::canWalk(const Tile* tile, Direction dir)
149 return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
150 (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
151 (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
152 (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
156 Tux::tryContinueWalking(float elapsed_time)
162 offset += TUXSPEED * elapsed_time;
164 // Do nothing if we have not yet reached the next tile
170 SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
171 if(sprite_change != NULL) {
172 sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
173 sprite_change->clear_stay_action();
176 // if this is a special_tile with passive_message, display it
177 SpecialTile* special_tile = worldmap->at_special_tile();
180 // direction and the apply_action_ are opposites, since they "see"
181 // directions in a different way
182 if((direction == D_NORTH && special_tile->apply_action_south) ||
183 (direction == D_SOUTH && special_tile->apply_action_north) ||
184 (direction == D_WEST && special_tile->apply_action_east) ||
185 (direction == D_EAST && special_tile->apply_action_west))
187 if(special_tile->passive_message) {
188 worldmap->passive_message = special_tile->map_message;
189 worldmap->passive_message_timer.start(map_message_TIME);
190 } else if(special_tile->script != "") {
192 std::istringstream in(special_tile->script);
193 worldmap->run_script(in, "specialtile");
194 } catch(std::exception& e) {
195 log_warning << "Couldn't execute special tile script: " << e.what()
202 // check if we are at a Teleporter
203 Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
205 // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
206 if ((worldmap->at_level())
207 || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP)
208 || (special_tile && !special_tile->passive_message
209 && special_tile->script == "")
211 if(special_tile && !special_tile->map_message.empty()
212 && !special_tile->passive_message)
213 worldmap->passive_message_timer.start(0);
218 // if user wants to change direction, try changing, else guess the direction in which to walk next
219 const Tile* tile = worldmap->at(tile_pos);
220 if (direction != input_direction) {
221 if(canWalk(tile, input_direction)) {
222 direction = input_direction;
223 back_direction = reverse_dir(direction);
226 Direction dir = D_NONE;
227 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
229 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
231 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST)
233 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST)
237 // Should never be reached if tiledata is good
238 log_warning << "Could not determine where to walk next" << std::endl;
244 input_direction = direction;
245 back_direction = reverse_dir(direction);
248 // Walk automatically to the next tile
249 if(direction == D_NONE)
253 if (!worldmap->path_ok(direction, tile_pos, &next_tile)) {
254 log_warning << "Tilemap data is buggy" << std::endl;
259 SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
260 if(next_sprite != NULL && next_sprite->change_on_touch) {
261 sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
262 next_sprite->clear_stay_action();
264 SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
265 if(last_sprite != NULL && next_sprite != NULL) {
266 log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
267 last_sprite->set_stay_action();
270 tile_pos = next_tile;
274 Tux::updateInputDirection()
276 if(main_controller->hold(Controller::UP))
277 input_direction = D_NORTH;
278 else if(main_controller->hold(Controller::DOWN))
279 input_direction = D_SOUTH;
280 else if(main_controller->hold(Controller::LEFT))
281 input_direction = D_WEST;
282 else if(main_controller->hold(Controller::RIGHT))
283 input_direction = D_EAST;
287 Tux::update(float elapsed_time)
289 updateInputDirection();
291 tryContinueWalking(elapsed_time);
299 // check if we already touch a SpriteChange object
300 SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
301 if(sprite_change != NULL) {
302 sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
303 sprite_change->clear_stay_action();