- Reworked Surface class and drawing stuff:
[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 <iostream>
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("use_path", use_path);
41   sprite = sprite_manager->create("platform");
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   if (path == NULL) { 
49      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";
50   }
51
52   path_offset = bbox.p1 - path->GetStart();
53 }
54
55 Platform::~Platform()
56 {
57   delete sprite;
58 }
59
60 //TODO: Squish Tux when standing between platform and solid tile/object
61 //      Improve collision handling
62 //      Move all MovingObjects lying on the platform instead of only the player
63 HitResponse
64 Platform::collision(GameObject& other, const CollisionHit& hit)
65 {
66   if (typeid(other) == typeid(Player)) {
67     Player* player = (Player*) &other;
68     if ((hit.normal.x == 0) && (hit.normal.y == 1)) {
69       //Tux is standing on the platform
70       player->movement += path->GetLastMovement();
71     }
72   }
73   if(other.get_flags() & FLAG_SOLID) {
74     //Collision with a solid tile
75     //does nothing, because the movement vector isn't used at the moment
76     return ABORT_MOVE;
77   }
78   return FORCE_MOVE;
79 }
80
81 void
82 Platform::update(float )
83 {
84   set_pos(path->GetPosition() + path_offset);
85 }
86
87 void
88 Platform::draw(DrawingContext& context)
89 {
90   sprite->draw(context, get_pos(), LAYER_OBJECTS);
91 }
92
93 IMPLEMENT_FACTORY(Platform, "platform");