Fixed wrong "top-edge" tiles in data/levels/bonus2/level11.stl ("The long cave")
[supertux.git] / src / worldmap / tux.cpp
index f1086af..7f5c50a 100644 (file)
@@ -1,4 +1,4 @@
-//  $Id: worldmap.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $
+//  $Id$
 //
 //  SuperTux -  A Jump'n Run
 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
 #include "video/drawing_context.hpp"
 #include "player_status.hpp"
 #include "worldmap.hpp"
-#include "level.hpp"
+#include "worldmap/level.hpp"
 #include "special_tile.hpp"
+#include "sprite_change.hpp"
 #include "control/joystickkeyboardcontroller.hpp"
+#include "scripting/squirrel_util.hpp"
 #include "main.hpp"
 
 namespace WorldMapNS
@@ -123,25 +125,22 @@ Tux::tryStartWalking()
   if (input_direction == D_NONE)
     return;
 
-  Level* level = worldmap->at_level();
+  LevelTile* level = worldmap->at_level();
 
   // We got a new direction, so lets start walking when possible
   Vector next_tile;
-  if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
-  {
+  if ((!level || level->solved)
+      && worldmap->path_ok(input_direction, tile_pos, &next_tile)) {
     tile_pos = next_tile;
     moving = true;
     direction = input_direction;
     back_direction = reverse_dir(direction);
-  }
-  else if (input_direction == back_direction)
-  {
+  } else if (input_direction == back_direction) {
     moving = true;
     direction = input_direction;
     tile_pos = worldmap->get_next_tile(tile_pos, direction);
     back_direction = reverse_dir(direction);
   }
-
 }
 
 bool 
@@ -156,19 +155,27 @@ Tux::canWalk(const Tile* tile, Direction dir)
 void 
 Tux::tryContinueWalking(float elapsed_time)
 {
-  if (!moving) return;
+  if (!moving)
+    return;
 
   // Let tux walk
   offset += TUXSPEED * elapsed_time;
 
   // Do nothing if we have not yet reached the next tile
-  if (offset <= 32) return;
+  if (offset <= 32)
+    return;
 
   offset -= 32;
 
+  SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
+  if(sprite_change != NULL) {
+    sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
+    sprite_change->clear_stay_action();
+  }
+
   // if this is a special_tile with passive_message, display it
   SpecialTile* special_tile = worldmap->at_special_tile();
-  if(special_tile && special_tile->passive_message)
+  if(special_tile)
   {  
     // direction and the apply_action_ are opposites, since they "see"
     // directions in a different way
@@ -177,41 +184,56 @@ Tux::tryContinueWalking(float elapsed_time)
                    (direction == D_WEST && special_tile->apply_action_east) ||
                    (direction == D_EAST && special_tile->apply_action_west))
     {
-      worldmap->passive_message = special_tile->map_message;
-      worldmap->passive_message_timer.start(map_message_TIME);
+      if(special_tile->passive_message) {
+        worldmap->passive_message = special_tile->map_message;
+        worldmap->passive_message_timer.start(map_message_TIME);
+      } else if(special_tile->script != "") {
+        try {
+          std::istringstream in(special_tile->script);
+          worldmap->run_script(in, "specialtile");
+        } catch(std::exception& e) {
+          log_warning << "Couldn't execute special tile script: " << e.what()
+                      << std::endl;
+        }
+      }
     }
   }
 
-
-
-  // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
-  if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
-  {
-    if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
+  // check if we are at a Teleporter
+  Teleporter* teleporter = worldmap->at_teleporter(tile_pos);
+
+  // stop if we reached a level, a WORLDMAP_STOP tile, a teleporter or a special tile without a passive_message
+  if ((worldmap->at_level())
+      || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP)
+      || (special_tile && !special_tile->passive_message
+                       && special_tile->script == "")
+      || (teleporter)) {
+    if(special_tile && !special_tile->map_message.empty()
+        && !special_tile->passive_message)
+      worldmap->passive_message_timer.start(0);
     stop();
     return;
   }
 
   // if user wants to change direction, try changing, else guess the direction in which to walk next
   const Tile* tile = worldmap->at(tile_pos);
-  if (direction != input_direction)
-  { 
-    if(canWalk(tile, input_direction))
-    {  
+  if (direction != input_direction) { 
+    if(canWalk(tile, input_direction)) {  
       direction = input_direction;
       back_direction = reverse_dir(direction);
     }
-  }
-  else
-  {
+  } else {
     Direction dir = D_NONE;
-    if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
-    else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
-    else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
-    else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
-
-    if (dir == D_NONE) 
-    {
+    if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH)
+      dir = D_NORTH;
+    else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH)
+      dir = D_SOUTH;
+    else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST)
+      dir = D_EAST;
+    else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST)
+      dir = D_WEST;
+
+    if (dir == D_NONE) {
       // Should never be reached if tiledata is good
       log_warning << "Could not determine where to walk next" << std::endl;
       stop();
@@ -224,19 +246,28 @@ Tux::tryContinueWalking(float elapsed_time)
   }
 
   // Walk automatically to the next tile
-  if(direction != D_NONE)
-  {
-    Vector next_tile;
-    if (worldmap->path_ok(direction, tile_pos, &next_tile))
-    {
-      tile_pos = next_tile;
-    }
-    else
-    {
-      log_warning << "Tilemap data is buggy" << std::endl;
-      stop();
-    }
+  if(direction == D_NONE)
+    return;
+  
+  Vector next_tile;
+  if (!worldmap->path_ok(direction, tile_pos, &next_tile)) {
+    log_warning << "Tilemap data is buggy" << std::endl;
+    stop();
+    return;
+  }
+
+  SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
+  if(next_sprite != NULL && next_sprite->change_on_touch) {
+    sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
+    next_sprite->clear_stay_action();
+  }
+  SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
+  if(last_sprite != NULL && next_sprite != NULL) {
+    log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
+    last_sprite->set_stay_action();
   }
+
+  tile_pos = next_tile;
 }
 
 void
@@ -256,7 +287,21 @@ void
 Tux::update(float elapsed_time)
 {
   updateInputDirection(); 
-  if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();
+  if (moving)
+    tryContinueWalking(elapsed_time);
+  else
+    tryStartWalking();
+}
+
+void
+Tux::setup()
+{
+  // check if we already touch a SpriteChange object
+  SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
+  if(sprite_change != NULL) {
+    sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
+    sprite_change->clear_stay_action();
+  }
 }
 
 }