- Reworked Surface class and drawing stuff:
[supertux.git] / src / object / platform.cpp
index 8197f6e..c0a59a7 100644 (file)
@@ -1,7 +1,7 @@
 //  $Id$
-// 
+//
 //  SuperTux
-//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2005 Marek Moeckel <wansti@gmx.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
-
 #include <config.h>
 
 #include "platform.hpp"
+
+#include <iostream>
 #include "video/drawing_context.hpp"
 #include "resources.hpp"
 #include "player.hpp"
 #include "sprite/sprite_manager.hpp"
 #include "lisp/lisp.hpp"
-#include "lisp/writer.hpp"
 #include "object_factory.hpp"
 
 Platform::Platform(const lisp::Lisp& reader)
 {
-  sprite = sprite_manager->create("flying_platform");
-  movement = Vector(0, 1);
+  std::string use_path;
+  std::string type;
+
   reader.get("x", bbox.p1.x);
   reader.get("y", bbox.p1.y);
+  reader.get("type", type);
+  reader.get("use_path", use_path);
+  sprite = sprite_manager->create("platform");
+  sprite->set_action(type);
   bbox.set_size(sprite->get_width(), sprite->get_height());
 
   flags |= FLAG_SOLID;
 
-  state = 0;
+  path = Path::GetByName(use_path);
+  if (path == NULL) { 
+     std::cerr << "Warning: Path for moving platform not found! Make sure that the name is spelled correctly,\nand that the path is initialized before the platform in the level file!\n";
+  }
+
+  path_offset = bbox.p1 - path->GetStart();
 }
 
 Platform::~Platform()
@@ -47,47 +57,31 @@ Platform::~Platform()
   delete sprite;
 }
 
+//TODO: Squish Tux when standing between platform and solid tile/object
+//      Improve collision handling
+//      Move all MovingObjects lying on the platform instead of only the player
 HitResponse
-Platform::collision(GameObject& , const CollisionHit& )
+Platform::collision(GameObject& other, const CollisionHit& hit)
 {
-#if 0
-  if(typeid(object) == typeid(Player)) {
-    Player* player = (Player*) &object;
-    //player->movement += movement;
+  if (typeid(other) == typeid(Player)) {
+    Player* player = (Player*) &other;
+    if ((hit.normal.x == 0) && (hit.normal.y == 1)) {
+      //Tux is standing on the platform
+      player->movement += path->GetLastMovement();
+    }
+  }
+  if(other.get_flags() & FLAG_SOLID) {
+    //Collision with a solid tile
+    //does nothing, because the movement vector isn't used at the moment
+    return ABORT_MOVE;
   }
-#endif
   return FORCE_MOVE;
 }
 
 void
 Platform::update(float )
 {
-  // just some test code...
-  if(state == 0) {
-    movement = Vector(0, 1);
-    if(bbox.p1.y > 250)
-      state = 1;
-  }
-  if(state == 1) {
-    movement = Vector(0, -1);
-    if(bbox.p1.y < 50)
-      state = 2;
-  }
-  if(state == 2) {
-    movement = Vector(1, 0);
-    if(bbox.p1.x > 800)
-      state = 3;
-  }
-  if(state == 3) {
-    movement = Vector(-1, 0);
-    if(bbox.p1.x < 400)
-      state = 4;
-  }
-  if(state == 4) {
-    movement = Vector(-1, 1);
-    if(bbox.p1.x < 0)
-      state = 0;
-  }
+  set_pos(path->GetPosition() + path_offset);
 }
 
 void
@@ -96,4 +90,4 @@ Platform::draw(DrawingContext& context)
   sprite->draw(context, get_pos(), LAYER_OBJECTS);
 }
 
-IMPLEMENT_FACTORY(Platform, "flying_platform");
+IMPLEMENT_FACTORY(Platform, "platform");