src/trigger/switch.cpp

Go to the documentation of this file.
00001 //  SuperTux - Switch Trigger
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 <config.h>
00018 #include <stdexcept>
00019 
00020 #include "audio/sound_manager.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "sprite/sprite_manager.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "supertux/sector.hpp"
00025 #include "trigger/switch.hpp"
00026 
00027 #include <sstream>
00028 
00029 namespace {
00030 const std::string SWITCH_SOUND = "sounds/switch.ogg";
00031 }
00032 
00033 Switch::Switch(const Reader& reader) :
00034   sprite_name(),
00035   sprite(),
00036   script(),
00037   state(OFF)
00038 {
00039   if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
00040   if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
00041   if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
00042   sprite = sprite_manager->create(sprite_name);
00043   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00044 
00045   if (!reader.get("script", script)) throw std::runtime_error("no script set");
00046   sound_manager->preload( SWITCH_SOUND );
00047 }
00048 
00049 Switch::~Switch()
00050 {
00051 }
00052 
00053 void
00054 Switch::update(float )
00055 {
00056   switch (state) {
00057     case OFF:
00058       break;
00059     case TURN_ON:
00060       if(sprite->animation_done()) {
00061         std::istringstream stream(script);
00062         std::ostringstream location;
00063         location << "switch" << bbox.p1;
00064         Sector::current()->run_script(stream, location.str());
00065 
00066         sprite->set_action("on", 1);
00067         state = ON;
00068       }
00069       break;
00070     case ON:
00071       if(sprite->animation_done()) {
00072         sprite->set_action("turnoff", 1);
00073         state = TURN_OFF;
00074       }
00075       break;
00076     case TURN_OFF:
00077       if(sprite->animation_done()) {
00078         sprite->set_action("off");
00079         state = OFF;
00080       }
00081       break;
00082   }
00083 }
00084 
00085 void
00086 Switch::draw(DrawingContext& context)
00087 {
00088   sprite->draw(context, bbox.p1, LAYER_TILES);
00089 }
00090 
00091 void
00092 Switch::event(Player& , EventType type)
00093 {
00094   if(type != EVENT_ACTIVATE) return;
00095 
00096   switch (state) {
00097     case OFF:
00098       sprite->set_action("turnon", 1);
00099       sound_manager->play( SWITCH_SOUND );
00100       state = TURN_ON;
00101       break;
00102     case TURN_ON:
00103       break;
00104     case ON:
00105       break;
00106     case TURN_OFF:
00107       break;
00108   }
00109 
00110 }
00111 
00112 /* EOF */

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