src/object/pushbutton.cpp

Go to the documentation of this file.
00001 //  SuperTux - PushButton running a script
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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 "audio/sound_manager.hpp"
00018 #include "object/player.hpp"
00019 #include "object/pushbutton.hpp"
00020 #include "sprite/sprite.hpp"
00021 #include "supertux/object_factory.hpp"
00022 #include "supertux/sector.hpp"
00023 #include "util/reader.hpp"
00024 
00025 #include <sstream>
00026 #include <stdexcept>
00027 
00028 namespace {
00029 const std::string BUTTON_SOUND = "sounds/switch.ogg";
00030 //14 -> 8
00031 }
00032 
00033 PushButton::PushButton(const Reader& lisp) :
00034   MovingSprite(lisp, "images/objects/pushbutton/pushbutton.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING), 
00035   script(),
00036   state(OFF)
00037 {
00038   sound_manager->preload(BUTTON_SOUND);
00039   set_action("off", -1);
00040   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00041 
00042   if (!lisp.get("script", script)) throw std::runtime_error("no script set");
00043 }
00044 
00045 void
00046 PushButton::update(float /*elapsed_time*/)
00047 {
00048 }
00049 
00050 HitResponse
00051 PushButton::collision(GameObject& other, const CollisionHit& hit)
00052 {
00053   Player* player = dynamic_cast<Player*>(&other);
00054   if (!player) return FORCE_MOVE;
00055   float vy = player->get_physic().get_velocity_y();
00056 
00057   //player->add_velocity(Vector(0, -150));
00058   player->get_physic().set_velocity_y(-150);
00059 
00060   if (state != OFF) return FORCE_MOVE;
00061   if (!hit.top) return FORCE_MOVE;
00062   if (vy <= 0) return FORCE_MOVE;
00063 
00064   // change appearance
00065   state = ON;
00066   float old_bbox_height = bbox.get_height();
00067   set_action("on", -1);
00068   float new_bbox_height = bbox.get_height();
00069   set_pos(get_pos() + Vector(0, old_bbox_height - new_bbox_height));
00070 
00071   // play sound
00072   sound_manager->play(BUTTON_SOUND);
00073 
00074   // run script
00075   std::istringstream stream(script);
00076   Sector::current()->run_script(stream, "PushButton");
00077 
00078   return FORCE_MOVE;
00079 }
00080 
00081 /* EOF */

Generated on Mon Apr 21 03:38:15 2014 for SuperTux by  doxygen 1.5.1