3 // SuperTux - PushButton running a script
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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.
23 #include "pushbutton.hpp"
24 #include "object_factory.hpp"
26 #include "audio/sound_manager.hpp"
27 #include "sprite/sprite_manager.hpp"
32 const std::string BUTTON_SOUND = "sounds/switch.ogg";
36 PushButton::PushButton(const lisp::Lisp& lisp)
37 : MovingSprite(lisp, "images/objects/pushbutton/pushbutton.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING), state(OFF)
39 sound_manager->preload(BUTTON_SOUND);
40 set_action("off", -1);
41 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
43 if (!lisp.get("script", script)) throw std::runtime_error("no script set");
47 PushButton::update(float /*elapsed_time*/)
52 PushButton::collision(GameObject& other, const CollisionHit& hit)
54 Player* player = dynamic_cast<Player*>(&other);
55 if (!player) return FORCE_MOVE;
56 float vy = player->physic.get_velocity_y();
58 //player->add_velocity(Vector(0, -150));
59 player->physic.set_velocity_y(-150);
61 if (state != OFF) return FORCE_MOVE;
62 if (!hit.top) return FORCE_MOVE;
63 if (vy <= 0) return FORCE_MOVE;
67 float old_bbox_height = bbox.get_height();
69 float new_bbox_height = bbox.get_height();
70 set_pos(get_pos() + Vector(0, old_bbox_height - new_bbox_height));
73 sound_manager->play(BUTTON_SOUND);
76 std::istringstream stream(script);
77 Sector::current()->run_script(stream, "PushButton");
82 IMPLEMENT_FACTORY(PushButton, "pushbutton");