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),
33 start_y = get_pos().y;
36 PneumaticPlatform::PneumaticPlatform(PneumaticPlatform* master) :
37 MovingSprite(*master),
40 start_y(master->start_y),
41 offset_y(-master->offset_y),
45 set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
46 master->master = master;
50 PneumaticPlatform::~PneumaticPlatform()
52 if ((this == master) && (master)) {
56 if ((master) && (this == slave)) {
65 PneumaticPlatform::collision(GameObject& other, const CollisionHit& )
68 // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
69 MovingObject* mo = dynamic_cast<MovingObject*>(&other);
70 if (!mo) return FORCE_MOVE;
71 if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
73 Player* pl = dynamic_cast<Player*>(mo);
75 if (pl->is_big()) contacts.insert(0);
76 Portable* po = pl->get_grabbed_object();
77 MovingObject* pomo = dynamic_cast<MovingObject*>(po);
78 if (pomo) contacts.insert(pomo);
81 contacts.insert(&other);
86 PneumaticPlatform::update(float elapsed_time)
89 Sector::current()->add_object(new PneumaticPlatform(this));
96 offset_y = -master->offset_y;
97 movement = Vector(0, (start_y + offset_y) - get_pos().y);
100 int contact_diff = contacts.size() - slave->contacts.size();
102 slave->contacts.clear();
104 speed_y += ((float)contact_diff * elapsed_time) * 128.0f;
105 speed_y -= (offset_y * elapsed_time * 0.5f);
106 speed_y *= 1 - elapsed_time;
107 offset_y += speed_y * elapsed_time;
108 if (offset_y < -256) { offset_y = -256; speed_y = 0; }
109 if (offset_y > 256) { offset_y = 256; speed_y = -0; }
110 movement = Vector(0, (start_y + offset_y) - get_pos().y);