b3c0dc52d26bd6bbd69c9a1fdf2f8f557fcc38dc
[supertux.git] / src / object / platform.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2005 Marek Moeckel <wansti@gmx.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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "platform.hpp"
24 #include "video/drawing_context.hpp"
25 #include "resources.hpp"
26 #include "player.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "lisp/lisp.hpp"
29 #include "object_factory.hpp"
30
31 Platform::Platform(const lisp::Lisp& reader)
32 {
33   std::string use_path;
34   std::string type;
35
36   reader.get("x", bbox.p1.x);
37   reader.get("y", bbox.p1.y);
38   reader.get("type", type);
39   reader.get("use_path", use_path);
40   sprite = sprite_manager->create("platform");
41   sprite->set_action(type);
42   bbox.set_size(sprite->get_width(), sprite->get_height());
43
44   flags |= FLAG_SOLID;
45
46   path = Path::GetByName(use_path);
47   if (path == NULL) { 
48      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";
49   }
50
51   path_offset = bbox.p1 - path->GetStart();
52 }
53
54 Platform::~Platform()
55 {
56   delete sprite;
57 }
58
59 //TODO: Squish Tux when standing between platform and solid tile/object
60 //      Improve collision handling
61 //      Move all MovingObjects lying on the platform instead of only the player
62 HitResponse
63 Platform::collision(GameObject& other, const CollisionHit& hit)
64 {
65   if (typeid(other) == typeid(Player)) {
66     Player* player = (Player*) &other;
67     if ((hit.normal.x == 0) && (hit.normal.y == 1)) {
68       //Tux is standing on the platform
69       player->movement += path->GetLastMovement();
70     }
71   }
72   if(other.get_flags() & FLAG_SOLID) {
73     //Collision with a solid tile
74     //does nothing, because the movement vector isn't used at the moment
75     return ABORT_MOVE;
76   }
77   return FORCE_MOVE;
78 }
79
80 void
81 Platform::update(float elapsed_time)
82 {
83   set_pos(path->GetPosition() + path_offset);
84 }
85
86 void
87 Platform::draw(DrawingContext& context)
88 {
89   sprite->draw(context, get_pos(), LAYER_OBJECTS);
90 }
91
92 IMPLEMENT_FACTORY(Platform, "platform");