bd166d3f0c20ee461945cd938a0d99f70703004d
[supertux.git] / src / object / bicycle_platform.cpp
1 //  $Id$
2 //
3 //  SuperTux - BicyclePlatform
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "bicycle_platform.hpp"
23
24 #include <math.h>
25 #include <stdexcept>
26 #include "log.hpp"
27 #include "video/drawing_context.hpp"
28 #include "resources.hpp"
29 #include "player.hpp"
30 #include "path.hpp"
31 #include "path_walker.hpp"
32 #include "sprite/sprite.hpp"
33 #include "lisp/lisp.hpp"
34 #include "object_factory.hpp"
35 #include "sector.hpp"
36 #include "object/portable.hpp"
37
38 BicyclePlatform::BicyclePlatform(const lisp::Lisp& reader)
39         : MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC), 
40         master(0), slave(0), radius(128), angle(0), angular_speed(0), momentum(0)
41 {
42   center = get_pos();
43 }
44
45 BicyclePlatform::BicyclePlatform(BicyclePlatform* master)
46         : MovingSprite(*master), 
47         master(master), slave(this), center(master->center), radius(master->radius), angle(master->angle + M_PI), angular_speed(0), momentum(0)
48 {
49   set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
50   master->master = master;
51   master->slave = this;
52 }
53
54 BicyclePlatform::~BicyclePlatform() {
55   if ((this == master) && (master)) {
56     slave->master = 0;
57     slave->slave = 0;
58   }
59   if ((master) && (this == slave)) {
60     master->master = 0;
61     master->slave = 0;
62   }
63   master = 0;
64   slave = 0;
65 }
66
67 HitResponse
68 BicyclePlatform::collision(GameObject& other, const CollisionHit& )
69 {
70
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;
75
76   Player* pl = dynamic_cast<Player*>(mo);
77   if (pl) {
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;
82   }
83
84   if (contacts.insert(&other).second) momentum += 1;
85   return FORCE_MOVE;
86 }
87
88 void
89 BicyclePlatform::update(float elapsed_time)
90 {
91   if (!slave) {
92     Sector::current()->add_object(new BicyclePlatform(this));
93     return;
94   }
95   if (!master) {
96     return;
97   }
98   if (this == slave) {
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();
104   }
105   if (this == master) {
106     float momentum_diff = momentum - slave->momentum;
107     contacts.clear(); momentum = 0;
108     slave->contacts.clear(); slave->momentum = 0;
109
110     float angular_momentum = cosf(angle) * momentum_diff;
111
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();
120
121     center += Vector(angular_speed, 0) * elapsed_time * 32;
122     slave->center += Vector(angular_speed, 0) * elapsed_time * 32;
123
124   }
125 }
126
127 IMPLEMENT_FACTORY(BicyclePlatform, "bicycle-platform");
128