-modified moving platform class to make use of the new path class.
[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
48   path_offset = bbox.p1 - path->GetStart();
49 }
50
51 Platform::~Platform()
52 {
53   delete sprite;
54 }
55
56 HitResponse
57 Platform::collision(GameObject& other, const CollisionHit& hit)
58 {
59   if (typeid(other) == typeid(Player)) {
60     Player* player = (Player*) &other;
61     if ((hit.normal.x == 0) && (hit.normal.y == 1)) {
62       //Tux is standing on the platform
63       player->movement += path->GetLastMovement();
64     }
65   }
66   if(other.get_flags() & FLAG_SOLID) {
67     //Collision with a solid tile
68     //does nothing, because the movement vector isn't used at the moment
69   }
70   return FORCE_MOVE;
71 }
72
73 void
74 Platform::update(float elapsed_time)
75 {
76   set_pos(path->GetPosition() + path_offset);
77 }
78
79 void
80 Platform::draw(DrawingContext& context)
81 {
82   sprite->draw(context, get_pos(), LAYER_OBJECTS);
83 }
84
85 IMPLEMENT_FACTORY(Platform, "platform");