Thunderstorm Class Reference

Thunderstorm scriptable GameObject; plays thunder, lightning and electrifies water at regular interval. More...

#include <thunderstorm.hpp>

Inherits GameObject, and ScriptInterface.

List of all members.

Public Member Functions

 Thunderstorm (const Reader &reader)
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
virtual void expose (HSQUIRRELVM vm, SQInteger table_idx)
virtual void unexpose (HSQUIRRELVM vm, SQInteger table_idx)
Scriptable Methods
void start ()
 Start playing thunder and lightning at configured interval.
void stop ()
 Stop playing thunder and lightning at configured interval.
void thunder ()
 Play thunder.
void lightning ()
 Play lightning, i.e.
void flash ()
 Display a nice flash.
void electrify ()
 Electrify water throughout the whole sector for a short time.

Private Attributes

bool running
 whether we currently automatically trigger lightnings
float interval
 time between two lightnings
int layer
 layer, where flash will be painted
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


Detailed Description

Thunderstorm scriptable GameObject; plays thunder, lightning and electrifies water at regular interval.

Definition at line 30 of file thunderstorm.hpp.


Constructor & Destructor Documentation

Thunderstorm::Thunderstorm ( const Reader reader  ) 

Definition at line 33 of file thunderstorm.cpp.

References lisp::Lisp::get(), interval, layer, LAYER_BACKGROUNDTILES, log_warning, GameObject::name, SoundManager::preload(), reader_get_layer(), running, sound_manager, and start().

00033                                                :
00034   running(true),
00035   interval(10.0f), 
00036   layer(LAYER_BACKGROUNDTILES-1),
00037   time_to_thunder(),
00038   time_to_lightning(),
00039   flash_display_timer()
00040 {
00041   reader.get("name", name);
00042   reader.get("running", running);
00043   reader.get("interval", interval);
00044   if(interval <= 0) {
00045     log_warning << "Running a thunderstorm with non-positive time interval is a bad idea" << std::endl;
00046   }
00047   layer = reader_get_layer (reader, /* default = */ LAYER_BACKGROUNDTILES-1);
00048 
00049   sound_manager->preload("sounds/thunder.wav");
00050   sound_manager->preload("sounds/lightning.wav");
00051 
00052   if (running) {
00053     running = false; // else start() is ignored
00054     start();
00055   }
00056 }


Member Function Documentation

void Thunderstorm::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Implements GameObject.

Definition at line 59 of file thunderstorm.cpp.

References Timer::check(), interval, lightning(), LIGHTNING_DELAY, running, Timer::start(), thunder(), time_to_lightning, and time_to_thunder.

00060 {
00061   if (!running) return;
00062   if (time_to_thunder.check()) {
00063     thunder();
00064     time_to_lightning.start(LIGHTNING_DELAY);
00065   }
00066   if (time_to_lightning.check()) {
00067     lightning();
00068     time_to_thunder.start(interval);
00069   }
00070 }

void Thunderstorm::draw ( DrawingContext context  )  [virtual]

The GameObject should draw itself onto the provided DrawingContext if this function is called.

Implements GameObject.

Definition at line 73 of file thunderstorm.cpp.

References DrawingContext::draw_filled_rect(), flash_display_timer, layer, DrawingContext::pop_transform(), DrawingContext::push_transform(), SCREEN_HEIGHT, SCREEN_WIDTH, DrawingContext::set_translation(), and Timer::started().

00074 {
00075   if (!flash_display_timer.started()) return;
00076 
00077   float alpha = 0.33f;
00078   context.push_transform();
00079   context.set_translation(Vector(0, 0));
00080   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(1, 1, 1, alpha), layer);
00081   context.pop_transform();
00082 
00083 }

void Thunderstorm::expose ( HSQUIRRELVM  vm,
SQInteger  table_idx 
) [virtual]

Implements ScriptInterface.

Definition at line 86 of file thunderstorm.cpp.

References scripting::expose_object(), and GameObject::name.

00087 {
00088   if (name == "") return;
00089   scripting::Thunderstorm* _this = new scripting::Thunderstorm(this);
00090   expose_object(vm, table_idx, _this, name, true);
00091 }

