Unified Messaging Subsystem
[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 #include <config.h>
21
22 #include "platform.hpp"
23
24 #include "msg.hpp"
25 #include "video/drawing_context.hpp"
26 #include "resources.hpp"
27 #include "player.hpp"
28 #include "sprite/sprite_manager.hpp"
29 #include "lisp/lisp.hpp"
30 #include "object_factory.hpp"
31
32 Platform::Platform(const lisp::Lisp& reader)
33 {
34   std::string use_path;
35   std::string type;
36
37   reader.get("x", bbox.p1.x);
38   reader.get("y", bbox.p1.y);
39   reader.get("type", type);
40   reader.get("path", use_path);
41   sprite = sprite_manager->create("images/objects/flying_platform/platform.sprite");
42   sprite->set_action(type);
43   bbox.set_size(sprite->get_width(), sprite->get_height());
44
45   flags |= FLAG_SOLID;
46
47   path = Path::GetByName(use_path);
48
49   if (path == NULL) {
50     msg_warning("Path \"" << use_path << "\" for moving platform not found! Make sure that the name is spelled correctly and that the path is initialized before the platform in the level file!");
51   }
52
53   path_offset = bbox.p1;
54 }
55
56 Platform::~Platform()
57 {
58   delete sprite;
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     Player* player = (Player*) &other;
69     if ((hit.normal.x == 0) && (hit.normal.y == 1)) {
70       //Tux is standing on the platform
71       player->movement += path->GetLastMovement();
72     }
73   }
74   if(other.get_flags() & FLAG_SOLID) {
75     //Collision with a solid tile
76     //does nothing, because the movement vector isn't used at the moment
77     return ABORT_MOVE;
78   }
79   return FORCE_MOVE;
80 }
81
82 void
83 Platform::update(float )
84 {
85   set_pos(path->GetPosition() + path_offset);
86 }
87
88 void
89 Platform::draw(DrawingContext& context)
90 {
91   sprite->draw(context, get_pos(), LAYER_OBJECTS);
92 }
93
94 IMPLEMENT_FACTORY(Platform, "platform");