X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Ftrigger%2Fscripttrigger.cpp;h=29700e2e4af46585ffcbefaedd15bb798b4282b5;hb=08813a74da6ac1fd045a105e4e8105f1d7f716f0;hp=7a7c27a538d69db01f30a99084da5f7b06d04dfd;hpb=aeec9baba33ec63d2d0fdd97d961a53ec5c1541d;p=supertux.git diff --git a/src/trigger/scripttrigger.cpp b/src/trigger/scripttrigger.cpp index 7a7c27a53..29700e2e4 100644 --- a/src/trigger/scripttrigger.cpp +++ b/src/trigger/scripttrigger.cpp @@ -1,107 +1,72 @@ -// $Id$ -// -// SuperTux -// Copyright (C) 2005 Matthias Braun -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. -// -#include - -#include - -#include "scripttrigger.h" -#include "game_session.h" -#include "lisp/lisp.h" -#include "lisp/writer.h" -#include "object_factory.h" -#include "scripting/script_interpreter.h" -#include "sector.h" - -ScriptTrigger::ScriptTrigger(const lisp::Lisp& reader) -{ - bool must_activate; - - reader.get("x", bbox.p1.x); - reader.get("y", bbox.p1.y); - float w, h; - reader.get("width", w); - reader.get("height", h); - bbox.set_size(w, h); - reader.get("script", script); - reader.get("button", must_activate); - - if (must_activate) - triggerevent = EVENT_ACTIVATE; - else - triggerevent = EVENT_TOUCH; -} - -ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script) -{ - bbox.set_pos(pos); - bbox.set_size(32, 32); - this->script = script; - triggerevent = EVENT_TOUCH; -} - -ScriptTrigger::~ScriptTrigger() -{ -} - -void -ScriptTrigger::write(lisp::Writer& writer) -{ - writer.start_list("scripttrigger"); - - writer.write_float("x", bbox.p1.x); - writer.write_float("y", bbox.p1.y); - writer.write_float("width", bbox.get_width()); - writer.write_float("height", bbox.get_height()); - writer.write_string("script", script); - writer.write_bool("button", (triggerevent == EVENT_ACTIVATE) ? true : false); - - writer.end_list("scripttrigger"); -} - -void -ScriptTrigger::event(Player& , EventType type) -{ - if(type == triggerevent) - { - if (script != "") - { - try - { - ScriptInterpreter* interpreter = new ScriptInterpreter(Sector::current()); - std::istringstream in(script); - interpreter->load_script(in, "trigger-script"); - interpreter->start_script(); - Sector::current()->add_object(interpreter); - } - catch(std::exception& e) - { - std::cerr << "Couldn't execute trigger script: " << e.what() << "\n"; - } - } - else - { - std::cerr << "... and where's the trigger script I'm supposed to run?\n"; - } - } -} - -IMPLEMENT_FACTORY(ScriptTrigger, "scripttrigger"); - +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include +#include +#include + +#include "supertux/object_factory.hpp" +#include "supertux/sector.hpp" +#include "trigger/scripttrigger.hpp" +#include "util/reader.hpp" + +ScriptTrigger::ScriptTrigger(const Reader& reader) +{ + bool must_activate = false; + + reader.get("x", bbox.p1.x); + reader.get("y", bbox.p1.y); + float w = 0, h = 0; + reader.get("width", w); + reader.get("height", h); + bbox.set_size(w, h); + reader.get("script", script); + reader.get("button", must_activate); + if(script == "") { + throw std::runtime_error("Need to specify a script for trigger object"); + } + + if (must_activate) + triggerevent = EVENT_ACTIVATE; + else + triggerevent = EVENT_TOUCH; +} + +ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script) +{ + bbox.set_pos(pos); + bbox.set_size(32, 32); + this->script = script; + triggerevent = EVENT_TOUCH; +} + +ScriptTrigger::~ScriptTrigger() +{ +} + +void +ScriptTrigger::event(Player& , EventType type) +{ + if(type != triggerevent) + return; + + std::istringstream stream(script); + Sector::current()->run_script(stream, "ScriptTrigger"); +} + +IMPLEMENT_FACTORY(ScriptTrigger, "scripttrigger"); + +/* EOF */