1 // SuperTux - BicyclePlatform
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/bicycle_platform.hpp"
21 #include "object/player.hpp"
22 #include "object/portable.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
26 BicyclePlatform::BicyclePlatform(const Reader& reader) :
27 MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC),
38 BicyclePlatform::BicyclePlatform(BicyclePlatform* master) :
39 MovingSprite(*master),
42 center(master->center),
43 radius(master->radius),
44 angle(master->angle + M_PI),
48 set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
49 master->master = master;
53 BicyclePlatform::~BicyclePlatform()
55 if ((this == master) && (master)) {
59 if ((master) && (this == slave)) {
68 BicyclePlatform::collision(GameObject& other, const CollisionHit& )
71 // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
72 MovingObject* mo = dynamic_cast<MovingObject*>(&other);
73 if (!mo) return FORCE_MOVE;
74 if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
76 Player* pl = dynamic_cast<Player*>(mo);
78 if (pl->is_big()) momentum += 1;
79 Portable* po = pl->get_grabbed_object();
80 MovingObject* pomo = dynamic_cast<MovingObject*>(po);
81 if (contacts.insert(pomo).second) momentum += 1;
84 if (contacts.insert(&other).second) momentum += 1;
89 BicyclePlatform::update(float elapsed_time)
92 Sector::current()->add_object(new BicyclePlatform(this));
99 angle = master->angle + M_PI;
100 while (angle < 0) { angle += 2*M_PI; }
101 while (angle > 2*M_PI) { angle -= 2*M_PI; }
102 Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size() * 0.5);
103 movement = dest - get_pos();
105 if (this == master) {
106 float momentum_diff = momentum - slave->momentum;
107 contacts.clear(); momentum = 0;
108 slave->contacts.clear(); slave->momentum = 0;
110 float angular_momentum = cosf(angle) * momentum_diff;
112 angular_speed += (angular_momentum * elapsed_time) * M_PI;
113 angular_speed *= 1 - elapsed_time * 0.2;
114 angle += angular_speed * elapsed_time;
115 while (angle < 0) { angle += 2*M_PI; }
116 while (angle > 2*M_PI) { angle -= 2*M_PI; }
117 angular_speed = std::min(std::max(angular_speed, static_cast<float>(-128*M_PI*elapsed_time)), static_cast<float>(128*M_PI*elapsed_time));
118 Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size() * 0.5);
119 movement = dest - get_pos();
121 center += Vector(angular_speed, 0) * elapsed_time * 32;
122 slave->center += Vector(angular_speed, 0) * elapsed_time * 32;
127 IMPLEMENT_FACTORY(BicyclePlatform, "bicycle-platform");