WillOWisp Class Reference

#include <willowisp.hpp>

Inherits BadGuy, scripting::WillOWisp, and ScriptInterface.

List of all members.

Public Member Functions

 WillOWisp (const Reader &reader)
void activate ()
 called when the badguy has been activated.
void deactivate ()
 called when the badguy has been deactivated
void active_update (float elapsed_time)
 called each frame when the badguy is activated.
virtual bool is_flammable () const
 Returns whether to call ignite() when a badguy gets hit by a fire bullet.
virtual bool is_freezable () const
virtual void kill_fall ()
 Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned upside-down).
void vanish ()
 make WillOWisp vanish
virtual void draw (DrawingContext &context)
 Called when the badguy is drawn.
virtual void goto_node (int node_no)
 Move willowisp to given node.
virtual void set_state (const std::string &state)
 set willowisp state; can be: -stopped willowisp doesn't move -move_path willowisp moves along the path (call goto_node) -move_path_track willowisp moves along path but catches tux when he is near -normal "normal" mode starts tracking tux when he is near enough -vanish vanish
virtual void start_moving ()
virtual void stop_moving ()
virtual void expose (HSQUIRRELVM vm, SQInteger table_idx)
virtual void unexpose (HSQUIRRELVM vm, SQInteger table_idx)

Protected Member Functions

virtual bool collides (GameObject &other, const CollisionHit &hit)
 when 2 objects collided, we will first call the pre_collision_check functions of both objects that can decide on how to react to the collision.
HitResponse collision_player (Player &player, const CollisionHit &hit)
 Called when the badguy collided with a player.

Private Types

enum  MyState {
  STATE_STOPPED, STATE_IDLE, STATE_TRACKING, STATE_VANISHING,
  STATE_WARPING, STATE_PATHMOVING, STATE_PATHMOVING_TRACK
}

Private Attributes

MyState mystate
std::string target_sector
std::string target_spawnpoint
std::string hit_script
std::auto_ptr< SoundSourcesound_source
std::auto_ptr< Pathpath
std::auto_ptr< PathWalkerwalker
float flyspeed
float track_range
float vanish_range


Detailed Description

Definition at line 29 of file willowisp.hpp.


Member Enumeration Documentation

enum WillOWisp::MyState [private]

Enumerator:
STATE_STOPPED 
STATE_IDLE 
STATE_TRACKING 
STATE_VANISHING 
STATE_WARPING 
STATE_PATHMOVING 
STATE_PATHMOVING_TRACK 

Definition at line 64 of file willowisp.hpp.


Constructor & Destructor Documentation

WillOWisp::WillOWisp ( const Reader reader  ) 

Definition at line 36 of file willowisp.cpp.

References BadGuy::countMe, FLYSPEED, flyspeed, lisp::Lisp::get(), lisp::Lisp::get_lisp(), hit_script, mystate, GameObject::name, path, SoundManager::preload(), sound_manager, SOUNDFILE, MovingSprite::sprite, STATE_PATHMOVING_TRACK, target_sector, target_spawnpoint, TRACK_RANGE, track_range, VANISH_RANGE, vanish_range, and walker.

00036                                          :
00037   BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), 
00038   mystate(STATE_IDLE), 
00039   target_sector("main"), 
00040   target_spawnpoint("main"),
00041   hit_script(),
00042   sound_source(),
00043   path(),
00044   walker(),
00045   flyspeed(),
00046   track_range(),
00047   vanish_range()
00048 {
00049   bool running = false;
00050   flyspeed     = FLYSPEED;
00051   track_range  = TRACK_RANGE;
00052   vanish_range = VANISH_RANGE;
00053 
00054   reader.get("sector", target_sector);
00055   reader.get("spawnpoint", target_spawnpoint);
00056   reader.get("name", name);
00057   reader.get("flyspeed", flyspeed);
00058   reader.get("track-range", track_range);
00059   reader.get("vanish-range", vanish_range);
00060   reader.get("hit-script", hit_script);
00061   reader.get("running", running);
00062 
00063   const lisp::Lisp* pathLisp = reader.get_lisp("path");
00064   if(pathLisp != NULL) {
00065     path.reset(new Path());
00066     path->read(*pathLisp);
00067     walker.reset(new PathWalker(path.get(), running));
00068     if(running)
00069       mystate = STATE_PATHMOVING_TRACK;
00070   }
00071 
00072   countMe = false;
00073   sound_manager->preload(SOUNDFILE);
00074   sound_manager->preload("sounds/warp.wav");
00075 
00076   sprite->set_action("idle");
00077 }


