src/trigger/scripttrigger.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 <memory>
00018 #include <sstream>
00019 #include <stdexcept>
00020 
00021 #include "supertux/object_factory.hpp"
00022 #include "supertux/sector.hpp"
00023 #include "trigger/scripttrigger.hpp"
00024 #include "util/reader.hpp"
00025 
00026 ScriptTrigger::ScriptTrigger(const Reader& reader) :
00027   triggerevent(),
00028   script()
00029 {
00030   bool must_activate = false;
00031 
00032   reader.get("x", bbox.p1.x);
00033   reader.get("y", bbox.p1.y);
00034   float w = 0, h = 0;
00035   reader.get("width", w);
00036   reader.get("height", h);
00037   bbox.set_size(w, h);
00038   reader.get("script", script);
00039   reader.get("button", must_activate);
00040   if(script == "") {
00041     throw std::runtime_error("Need to specify a script for trigger object");
00042   }
00043 
00044   if (must_activate)
00045     triggerevent = EVENT_ACTIVATE;
00046   else
00047     triggerevent = EVENT_TOUCH;
00048 }
00049 
00050 ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script) :
00051   triggerevent(),
00052   script()
00053 {
00054   bbox.set_pos(pos);
00055   bbox.set_size(32, 32);
00056   this->script = script;
00057   triggerevent = EVENT_TOUCH;
00058 }
00059 
00060 ScriptTrigger::~ScriptTrigger()
00061 {
00062 }
00063 
00064 void
00065 ScriptTrigger::event(Player& , EventType type)
00066 {
00067   if(type != triggerevent)
00068     return;
00069 
00070   std::istringstream stream(script);
00071   Sector::current()->run_script(stream, "ScriptTrigger");
00072 }
00073 
00074 /* EOF */

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