Platform that hurts Tux and Badguys when touched
[supertux.git] / src / object / platform.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "platform.hpp"
23
24 #include <stdexcept>
25 #include "log.hpp"
26 #include "video/drawing_context.hpp"
27 #include "resources.hpp"
28 #include "player.hpp"
29 #include "path.hpp"
30 #include "path_walker.hpp"
31 #include "sprite/sprite.hpp"
32 #include "lisp/lisp.hpp"
33 #include "object_factory.hpp"
34
35 Platform::Platform(const lisp::Lisp& reader)
36         : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), speed(Vector(0,0))
37 {
38   const lisp::Lisp* pathLisp = reader.get_lisp("path");
39   if(pathLisp == NULL)
40     throw std::runtime_error("No path specified for platform");
41   path.reset(new Path());
42   path->read(*pathLisp);
43   walker.reset(new PathWalker(path.get()));
44   bbox.set_pos(path->get_base());
45  
46   flags |= FLAG_SOLID;
47 }
48
49 Platform::Platform(const Platform& other)
50         : MovingSprite(other), speed(other.speed)
51 {
52   path.reset(new Path(*other.path));
53   walker.reset(new PathWalker(*other.walker));
54   walker->path = &*path;
55 }
56
57 //TODO: Squish Tux when standing between platform and solid tile/object
58 //      Improve collision handling
59 //      Move all MovingObjects lying on the platform instead of only the player
60 HitResponse
61 Platform::collision(GameObject& other, const CollisionHit& hit)
62 {
63   if (typeid(other) == typeid(Player)) {
64     if (hit.normal.y >= 0.9) {
65       //Tux is standing on the platform
66       //Player* player = (Player*) &other;
67       //player->add_velocity(speed * 1.5);
68       return PASS_MOVEMENT;
69     }
70   }
71   if(other.get_flags() & FLAG_SOLID) {
72     //Collision with a solid tile
73     return ABORT_MOVE;
74   }
75   return FORCE_MOVE;
76 }
77
78 void
79 Platform::update(float elapsed_time)
80 {
81   movement = walker->advance(elapsed_time);
82   speed = movement / elapsed_time;
83 }
84
85 IMPLEMENT_FACTORY(Platform, "platform");