Member Function Documentation

void WillOWisp::activate (  )  [virtual]

called when the badguy has been activated.

(As a side effect the dir variable might have been changed so that it faces towards the player.

Reimplemented from BadGuy.

Definition at line 154 of file willowisp.cpp.

References SoundManager::create_sound_source(), MovingObject::get_pos(), sound_manager, sound_source, and SOUNDFILE.

00155 {
00156   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
00157   sound_source->set_position(get_pos());
00158   sound_source->set_looping(true);
00159   sound_source->set_gain(2.0);
00160   sound_source->set_reference_distance(32);
00161   sound_source->play();
00162 }

void WillOWisp::deactivate (  )  [virtual]

called when the badguy has been deactivated

Reimplemented from BadGuy.

Definition at line 165 of file willowisp.cpp.

References mystate, GameObject::remove_me(), sound_source, STATE_IDLE, STATE_PATHMOVING, STATE_PATHMOVING_TRACK, STATE_STOPPED, STATE_TRACKING, STATE_VANISHING, and STATE_WARPING.

00166 {
00167   sound_source.reset(NULL);
00168 
00169   switch (mystate) {
00170     case STATE_STOPPED:
00171     case STATE_IDLE:
00172     case STATE_PATHMOVING:
00173     case STATE_PATHMOVING_TRACK:
00174       break;
00175     case STATE_TRACKING:
00176       mystate = STATE_IDLE;
00177       break;
00178     case STATE_WARPING:
00179     case STATE_VANISHING:
00180       remove_me();
00181       break;
00182   }
00183 }

void WillOWisp::active_update ( float  elapsed_time  )  [virtual]

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Definition at line 93 of file willowisp.cpp.

References BadGuy::dir, flyspeed, MovingObject::get_bbox(), BadGuy::get_nearest_player(), MovingObject::get_pos(), MovingObject::movement, mystate, Vector::norm(), Rectf::p1, Rectf::p2, GameObject::remove_me(), sound_source, MovingSprite::sprite, STATE_IDLE, STATE_PATHMOVING, STATE_PATHMOVING_TRACK, STATE_STOPPED, STATE_TRACKING, STATE_VANISHING, STATE_WARPING, track_range, Vector::unit(), vanish(), vanish_range, and walker.

00094 {
00095   Player* player = get_nearest_player();
00096   if (!player) return;
00097   Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
00098   Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
00099   Vector dist = (p2 - p1);
00100 
00101   switch(mystate) {
00102     case STATE_STOPPED:
00103       break;
00104 
00105     case STATE_IDLE:
00106       if (dist.norm() <= track_range) {
00107         mystate = STATE_TRACKING;
00108       }
00109       break;
00110 
00111     case STATE_TRACKING:
00112       if (dist.norm() > vanish_range) {
00113         vanish();
00114       } else if (dist.norm() >= 1) {
00115         Vector dir = dist.unit();
00116         movement = dir * elapsed_time * flyspeed;
00117       } else {
00118         /* We somehow landed right on top of the player without colliding.
00119          * Sit tight and avoid a division by zero. */
00120       }
00121       sound_source->set_position(get_pos());
00122       break;
00123 
00124     case STATE_WARPING:
00125       if(sprite->animation_done()) {
00126         remove_me();
00127       }
00128 
00129     case STATE_VANISHING: {
00130       Vector dir = dist.unit();
00131       movement = dir * elapsed_time * flyspeed;
00132       if(sprite->animation_done()) {
00133         remove_me();
00134       }
00135       break;
00136     }
00137 
00138     case STATE_PATHMOVING:
00139     case STATE_PATHMOVING_TRACK:
00140       if(walker.get() == NULL)
00141         return;
00142       movement = walker->advance(elapsed_time) - get_pos();
00143       if(mystate == STATE_PATHMOVING_TRACK && dist.norm() <= track_range) {
00144         mystate = STATE_TRACKING;
00145       }
00146       break;
00147 
00148     default:
00149       assert(false);
00150   }
00151 }

virtual bool WillOWisp::is_flammable (  )  const [inline, virtual]

Returns whether to call ignite() when a badguy gets hit by a fire bullet.

Reimplemented from BadGuy.

Definition at line 40 of file willowisp.hpp.

00040 { return false; }

virtual bool WillOWisp::is_freezable (  )  const [inline, virtual]

Reimplemented from BadGuy.

Definition at line 41 of file willowisp.hpp.

00041 { return false; }

virtual void WillOWisp::kill_fall (  )  [inline, virtual]

Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned upside-down).

