From: Ondřej Hošek Date: Wed, 11 May 2005 05:22:46 +0000 (+0000) Subject: * Fixed a typographic mistake in scripting.xml (closed a bracket) X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=5587a88b9d533556461090744e50d070dcd70e14;p=supertux.git * Fixed a typographic mistake in scripting.xml (closed a bracket) * Added a pretty simple but versatile script-firing trigger. SVN-Revision: 2465 --- diff --git a/docs/scripting/scripting.xml b/docs/scripting/scripting.xml index 69b8043ae..efbf97ce9 100644 --- a/docs/scripting/scripting.xml +++ b/docs/scripting/scripting.xml @@ -253,7 +253,7 @@ WOOT.set_animation("left"); set_visible Usage: Text.set_visible(bool visible) -Effect: Shows or hides the text abruptly (drastic counterpart to fade_in and fade_out. +Effect: Shows or hides the text abruptly (drastic counterpart to fade_in and fade_out). diff --git a/src/trigger/scripttrigger.cpp b/src/trigger/scripttrigger.cpp new file mode 100644 index 000000000..1d2ccb84d --- /dev/null +++ b/src/trigger/scripttrigger.cpp @@ -0,0 +1,105 @@ +// $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.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") diff --git a/src/trigger/scripttrigger.h b/src/trigger/scripttrigger.h new file mode 100644 index 000000000..321be3142 --- /dev/null +++ b/src/trigger/scripttrigger.h @@ -0,0 +1,42 @@ +// $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. + +#ifndef __SCRIPTTRIGGER_H__ +#define __SCRIPTTRIGGER_H__ + +#include "trigger_base.h" +#include "serializable.h" + +class ScriptTrigger : public TriggerBase, public Serializable +{ +public: + ScriptTrigger(const lisp::Lisp& reader); + ScriptTrigger(const Vector& pos, const std::string& script); + ~ScriptTrigger(); + + void write(lisp::Writer& writer); + void event(Player& player, EventType type); + +private: + EventType triggerevent; + std::string script; +}; + +#endif