src/object/pneumatic_platform.cpp

Go to the documentation of this file.
00001 //  SuperTux - PneumaticPlatform
00002 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/pneumatic_platform.hpp"
00018 
00019 #include "object/player.hpp"
00020 #include "object/portable.hpp"
00021 #include "supertux/object_factory.hpp"
00022 #include "supertux/sector.hpp"
00023 
00024 PneumaticPlatform::PneumaticPlatform(const Reader& reader) :
00025   MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC), 
00026   master(0), 
00027   slave(0), 
00028   start_y(0),
00029   offset_y(0), 
00030   speed_y(0),
00031   contacts()
00032 {
00033   start_y = get_pos().y;
00034 }
00035 
00036 PneumaticPlatform::PneumaticPlatform(PneumaticPlatform* master) :
00037   MovingSprite(*master), 
00038   master(master), 
00039   slave(this), 
00040   start_y(master->start_y),
00041   offset_y(-master->offset_y), 
00042   speed_y(0),
00043   contacts()
00044 {
00045   set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
00046   master->master = master;
00047   master->slave = this;
00048 }
00049 
00050 PneumaticPlatform::~PneumaticPlatform() 
00051 {
00052   if ((this == master) && (master)) {
00053     slave->master = 0;
00054     slave->slave = 0;
00055   }
00056   if ((master) && (this == slave)) {
00057     master->master = 0;
00058     master->slave = 0;
00059   }
00060   master = 0;
00061   slave = 0;
00062 }
00063 
00064 HitResponse
00065 PneumaticPlatform::collision(GameObject& other, const CollisionHit& )
00066 {
00067 
00068   // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
00069   MovingObject* mo = dynamic_cast<MovingObject*>(&other);
00070   if (!mo) return FORCE_MOVE;
00071   if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
00072 
00073   Player* pl = dynamic_cast<Player*>(mo);
00074   if (pl) {
00075     if (pl->is_big()) contacts.insert(0);
00076     Portable* po = pl->get_grabbed_object();
00077     MovingObject* pomo = dynamic_cast<MovingObject*>(po);
00078     if (pomo) contacts.insert(pomo);
00079   }
00080 
00081   contacts.insert(&other);
00082   return FORCE_MOVE;
00083 }
00084 
00085 void
00086 PneumaticPlatform::update(float elapsed_time)
00087 {
00088   if (!slave) {
00089     Sector::current()->add_object(new PneumaticPlatform(this));
00090     return;
00091   }
00092   if (!master) {
00093     return;
00094   }
00095   if (this == slave) {
00096     offset_y = -master->offset_y;
00097     movement = Vector(0, (start_y + offset_y) - get_pos().y);
00098   }
00099   if (this == master) {
00100     int contact_diff = contacts.size() - slave->contacts.size();
00101     contacts.clear();
00102     slave->contacts.clear();
00103 
00104     speed_y += ((float)contact_diff * elapsed_time) * 128.0f;
00105     speed_y -= (offset_y * elapsed_time * 0.5f);
00106     speed_y *= 1 - elapsed_time;
00107     offset_y += speed_y * elapsed_time;
00108     if (offset_y < -256) { offset_y = -256; speed_y = 0; } 
00109     if (offset_y > 256) { offset_y = 256; speed_y = -0; } 
00110     movement = Vector(0, (start_y + offset_y) - get_pos().y);
00111   }
00112 }
00113 
00114 /* EOF */

Generated on Mon Jun 9 03:38:20 2014 for SuperTux by  doxygen 1.5.1