Reimplemented from BadGuy.

Definition at line 42 of file willowisp.hpp.

References vanish().

00042 { vanish(); }

void WillOWisp::vanish (  ) 

make WillOWisp vanish

Definition at line 186 of file willowisp.cpp.

References COLGROUP_DISABLED, mystate, BadGuy::set_colgroup_active(), MovingSprite::sprite, and STATE_VANISHING.

Referenced by active_update(), Lantern::collision(), kill_fall(), and set_state().

00187 {
00188   mystate = STATE_VANISHING;
00189   sprite->set_action("vanishing", 1);
00190   set_colgroup_active(COLGROUP_DISABLED);
00191 }

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

Called when the badguy is drawn.

The default implementation simply draws the badguy sprite on screen

Reimplemented from BadGuy.

Definition at line 80 of file willowisp.cpp.

References MovingObject::get_pos(), MovingSprite::layer, DrawingContext::LIGHTMAP, DrawingContext::pop_target(), DrawingContext::push_target(), DrawingContext::set_target(), and MovingSprite::sprite.

00081 {
00082   sprite->draw(context, get_pos(), layer);
00083 
00084   context.push_target();
00085   context.set_target(DrawingContext::LIGHTMAP);
00086 
00087   sprite->draw(context, get_pos(), layer);
00088 
00089   context.pop_target();
00090 }

void WillOWisp::goto_node ( int  node_no  )  [virtual]

Move willowisp to given node.

Implements scripting::WillOWisp.

Definition at line 229 of file willowisp.cpp.

References mystate, STATE_PATHMOVING, STATE_PATHMOVING_TRACK, and walker.

00230 {
00231   walker->goto_node(node_no);
00232   if(mystate != STATE_PATHMOVING && mystate != STATE_PATHMOVING_TRACK) {
00233     mystate = STATE_PATHMOVING;
00234   }
00235 }

void WillOWisp::set_state ( const std::string &  state  )  [virtual]

set willowisp state; can be: -stopped willowisp doesn't move -move_path willowisp moves along the path (call goto_node) -move_path_track willowisp moves along path but catches tux when he is near -normal "normal" mode starts tracking tux when he is near enough -vanish vanish

Implements scripting::WillOWisp.

Definition at line 250 of file willowisp.cpp.

References mystate, STATE_IDLE, STATE_PATHMOVING, STATE_PATHMOVING_TRACK, STATE_STOPPED, vanish(), and walker.

00251 {
00252   if(new_state == "stopped") {
00253     mystate = STATE_STOPPED;
00254   } else if(new_state == "idle") {
00255     mystate = STATE_IDLE;
00256   } else if(new_state == "move_path") {
00257     mystate = STATE_PATHMOVING;
00258     walker->start_moving();
00259   } else if(new_state == "move_path_track") {
00260     mystate = STATE_PATHMOVING_TRACK;
00261     walker->start_moving();
00262   } else if(new_state == "normal") {
00263     mystate = STATE_IDLE;
00264   } else if(new_state == "vanish") {
00265     vanish();
00266   } else {
00267     std::ostringstream msg;
00268     msg << "Can't set unknown willowisp state '" << new_state << "', should "
00269       "be stopped, move_path, move_path_track or normal";
00270     throw new std::runtime_error(msg.str());
00271   }
00272 }

void WillOWisp::start_moving (  )  [virtual]

Implements scripting::WillOWisp.

Definition at line 238 of file willowisp.cpp.

References walker.

00239 {
00240   walker->start_moving();
00241 }

void WillOWisp::stop_moving (  )  [virtual]

Implements scripting::WillOWisp.

Definition at line 244 of file willowisp.cpp.

