| 1 | //  SuperTux - PushButton running a script | 
| 2 | //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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/pushbutton.hpp" | 
| 18 |  | 
| 19 | #include "audio/sound_manager.hpp" | 
| 20 | #include "object/player.hpp" | 
| 21 | #include "sprite/sprite.hpp" | 
| 22 | #include "supertux/sector.hpp" | 
| 23 | #include "util/reader_mapping.hpp" | 
| 24 |  | 
| 25 | namespace { | 
| 26 | const std::string BUTTON_SOUND = "sounds/switch.ogg" ; | 
| 27 | //14 -> 8 | 
| 28 | } | 
| 29 |  | 
| 30 | PushButton::PushButton(const ReaderMapping& mapping) : | 
| 31 |   MovingSprite(mapping, "images/objects/pushbutton/pushbutton.sprite" , LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING), | 
| 32 |   script(), | 
| 33 |   state(OFF) | 
| 34 | { | 
| 35 |   SoundManager::current()->preload(BUTTON_SOUND); | 
| 36 |   set_action("off" , -1); | 
| 37 |   m_col.m_bbox.set_size(m_sprite->get_current_hitbox_width(), m_sprite->get_current_hitbox_height()); | 
| 38 |  | 
| 39 |   if (!mapping.get("script" , script)) { | 
| 40 |     log_warning << "No script set for pushbutton."  << std::endl; | 
| 41 |   } | 
| 42 | } | 
| 43 |  | 
| 44 | ObjectSettings | 
| 45 | PushButton::get_settings() | 
| 46 | { | 
| 47 |   ObjectSettings result = MovingSprite::get_settings(); | 
| 48 |  | 
| 49 |   result.add_script(_("Script" ), &script, "script" ); | 
| 50 |  | 
| 51 |   result.reorder({"script" , "x" , "y" }); | 
| 52 |  | 
| 53 |   return result; | 
| 54 | } | 
| 55 |  | 
| 56 | void | 
| 57 | PushButton::update(float /*dt_sec*/) | 
| 58 | { | 
| 59 | } | 
| 60 |  | 
| 61 | HitResponse | 
| 62 | PushButton::collision(GameObject& other, const CollisionHit& hit) | 
| 63 | { | 
| 64 |   auto player = dynamic_cast<Player*>(&other); | 
| 65 |   if (!player) return FORCE_MOVE; | 
| 66 |   float vy = player->get_physic().get_velocity_y(); | 
| 67 |  | 
| 68 |   //player->add_velocity(Vector(0, -150)); | 
| 69 |   player->get_physic().set_velocity_y(-150); | 
| 70 |  | 
| 71 |   if (state != OFF) return FORCE_MOVE; | 
| 72 |   if (!hit.top) return FORCE_MOVE; | 
| 73 |   if (vy <= 0) return FORCE_MOVE; | 
| 74 |  | 
| 75 |   // change appearance | 
| 76 |   state = ON; | 
| 77 |   float old_bbox_height = m_col.m_bbox.get_height(); | 
| 78 |   set_action("on" , -1); | 
| 79 |   float new_bbox_height = m_col.m_bbox.get_height(); | 
| 80 |   set_pos(get_pos() + Vector(0, old_bbox_height - new_bbox_height)); | 
| 81 |  | 
| 82 |   // play sound | 
| 83 |   SoundManager::current()->play(BUTTON_SOUND); | 
| 84 |  | 
| 85 |   // run script | 
| 86 |   Sector::get().run_script(script, "PushButton" ); | 
| 87 |  | 
| 88 |   return FORCE_MOVE; | 
| 89 | } | 
| 90 |  | 
| 91 | /* EOF */ | 
| 92 |  |