Updated addon repository URL and improved debug output on download
[supertux.git] / src / object / bicycle_platform.cpp
1 //  SuperTux - BicyclePlatform
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "object/bicycle_platform.hpp"
18
19 #include <math.h>
20
21 #include "object/player.hpp"
22 #include "object/portable.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 BicyclePlatform::BicyclePlatform(const Reader& reader) :
27   MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC),
28   master(0),
29   slave(0),
30   center(),
31   radius(128),
32   angle(0),
33   angular_speed(0),
34   contacts(),
35   momentum(0)
36 {
37   center = get_pos();
38 }
39
40 BicyclePlatform::BicyclePlatform(BicyclePlatform* master_) :
41   MovingSprite(*master_),
42   master(master_),
43   slave(this),
44   center(master->center),
45   radius(master->radius),
46   angle(master->angle + M_PI),
47   angular_speed(0),
48   contacts(),
49   momentum(0)
50 {
51   set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
52   master->master = master;
53   master->slave = this;
54 }
55
56 BicyclePlatform::~BicyclePlatform()
57 {
58   if ((this == master) && (master)) {
59     slave->master = 0;
60     slave->slave = 0;
61   }
62   if ((master) && (this == slave)) {
63     master->master = 0;
64     master->slave = 0;
65   }
66   master = 0;
67   slave = 0;
68 }
69
70 HitResponse
71 BicyclePlatform::collision(GameObject& other, const CollisionHit& )
72 {
73
74   // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
75   MovingObject* mo = dynamic_cast<MovingObject*>(&other);
76   if (!mo) return FORCE_MOVE;
77   if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
78
79   Player* pl = dynamic_cast<Player*>(mo);
80   if (pl) {
81     if (pl->is_big()) momentum += 1;
82     Portable* po = pl->get_grabbed_object();
83     MovingObject* pomo = dynamic_cast<MovingObject*>(po);
84     if (contacts.insert(pomo).second) momentum += 1;
85   }
86
87   if (contacts.insert(&other).second) momentum += 1;
88   return FORCE_MOVE;
89 }
90
91 void
92 BicyclePlatform::update(float elapsed_time)
93 {
94   if (!slave) {
95     Sector::current()->add_object(std::make_shared<BicyclePlatform>(this));
96     return;
97   }
98   if (!master) {
99     return;
100   }
101   if (this == slave) {
102     angle = master->angle + M_PI;
103     while (angle < 0) { angle += 2*M_PI; }
104     while (angle > 2*M_PI) { angle -= 2*M_PI; }
105     Vector dest_ = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size().as_vector() * 0.5);
106     movement = dest_ - get_pos();
107   }
108   if (this == master) {
109     float momentum_diff = momentum - slave->momentum;
110     contacts.clear(); momentum = 0;
111     slave->contacts.clear(); slave->momentum = 0;
112
113     float angular_momentum = cosf(angle) * momentum_diff;
114
115     angular_speed += (angular_momentum * elapsed_time) * M_PI;
116     angular_speed *= 1 - elapsed_time * 0.2;
117     angle += angular_speed * elapsed_time;
118     while (angle < 0) { angle += 2*M_PI; }
119     while (angle > 2*M_PI) { angle -= 2*M_PI; }
120     angular_speed = std::min(std::max(angular_speed, static_cast<float>(-128*M_PI*elapsed_time)), static_cast<float>(128*M_PI*elapsed_time));
121     Vector dest_ = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size().as_vector() * 0.5);
122     movement = dest_ - get_pos();
123
124     center += Vector(angular_speed, 0) * elapsed_time * 32;
125     slave->center += Vector(angular_speed, 0) * elapsed_time * 32;
126
127   }
128 }
129
130 /* EOF */