cba90384e5f5b5ba46139d4d87d9d3ebcbd31067
[supertux.git] / src / object / pneumatic_platform.cpp
1 //  SuperTux - PneumaticPlatform
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/pneumatic_platform.hpp"
18
19 #include "object/player.hpp"
20 #include "object/portable.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
23
24 PneumaticPlatform::PneumaticPlatform(const Reader& reader) :
25   MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC),
26   master(0),
27   slave(0),
28   start_y(0),
29   offset_y(0),
30   speed_y(0),
31   contacts()
32 {
33   start_y = get_pos().y;
34 }
35
36 PneumaticPlatform::PneumaticPlatform(PneumaticPlatform* master) :
37   MovingSprite(*master),
38   master(master),
39   slave(this),
40   start_y(master->start_y),
41   offset_y(-master->offset_y),
42   speed_y(0),
43   contacts()
44 {
45   set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
46   master->master = master;
47   master->slave = this;
48 }
49
50 PneumaticPlatform::~PneumaticPlatform()
51 {
52   if ((this == master) && (master)) {
53     slave->master = 0;
54     slave->slave = 0;
55   }
56   if ((master) && (this == slave)) {
57     master->master = 0;
58     master->slave = 0;
59   }
60   master = 0;
61   slave = 0;
62 }
63
64 HitResponse
65 PneumaticPlatform::collision(GameObject& other, const CollisionHit& )
66 {
67
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;
72
73   Player* pl = dynamic_cast<Player*>(mo);
74   if (pl) {
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);
79   }
80
81   contacts.insert(&other);
82   return FORCE_MOVE;
83 }
84
85 void
86 PneumaticPlatform::update(float elapsed_time)
87 {
88   if (!slave) {
89     Sector::current()->add_object(new PneumaticPlatform(this));
90     return;
91   }
92   if (!master) {
93     return;
94   }
95   if (this == slave) {
96     offset_y = -master->offset_y;
97     movement = Vector(0, (start_y + offset_y) - get_pos().y);
98   }
99   if (this == master) {
100     int contact_diff = contacts.size() - slave->contacts.size();
101     contacts.clear();
102     slave->contacts.clear();
103
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);
111   }
112 }
113
114 /* EOF */