Explosion Class Reference

Just your average explosion - goes boom, hurts Tux. More...

#include <explosion.hpp>

Inherits MovingSprite.

List of all members.

Public Member Functions

 Explosion (const Vector &pos)
 Create new Explosion centered(!) at pos.
 Explosion (const Reader &reader)
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
bool hurts (void) const
void hurts (bool val)
bool pushes (void) const
void pushes (bool val)

Protected Member Functions

void explode ()
 plays sound, starts animation

Private Types

enum  State { STATE_WAITING, STATE_EXPLODING }

Private Attributes

bool hurt
bool push
State state


Detailed Description

Just your average explosion - goes boom, hurts Tux.

Definition at line 25 of file explosion.hpp.


Member Enumeration Documentation

enum Explosion::State [private]

Enumerator:
STATE_WAITING 
STATE_EXPLODING 

Definition at line 64 of file explosion.hpp.

00064              {
00065     STATE_WAITING,
00066     STATE_EXPLODING
00067   };


Constructor & Destructor Documentation

Explosion::Explosion ( const Vector pos  ) 

Create new Explosion centered(!) at pos.

Definition at line 30 of file explosion.cpp.

References MovingObject::get_bbox(), MovingObject::get_pos(), SoundManager::preload(), MovingObject::set_pos(), and sound_manager.

00030                                       :
00031   MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
00032   hurt(true),
00033   push(false),
00034   state(STATE_WAITING)
00035 {
00036   sound_manager->preload("sounds/explosion.wav");
00037   set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
00038 }

Explosion::Explosion ( const Reader reader  ) 

Definition at line 40 of file explosion.cpp.

References SoundManager::preload(), and sound_manager.

00040                                          :
00041   MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
00042   hurt(true),
00043   push(false),
00044   state(STATE_WAITING)
00045 {
00046   sound_manager->preload("sounds/explosion.wav");
00047 }


Member Function Documentation

void Explosion::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)

Reimplemented from MovingSprite.

Definition at line 112 of file explosion.cpp.

References explode(), GameObject::remove_me(), MovingSprite::sprite, state, STATE_EXPLODING, and STATE_WAITING.

00113 {
00114   switch(state) {
00115     case STATE_WAITING:
00116       explode();
00117       break;
00118     case STATE_EXPLODING:
00119       if(sprite->animation_done()) {
00120         remove_me();
00121       }
00122       break;
00123   }
00124 }

HitResponse Explosion::collision ( GameObject other,
const CollisionHit hit 
) [virtual]

this function is called when the object collided with any other object

Implements MovingObject.

Definition at line 127 of file explosion.cpp.

References ABORT_MOVE, hurt, Player::kill(), BadGuy::kill_fall(), state, and STATE_EXPLODING.

00128 {
00129   if ((state != STATE_EXPLODING) || !hurt)
00130     return ABORT_MOVE;
00131 
00132   Player* player = dynamic_cast<Player*>(&other);
00133   if(player != 0) {
00134     player->kill(false);
00135   }
00136 
00137   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
00138   if(badguy != 0) {
00139     badguy->kill_fall();
00140   }
00141 
00142   return ABORT_MOVE;
00143 }

bool Explosion::hurts ( void   )  const [inline]

Definition at line 37 of file explosion.hpp.

References hurt.

Referenced by SkyDive::explode(), and ShortFuse::explode().

00038   {
00039     return this->hurt;
00040   }

void Explosion::hurts ( bool  val  )  [inline]

Definition at line 42 of file explosion.hpp.

References hurt.

00043   {
00044     this->hurt = val;
00045   }

bool Explosion::pushes ( void   )  const [inline]

Definition at line 47 of file explosion.hpp.

References push.

Referenced by SkyDive::explode(), and ShortFuse::explode().

00048   {
00049     return this->push;
00050   }

void Explosion::pushes ( bool  val  )  [inline]

Definition at line 52 of file explosion.hpp.

References push.

00053   {
00054     this->push = val;
00055   }

void Explosion::explode (  )  [protected]

plays sound, starts animation

Definition at line 50 of file explosion.cpp.

References Sector::add_object(), WalkingBadguy::add_velocity(), Player::add_velocity(), ANCHOR_MIDDLE, MovingObject::bbox, Sector::current(), MovingObject::get_bbox(), Rectf::get_middle(), Sector::get_nearby_objects(), MovingObject::get_pos(), graphicsRandom, LAYER_OBJECTS, Vector::norm(), SoundManager::play(), push, RandomGenerator::randf(), MovingSprite::set_action(), sound_manager, MovingSprite::sprite, state, STATE_EXPLODING, STATE_WAITING, and Vector::unit().

Referenced by update().

00051 {
00052   if (state != STATE_WAITING)
00053     return;
00054   state = STATE_EXPLODING;
00055 
00056   set_action("default", 1);
00057   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
00058   sound_manager->play("sounds/explosion.wav", get_pos());
00059 
00060 #if 0
00061   // spawn some particles
00062   // TODO: provide convenience function in MovingSprite or MovingObject?
00063   for (int i = 0; i < 100; i++) {
00064     Vector ppos = bbox.get_middle();
00065     float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
00066     float velocity = graphicsRandom.randf(450, 900);
00067     float vx = sin(angle)*velocity;
00068     float vy = -cos(angle)*velocity;
00069     Vector pspeed = Vector(vx, vy);
00070     Vector paccel = Vector(0, 1000);
00071     Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
00072   }
00073 #endif
00074 
00075   if (push) {
00076     Vector center = get_bbox ().get_middle ();
00077     std::vector<MovingObject*> near_objects = Sector::current()->get_nearby_objects (center, 10.0 * 32.0);
00078 
00079     for (size_t i = 0; i < near_objects.size (); i++) {
00080       MovingObject *obj = near_objects[i];
00081       Vector obj_vector = obj->get_bbox ().get_middle ();
00082       Vector direction = obj_vector - center;
00083       float distance = direction.norm ();
00084 
00085       /* If the distance is very small, for example because "obj" is the badguy
00086        * causing the explosion, skip this object. */
00087       if (distance <= 1.0)
00088         continue;
00089 
00090       /* The force decreases with the distance squared. In the distance of one
00091        * tile (32 pixels) you will have a speed increase of 150 pixels/s. */
00092       float force = 150.0 * 32.0*32.0 / (distance * distance);
00093       if (force > 200.0)
00094         force = 200.0;
00095 
00096       Vector add_speed = direction.unit () * force;
00097 
00098       Player *player = dynamic_cast<Player *> (obj);
00099       if (player) {
00100         player->add_velocity (add_speed);
00101       }
00102 
00103       WalkingBadguy *badguy = dynamic_cast<WalkingBadguy *> (obj);
00104       if (badguy) {
00105         badguy->add_velocity (add_speed);
00106       }
00107     } /* for (i = 0 ... near_objects) */
00108   } /* if (push) */
00109 }


Member Data Documentation

bool Explosion::hurt [private]

Definition at line 68 of file explosion.hpp.

Referenced by collision(), and hurts().

bool Explosion::push [private]

Definition at line 69 of file explosion.hpp.

Referenced by explode(), and pushes().

State Explosion::state [private]

Definition at line 70 of file explosion.hpp.

Referenced by collision(), explode(), and update().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:30 2014 for SuperTux by  doxygen 1.5.1