00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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 )
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
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
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
00072 sound_manager->play(BUTTON_SOUND);
00073
00074
00075 std::istringstream stream(script);
00076 Sector::current()->run_script(stream, "PushButton");
00077
00078 return FORCE_MOVE;
00079 }
00080
00081