+// $Id$
+//
+// SuperTux
+// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy 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.h"
-#include "video/drawing_context.h"
-#include "resources.h"
-#include "player.h"
-#include "special/sprite_manager.h"
-#include "lisp/lisp.h"
-#include "lisp/writer.h"
-#include "object_factory.h"
+#include "platform.hpp"
+
+#include <stdexcept>
+#include "log.hpp"
+#include "video/drawing_context.hpp"
+#include "resources.hpp"
+#include "player.hpp"
+#include "path.hpp"
+#include "path_walker.hpp"
+#include "sprite/sprite.hpp"
+#include "lisp/lisp.hpp"
+#include "object_factory.hpp"
Platform::Platform(const lisp::Lisp& reader)
+ : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), speed(Vector(0,0))
{
- sprite = sprite_manager->create("flying_platform");
- movement = Vector(0, 1);
- reader.get("x", bbox.p1.x);
- reader.get("y", bbox.p1.y);
- bbox.set_size(sprite->get_width(), sprite->get_height());
-
+ const lisp::Lisp* pathLisp = reader.get_lisp("path");
+ if(pathLisp == NULL)
+ throw std::runtime_error("No path specified for platform");
+ path.reset(new Path());
+ path->read(*pathLisp);
+ walker.reset(new PathWalker(path.get()));
+ bbox.set_pos(path->get_base());
+
flags |= FLAG_SOLID;
-
- state = 0;
}
-Platform::~Platform()
+Platform::Platform(const Platform& other)
+ : MovingSprite(other), speed(other.speed)
{
- delete sprite;
+ path.reset(new Path(*other.path));
+ walker.reset(new PathWalker(*other.walker));
+ walker->path = &*path;
}
+//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& )
-{
-#if 0
- if(typeid(object) == typeid(Player)) {
- Player* player = (Player*) &object;
- //player->movement += movement;
- }
-#endif
- return FORCE_MOVE;
-}
-
-void
-Platform::action(float )
+Platform::collision(GameObject& other, const CollisionHit& hit)
{
- // 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 (typeid(other) == typeid(Player)) {
+ if (hit.normal.y >= 0.9) {
+ //Tux is standing on the platform
+ //Player* player = (Player*) &other;
+ //player->add_velocity(speed * 1.5);
+ return PASS_MOVEMENT;
+ }
}
- 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;
+ if(other.get_flags() & FLAG_SOLID) {
+ //Collision with a solid tile
+ return ABORT_MOVE;
}
+ return FORCE_MOVE;
}
void
-Platform::draw(DrawingContext& context)
+Platform::update(float elapsed_time)
{
- sprite->draw(context, get_pos(), LAYER_OBJECTS);
+ movement = walker->advance(elapsed_time);
+ speed = movement / elapsed_time;
}
-IMPLEMENT_FACTORY(Platform, "flying_platform");
+IMPLEMENT_FACTORY(Platform, "platform");