References walker.

00245 {
00246   walker->stop_moving();
00247 }

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

Implements ScriptInterface.

Definition at line 275 of file willowisp.cpp.

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

00276 {
00277   if (name.empty())
00278     return;
00279 
00280   std::cout << "Expose me '" << name << "'\n";
00281   scripting::WillOWisp* _this = static_cast<scripting::WillOWisp*> (this);
00282   expose_object(vm, table_idx, _this, name);
00283 }

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

Implements ScriptInterface.

Definition at line 286 of file willowisp.cpp.

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

00287 {
00288   if (name.empty())
00289     return;
00290 
00291   std::cout << "UnExpose me '" << name << "'\n";
00292   scripting::unexpose_object(vm, table_idx, name);
00293 }

bool WillOWisp::collides ( GameObject other,
const CollisionHit hit 
) [protected, virtual]

when 2 objects collided, we will first call the pre_collision_check functions of both objects that can decide on how to react to the collision.

Reimplemented from MovingObject.

Definition at line 194 of file willowisp.cpp.

References Lantern::is_open().

00194                                                            {
00195   Lantern* lantern = dynamic_cast<Lantern*>(&other);
00196 
00197   if (lantern && lantern->is_open())
00198     return true;
00199 
00200   if (dynamic_cast<Player*>(&other))
00201     return true;
00202 
00203   return false;
00204 }

HitResponse WillOWisp::collision_player ( Player player,
const CollisionHit hit 
) [protected, virtual]

Called when the badguy collided with a player.

Reimplemented from BadGuy.

Definition at line 207 of file willowisp.cpp.

References ABORT_MOVE, CONTINUE, Currenton< GameSession >::current(), Sector::current(), hit_script, Player::is_invincible(), mystate, SoundManager::play(), GameSession::respawn(), Sector::run_script(), sound_manager, MovingSprite::sprite, STATE_TRACKING, STATE_WARPING, target_sector, and target_spawnpoint.

00207                                                                 {
00208   if(player.is_invincible())
00209     return ABORT_MOVE;
00210 
00211   if (mystate != STATE_TRACKING)
00212     return ABORT_MOVE;
00213 
00214   mystate = STATE_WARPING;
00215   sprite->set_action("warping", 1);
00216 
00217   if(hit_script != "") {
00218     std::istringstream stream(hit_script);
00219     Sector::current()->run_script(stream, "hit-script");
00220   } else {
00221     GameSession::current()->respawn(target_sector, target_spawnpoint);
00222   }
00223   sound_manager->play("sounds/warp.wav");
00224 
00225   return CONTINUE;
00226 }


Member Data Documentation

MyState WillOWisp::mystate [private]

Definition at line 70 of file willowisp.hpp.

Referenced by active_update(), collision_player(), deactivate(), goto_node(), set_state(), vanish(), and WillOWisp().

std::string WillOWisp::target_sector [private]

Definition at line 72 of file willowisp.hpp.

Referenced by collision_player(), and WillOWisp().

std::string WillOWisp::target_spawnpoint [private]

Definition at line 73 of file willowisp.hpp.

Referenced by collision_player(), and WillOWisp().

std::string WillOWisp::hit_script [private]

Definition at line 74 of file willowisp.hpp.

Referenced by collision_player(), and WillOWisp().

std::auto_ptr<SoundSource> WillOWisp::sound_source [private]

Definition at line 76 of file willowisp.hpp.

Referenced by activate(), active_update(), and deactivate().

std::auto_ptr<Path> WillOWisp::path [private]

Definition at line 78 of file willowisp.hpp.

Referenced by WillOWisp().

std::auto_ptr<PathWalker> WillOWisp::walker [private]

Definition at line 79 of file willowisp.hpp.

Referenced by active_update(), goto_node(), set_state(), start_moving(), stop_moving(), and WillOWisp().

float WillOWisp::flyspeed [private]

Definition at line 81 of file willowisp.hpp.

Referenced by active_update(), and WillOWisp().

float WillOWisp::track_range [private]

Definition at line 82 of file willowisp.hpp.

Referenced by active_update(), and WillOWisp().

float WillOWisp::vanish_range [private]

Definition at line 83 of file willowisp.hpp.

Referenced by active_update(), and WillOWisp().


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