void Thunderstorm::unexpose ( HSQUIRRELVM  vm,
SQInteger  table_idx 
) [virtual]

Implements ScriptInterface.

Definition at line 94 of file thunderstorm.cpp.

References GameObject::name, and scripting::unexpose_object().

00095 {
00096   if (name == "") return;
00097   scripting::unexpose_object(vm, table_idx, name);
00098 }

void Thunderstorm::start (  ) 

Start playing thunder and lightning at configured interval.

Definition at line 101 of file thunderstorm.cpp.

References interval, running, Timer::start(), Timer::stop(), time_to_lightning, and time_to_thunder.

Referenced by scripting::Thunderstorm::start(), and Thunderstorm().

00102 {
00103   if (running) return;
00104   running = true;
00105   time_to_thunder.start(interval);
00106   time_to_lightning.stop();
00107 }

void Thunderstorm::stop (  ) 

Stop playing thunder and lightning at configured interval.

Definition at line 110 of file thunderstorm.cpp.

References running, Timer::stop(), time_to_lightning, and time_to_thunder.

Referenced by scripting::Thunderstorm::stop().

00111 {
00112   if (!running) return;
00113   running = false;
00114   time_to_thunder.stop();
00115   time_to_lightning.stop();
00116 }

void Thunderstorm::thunder (  ) 

Play thunder.

Definition at line 119 of file thunderstorm.cpp.

References SoundManager::play(), and sound_manager.

Referenced by scripting::Thunderstorm::thunder(), and update().

00120 {
00121   sound_manager->play("sounds/thunder.wav");
00122 }

void Thunderstorm::lightning (  ) 

Play lightning, i.e.

call flash() and electrify()

Definition at line 125 of file thunderstorm.cpp.

References electrify(), and flash().

Referenced by scripting::Thunderstorm::lightning(), and update().

00126 {
00127   flash();
00128   electrify();
00129 }

void Thunderstorm::flash (  ) 

Display a nice flash.

Definition at line 132 of file thunderstorm.cpp.

References FLASH_DISPLAY_TIME, flash_display_timer, SoundManager::play(), sound_manager, and Timer::start().

Referenced by scripting::Thunderstorm::flash(), and lightning().

00133 {
00134   sound_manager->play("sounds/lightning.wav");
00135   flash_display_timer.start(FLASH_DISPLAY_TIME);
00136 }

void Thunderstorm::electrify (  ) 

Electrify water throughout the whole sector for a short time.

Definition at line 139 of file thunderstorm.cpp.

References Sector::add_object(), Sector::current(), and ELECTRIFY_TIME.

Referenced by scripting::Thunderstorm::electrify(), and lightning().

00140 {
00141   Sector::current()->add_object(new Electrifier(200, 1421, ELECTRIFY_TIME));
00142   Sector::current()->add_object(new Electrifier(201, 1422, ELECTRIFY_TIME));
00143 }


Member Data Documentation

bool Thunderstorm::running [private]

whether we currently automatically trigger lightnings

Definition at line 82 of file thunderstorm.hpp.

Referenced by start(), stop(), Thunderstorm(), and update().

float Thunderstorm::interval [private]

time between two lightnings

Definition at line 83 of file thunderstorm.hpp.

Referenced by start(), Thunderstorm(), and update().

int Thunderstorm::layer [private]

layer, where flash will be painted

Definition at line 84 of file thunderstorm.hpp.

Referenced by draw(), and Thunderstorm().

Timer Thunderstorm::time_to_thunder [private]

counts down until next thunder

Definition at line 86 of file thunderstorm.hpp.

Referenced by start(), stop(), and update().

Timer Thunderstorm::time_to_lightning [private]

counts down until next lightning

Definition at line 87 of file thunderstorm.hpp.

Referenced by start(), stop(), and update().

Timer Thunderstorm::flash_display_timer [private]

counts down while flash is displayed

Definition at line 88 of file thunderstorm.hpp.

Referenced by draw(), and flash().


The documentation for this class was generated from the following files:
Generated on Mon Apr 21 03:38:31 2014 for SuperTux by  doxygen 1.5.1