From fe875e6f18c4ed968f7b1534b4039c8a580b852b Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Thu, 13 Jul 2006 00:08:15 +0000 Subject: [PATCH] Thunderstorm SVN-Revision: 3994 --- data/images/engine/editor/thunderstorm.png | Bin 0 -> 2212 bytes data/levels/test/kugelblitz.stl | 171 ++++++++++------------ src/object/thunderstorm.cpp | 152 ++++++++++++++++++++ src/object/thunderstorm.hpp | 84 +++++++++++ src/scripting/candle.cpp | 2 +- src/scripting/candle.hpp | 2 +- src/scripting/platform.cpp | 2 +- src/scripting/platform.hpp | 2 +- src/scripting/thunderstorm.hpp | 70 +++++++++ src/scripting/wrapper.cpp | 224 +++++++++++++++++++++++++++++ src/scripting/wrapper.hpp | 1 + src/scripting/wrapper.interface.hpp | 1 + 12 files changed, 610 insertions(+), 101 deletions(-) create mode 100644 data/images/engine/editor/thunderstorm.png create mode 100644 src/object/thunderstorm.cpp create mode 100644 src/object/thunderstorm.hpp create mode 100644 src/scripting/thunderstorm.hpp diff --git a/data/images/engine/editor/thunderstorm.png b/data/images/engine/editor/thunderstorm.png new file mode 100644 index 0000000000000000000000000000000000000000..3c0babde5222d1f1393680d6abf8e9d2f14e20c5 GIT binary patch literal 2212 zcmV;V2wV4wP)Nklo=wpEbB2Qch(Fm^rH=gwfnyR`rp*Y5LBNZrZ@i`u6H>w>>*^ zKge);c422{`_`>@b86Wz%0j68{(}|9G!)96_1hd+r_+*=+u3O(pnvqs*?T)i_r-8gpipwk{xv_cq$Yildp^_Hp{c5!ra;-C`B zBxY=D<30~lfFEZQA@*qY zzSl9hh=F_lCY06?xWZyxL%|_QwCgt~Is0d)yW{jjOVf(u_Zu1Y)9dfWVaykfRR~ZK z+;t;jNtn1y;fzAp^_*UZNR>(KbZW7bZnwoCSyzJyICt&?WyUBnM1P&rZ0AP*BmF75@HEqhzWc>lc^ed_K}5?Im<4T zCrDD>9_@A=g1EiAjuTS{q?C-wN>M*?bUd3yRS}$VKkQ2x5^&F#$Py`GZ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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 "thunderstorm.hpp" +#include "scripting/squirrel_util.hpp" +#include "audio/sound_manager.hpp" +#include "video/drawing_context.hpp" +#include "object_factory.hpp" +#include "object/electrifier.hpp" + +#include +#include +#include "main.hpp" +#include "resources.hpp" +#include "sector.hpp" +#include "gettext.hpp" +#include "object/player.hpp" +#include "lisp/list_iterator.hpp" +#include "log.hpp" + +namespace { + const float LIGHTNING_DELAY = 2.0; + const float FLASH_DISPLAY_TIME = 0.1; +} + +Thunderstorm::Thunderstorm(const lisp::Lisp& reader) + : name(""), running(true), interval(10.0f) +{ + reader.get("name", name); + reader.get("running", running); + reader.get("interval", interval); + if(interval <= 0) { + log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl; + } + + sound_manager->preload("sounds/explosion.wav"); + sound_manager->preload("sounds/upgrade.wav"); + + if (running) { + running = false; // else start() is ignored + start(); + } +} + +void +Thunderstorm::update(float ) +{ + if (!running) return; + if (time_to_thunder.check()) { + thunder(); + time_to_lightning.start(LIGHTNING_DELAY); + } + if (time_to_lightning.check()) { + lightning(); + time_to_thunder.start(interval); + } +} + +void +Thunderstorm::draw(DrawingContext& context) +{ + if (!flash_display_timer.started()) return; + + float alpha = 0.33f; + context.push_transform(); + context.set_translation(Vector(0, 0)); + context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), LAYER_BACKGROUNDTILES-1); + context.pop_transform(); + +} + +void +Thunderstorm::expose(HSQUIRRELVM vm, SQInteger table_idx) +{ + if (name == "") return; + Scripting::Thunderstorm* interface = static_cast(this); + expose_object(vm, table_idx, interface, name, true); +} + +void +Thunderstorm::unexpose(HSQUIRRELVM vm, SQInteger table_idx) +{ + if (name == "") return; + Scripting::unexpose_object(vm, table_idx, name); +} + +void +Thunderstorm::start() +{ + if (running) return; + running = true; + time_to_thunder.start(interval); + time_to_lightning.stop(); +} + +void +Thunderstorm::stop() +{ + if (!running) return; + running = false; + time_to_thunder.stop(); + time_to_lightning.stop(); +} + +void +Thunderstorm::thunder() +{ + sound_manager->play("sounds/explosion.wav"); +} + +void +Thunderstorm::lightning() +{ + flash(); + electrify(); +} + +void +Thunderstorm::flash() +{ + sound_manager->play("sounds/upgrade.wav"); + sound_manager->play("sounds/explosion.wav"); + flash_display_timer.start(FLASH_DISPLAY_TIME); +} + +void +Thunderstorm::electrify() +{ + Sector::current()->add_object(new Electrifier(75, 1421, 0.5)); + Sector::current()->add_object(new Electrifier(76, 1422, 0.5)); +} + +IMPLEMENT_FACTORY(Thunderstorm, "thunderstorm"); + diff --git a/src/object/thunderstorm.hpp b/src/object/thunderstorm.hpp new file mode 100644 index 000000000..804312f4c --- /dev/null +++ b/src/object/thunderstorm.hpp @@ -0,0 +1,84 @@ +// $Id$ +// +// SuperTux - Thunderstorm Game Object +// Copyright (C) 2006 Christoph Sommer +// +// 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 __THUNDERSTORM_H__ +#define __THUNDERSTORM_H__ + +#include "game_object.hpp" +#include "timer.hpp" +#include "lisp/lisp.hpp" +#include "scripting/thunderstorm.hpp" +#include "script_interface.hpp" + +/** + * Thunderstorm scriptable GameObject; plays thunder, lightning and electrifies water at regular interval + */ +class Thunderstorm : public GameObject, public Scripting::Thunderstorm, public ScriptInterface +{ +public: + Thunderstorm(const lisp::Lisp& reader); + + void update(float elapsed_time); + void draw(DrawingContext& context); + + virtual void expose(HSQUIRRELVM vm, SQInteger table_idx); + virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx); + + /** + * Start playing thunder and lightning at configured interval + */ + void start(); + + /** + * Stop playing thunder and lightning at configured interval + */ + void stop(); + + /** + * Play thunder + */ + void thunder(); + + /** + * Play lightning, i.e. call flash() and electrify() + */ + void lightning(); + + /** + * Display a nice flash + */ + void flash(); + + /** + * Electrify water throughout the whole sector for a short time + */ + void electrify(); + +private: + std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ + bool running; /**< whether we currently automatically trigger lightnings */ + float interval; /**< time between two lightnings */ + + Timer time_to_thunder; /**< counts down until next thunder */ + Timer time_to_lightning; /**< counts down until next lightning */ + Timer flash_display_timer; /**< counts down while flash is displayed */ +}; + +#endif + diff --git a/src/scripting/candle.cpp b/src/scripting/candle.cpp index 1a4d3a655..cab136cd8 100644 --- a/src/scripting/candle.cpp +++ b/src/scripting/candle.cpp @@ -1,4 +1,4 @@ -// $Id: candle.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $ +// $Id$ // // SuperTux // Copyright (C) 2006 Matthias Braun diff --git a/src/scripting/candle.hpp b/src/scripting/candle.hpp index d68f30f64..c7c022ee4 100644 --- a/src/scripting/candle.hpp +++ b/src/scripting/candle.hpp @@ -1,4 +1,4 @@ -// $Id: candle.hpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $ +// $Id$ // // SuperTux // Copyright (C) 2006 Matthias Braun diff --git a/src/scripting/platform.cpp b/src/scripting/platform.cpp index b0f52dcf0..cb20a73eb 100644 --- a/src/scripting/platform.cpp +++ b/src/scripting/platform.cpp @@ -1,4 +1,4 @@ -// $Id: platform.cpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $ +// $Id$ // // SuperTux // Copyright (C) 2006 Matthias Braun diff --git a/src/scripting/platform.hpp b/src/scripting/platform.hpp index 85115d7cb..1bff5ca2d 100644 --- a/src/scripting/platform.hpp +++ b/src/scripting/platform.hpp @@ -1,4 +1,4 @@ -// $Id: platform.hpp 3327 2006-04-13 15:02:40Z ravu_al_hemio $ +// $Id$ // // SuperTux // Copyright (C) 2006 Matthias Braun diff --git a/src/scripting/thunderstorm.hpp b/src/scripting/thunderstorm.hpp new file mode 100644 index 000000000..79e350bdc --- /dev/null +++ b/src/scripting/thunderstorm.hpp @@ -0,0 +1,70 @@ +// $Id: player.hpp 3962 2006-07-09 10:32:59Z matzebraun $ +// +// 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 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 __SCRIPTING_THUNDERSTORM_H__ +#define __SCRIPTING_THUNDERSTORM_H__ + +namespace Scripting +{ + +class Thunderstorm +{ +public: +#ifndef SCRIPTING_API + virtual ~Thunderstorm() + {} +#endif + + /** + * Start playing thunder and lightning at configured interval + */ + virtual void start() = 0; + + /** + * Stop playing thunder and lightning at configured interval + */ + virtual void stop() = 0; + + /** + * Play thunder + */ + virtual void thunder() = 0; + + /** + * Play lightning, i.e. call flash() and electrify() + */ + virtual void lightning() = 0; + + /** + * Display a nice flash + */ + virtual void flash() = 0; + + /** + * Electrify water throughout the whole sector for a short time + */ + virtual void electrify() = 0; + + +}; + +} + +#endif + diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index a175623f7..a90bb81b8 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -2093,6 +2093,157 @@ static SQInteger AmbientSound_get_pos_y_wrapper(HSQUIRRELVM vm) } +static SQInteger Thunderstorm_release_hook(SQUserPointer ptr, SQInteger ) +{ + Scripting::Thunderstorm* _this = reinterpret_cast (ptr); + delete _this; + return 0; +} + +static SQInteger Thunderstorm_start_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'start' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->start(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'start'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_stop_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'stop' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->stop(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'stop'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_thunder_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'thunder' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->thunder(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'thunder'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_lightning_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'lightning' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->lightning(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'lightning'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_flash_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'flash' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->flash(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'flash'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_electrify_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'electrify' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->electrify(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'electrify'")); + return SQ_ERROR; + } + +} + static SQInteger display_wrapper(HSQUIRRELVM vm) { return Scripting::display(vm); @@ -2969,6 +3120,32 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::AmbientSound* object, bo sq_remove(v, -2); // remove root table } +void create_squirrel_instance(HSQUIRRELVM v, Scripting::Thunderstorm* object, bool setup_releasehook) +{ + using namespace Wrapper; + + sq_pushroottable(v); + sq_pushstring(v, "Thunderstorm", -1); + if(SQ_FAILED(sq_get(v, -2))) { + std::ostringstream msg; + msg << "Couldn't resolved squirrel type 'Thunderstorm'"; + throw SquirrelError(v, msg.str()); + } + + if(SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, object))) { + std::ostringstream msg; + msg << "Couldn't setup squirrel instance for object of type 'Thunderstorm'"; + throw SquirrelError(v, msg.str()); + } + sq_remove(v, -2); // remove object name + + if(setup_releasehook) { + sq_setreleasehook(v, -1, Thunderstorm_release_hook); + } + + sq_remove(v, -2); // remove root table +} + void register_supertux_wrapper(HSQUIRRELVM v) { using namespace Wrapper; @@ -3754,6 +3931,53 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register class 'AmbientSound'"); } + // Register class Thunderstorm + sq_pushstring(v, "Thunderstorm", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::ostringstream msg; + msg << "Couldn't create new class 'Thunderstorm'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "start", -1); + sq_newclosure(v, &Thunderstorm_start_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'start'"); + } + + sq_pushstring(v, "stop", -1); + sq_newclosure(v, &Thunderstorm_stop_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'stop'"); + } + + sq_pushstring(v, "thunder", -1); + sq_newclosure(v, &Thunderstorm_thunder_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'thunder'"); + } + + sq_pushstring(v, "lightning", -1); + sq_newclosure(v, &Thunderstorm_lightning_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'lightning'"); + } + + sq_pushstring(v, "flash", -1); + sq_newclosure(v, &Thunderstorm_flash_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'flash'"); + } + + sq_pushstring(v, "electrify", -1); + sq_newclosure(v, &Thunderstorm_electrify_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'electrify'"); + } + + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register class 'Thunderstorm'"); + } + } } // end of namespace Scripting diff --git a/src/scripting/wrapper.hpp b/src/scripting/wrapper.hpp index b4c9bd932..f93747031 100644 --- a/src/scripting/wrapper.hpp +++ b/src/scripting/wrapper.hpp @@ -25,6 +25,7 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::Platform* object, bool s void create_squirrel_instance(HSQUIRRELVM v, Scripting::Candle* object, bool setup_releasehook = false); void create_squirrel_instance(HSQUIRRELVM v, Scripting::Wind* object, bool setup_releasehook = false); void create_squirrel_instance(HSQUIRRELVM v, Scripting::AmbientSound* object, bool setup_releasehook = false); +void create_squirrel_instance(HSQUIRRELVM v, Scripting::Thunderstorm* object, bool setup_releasehook = false); } diff --git a/src/scripting/wrapper.interface.hpp b/src/scripting/wrapper.interface.hpp index d7ae9204f..9da2ff27d 100644 --- a/src/scripting/wrapper.interface.hpp +++ b/src/scripting/wrapper.interface.hpp @@ -12,4 +12,5 @@ #include "candle.hpp" #include "wind.hpp" #include "ambient_sound.hpp" +#include "thunderstorm.hpp" -- 2.11.0