1 // SuperTux - PneumaticPlatform
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/pneumatic_platform.hpp"
19 #include "object/player.hpp"
20 #include "object/portable.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
24 PneumaticPlatform::PneumaticPlatform(const Reader& reader) :
25 MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC),
32 start_y = get_pos().y;
35 PneumaticPlatform::PneumaticPlatform(PneumaticPlatform* master) :
36 MovingSprite(*master),
39 start_y(master->start_y),
40 offset_y(-master->offset_y),
43 set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
44 master->master = master;
48 PneumaticPlatform::~PneumaticPlatform()
50 if ((this == master) && (master)) {
54 if ((master) && (this == slave)) {
63 PneumaticPlatform::collision(GameObject& other, const CollisionHit& )
66 // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
67 MovingObject* mo = dynamic_cast<MovingObject*>(&other);
68 if (!mo) return FORCE_MOVE;
69 if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
71 Player* pl = dynamic_cast<Player*>(mo);
73 if (pl->is_big()) contacts.insert(0);
74 Portable* po = pl->get_grabbed_object();
75 MovingObject* pomo = dynamic_cast<MovingObject*>(po);
76 if (pomo) contacts.insert(pomo);
79 contacts.insert(&other);
84 PneumaticPlatform::update(float elapsed_time)
87 Sector::current()->add_object(new PneumaticPlatform(this));
94 offset_y = -master->offset_y;
95 movement = Vector(0, (start_y + offset_y) - get_pos().y);
98 int contact_diff = contacts.size() - slave->contacts.size();
100 slave->contacts.clear();
102 speed_y += ((float)contact_diff * elapsed_time) * 128.0f;
103 speed_y -= (offset_y * elapsed_time * 0.5f);
104 speed_y *= 1 - elapsed_time;
105 offset_y += speed_y * elapsed_time;
106 if (offset_y < -256) { offset_y = -256; speed_y = 0; }
107 if (offset_y > 256) { offset_y = 256; speed_y = -0; }
108 movement = Vector(0, (start_y + offset_y) - get_pos().y);
112 IMPLEMENT_FACTORY(PneumaticPlatform, "pneumatic-platform");