Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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_manager.hpp"
32 #include "lisp/lisp.hpp"
33 #include "object_factory.hpp"
34
35 Platform::Platform(const lisp::Lisp& reader)
36 {
37   std::string sprite_name;
38   reader.get("sprite", sprite_name);
39   if(sprite_name == "")
40     throw std::runtime_error("No sprite specified in platform object"); 
41   sprite.reset(sprite_manager->create(sprite_name));
42
43   const lisp::Lisp* pathLisp = reader.get_lisp("path");
44   if(pathLisp == NULL)
45     throw std::runtime_error("No path specified for platform");
46   path.reset(new Path());
47   path->read(*pathLisp);
48   walker.reset(new PathWalker(path.get()));
49
50   bbox.p1 = path->get_base();
51   bbox.set_size(sprite->get_width(), sprite->get_height());
52   
53   set_group(COLGROUP_STATIC);
54   flags |= FLAG_SOLID;
55 }
56
57 Platform::~Platform()
58 {
59 }
60
61 //TODO: Squish Tux when standing between platform and solid tile/object
62 //      Improve collision handling
63 //      Move all MovingObjects lying on the platform instead of only the player
64 HitResponse
65 Platform::collision(GameObject& other, const CollisionHit& hit)
66 {
67   if (typeid(other) == typeid(Player)) {
68     if (hit.normal.y >= 0.9) {
69       //Tux is standing on the platform
70       //Player* player = (Player*) &other;
71       //player->add_velocity(speed * 1.5);
72       return PASS_MOVEMENT;
73     }
74   }
75   if(other.get_flags() & FLAG_SOLID) {
76     //Collision with a solid tile
77     return ABORT_MOVE;
78   }
79   return FORCE_MOVE;
80 }
81
82 void
83 Platform::update(float elapsed_time)
84 {
85   movement = walker->advance(elapsed_time);
86   speed = movement / elapsed_time;
87 }
88
89 void
90 Platform::draw(DrawingContext& context)
91 {
92   sprite->draw(context, get_pos(), LAYER_OBJECTS);
93 }
94
95 IMPLEMENT_FACTORY(Platform, "platform");