SnowParticleSystem Class Reference

#include <snow_particle_system.hpp>

Inherits ParticleSystem.

List of all members.

Public Member Functions

 SnowParticleSystem ()
virtual ~SnowParticleSystem ()
void parse (const Reader &lisp)
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
std::string type () const

Private Types

enum  State {
  ATTACKING, DECAYING, SUSTAINING, RELEASING,
  RESTING, MAX_STATE
}

Private Member Functions

 SnowParticleSystem (const SnowParticleSystem &)
SnowParticleSystemoperator= (const SnowParticleSystem &)

Private Attributes

State state
Timer timer
float gust_onset
float gust_current_velocity
SurfacePtr snowimages [3]

Classes

class  SnowParticle


Detailed Description

Definition at line 23 of file snow_particle_system.hpp.


Member Enumeration Documentation

enum SnowParticleSystem::State [private]

Enumerator:
ATTACKING 
DECAYING 
SUSTAINING 
RELEASING 
RESTING 
MAX_STATE 

Definition at line 64 of file snow_particle_system.hpp.

00064              {
00065     ATTACKING,
00066     DECAYING,
00067     SUSTAINING,
00068     RELEASING,
00069     RESTING,
00070     MAX_STATE
00071   };


Constructor & Destructor Documentation

SnowParticleSystem::SnowParticleSystem (  ) 

Definition at line 36 of file snow_particle_system.cpp.

References Surface::create(), graphicsRandom, ParticleSystem::particles, RandomGenerator::rand(), RandomGenerator::randf(), SCREEN_HEIGHT, SCREEN_WIDTH, snowimages, SNOW::SPIN_SPEED, Timer::start(), timer, and ParticleSystem::virtual_width.

00036                                        :
00037   state(RELEASING),
00038   timer(),
00039   gust_onset(0),
00040   gust_current_velocity(0)
00041 {
00042   snowimages[0] = Surface::create("images/objects/particles/snow2.png");
00043   snowimages[1] = Surface::create("images/objects/particles/snow1.png");
00044   snowimages[2] = Surface::create("images/objects/particles/snow0.png");
00045 
00046   virtual_width = SCREEN_WIDTH * 2;
00047 
00048   timer.start(.01);
00049 
00050   // create some random snowflakes
00051   size_t snowflakecount = size_t(virtual_width/10.0);
00052   for(size_t i=0; i<snowflakecount; ++i) {
00053     SnowParticle* particle = new SnowParticle;
00054     int snowsize = graphicsRandom.rand(3);
00055 
00056     particle->pos.x = graphicsRandom.randf(virtual_width);
00057     particle->pos.y = graphicsRandom.randf(SCREEN_HEIGHT);
00058     particle->anchorx = particle->pos.x + (graphicsRandom.randf(-0.5, 0.5) * 16);
00059     // drift will change with wind gusts
00060     particle->drift_speed = graphicsRandom.randf(-0.5, 0.5) * 0.3;
00061     particle->wobble = 0.0;
00062 
00063     particle->texture = snowimages[snowsize];
00064     particle->flake_size = powf(snowsize+3,4); // since it ranges from 0 to 2
00065 
00066     particle->speed = 2 * (1 + (2 - snowsize)/2 + graphicsRandom.randf(1.8)) * 10; // gravity
00067 
00068     // Spinning
00069     particle->angle = graphicsRandom.randf(360.0);
00070     particle->spin_speed = graphicsRandom.randf(-SNOW::SPIN_SPEED,SNOW::SPIN_SPEED);
00071 
00072     particles.push_back(particle);
00073   }
00074 }

SnowParticleSystem::~SnowParticleSystem (  )  [virtual]

Definition at line 82 of file snow_particle_system.cpp.

00083 {
00084 }

SnowParticleSystem::SnowParticleSystem ( const SnowParticleSystem  )  [private]


Member Function Documentation

void SnowParticleSystem::parse ( const Reader lisp  ) 

Definition at line 77 of file snow_particle_system.cpp.

References LAYER_BACKGROUND1, reader_get_layer(), and ParticleSystem::z_pos.

Referenced by Sector::parse_object().

00078 {
00079   z_pos = reader_get_layer (reader, /* default = */ LAYER_BACKGROUND1);
00080 }

void SnowParticleSystem::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 86 of file snow_particle_system.cpp.

References ATTACKING, Timer::check(), SNOW::DECAY_RATIO, DECAYING, SNOW::EPSILON, Timer::get_timeleft(), graphicsRandom, gust_current_velocity, gust_onset, MAX_STATE, ParticleSystem::particles, RandomGenerator::randf(), RELEASING, RESTING, Timer::start(), state, SNOW::STATE_LENGTH, SUSTAINING, timer, SNOW::WIND_SPEED, SNOW::WOBBLE_DECAY, and SNOW::WOBBLE_FACTOR.

00087 {
00088   // Simple ADSR wind gusts
00089 
00090   if (timer.check()) {
00091     // Change state
00092     state = (State) ((state + 1) % MAX_STATE);
00093 
00094     if(state == RESTING) {
00095       // stop wind
00096       gust_current_velocity = 0;
00097       // new wind strength
00098       gust_onset   = graphicsRandom.randf(-SNOW::WIND_SPEED, SNOW::WIND_SPEED);
00099     }
00100     timer.start(graphicsRandom.randf(SNOW::STATE_LENGTH));
00101   }
00102 
00103   // Update velocities
00104   switch(state) {
00105     case ATTACKING:
00106       gust_current_velocity += gust_onset * elapsed_time;
00107       break;
00108     case DECAYING:
00109       gust_current_velocity -= gust_onset * elapsed_time * SNOW::DECAY_RATIO;
00110       break;
00111     case RELEASING:
00112       // uses current time/velocity instead of constants
00113       gust_current_velocity -= gust_current_velocity * elapsed_time / timer.get_timeleft();
00114       break;
00115     case SUSTAINING:
00116     case RESTING:
00117       //do nothing
00118       break;
00119     default:
00120       assert(false);
00121   }
00122 
00123   std::vector<Particle*>::iterator i;
00124 
00125   for(i = particles.begin(); i != particles.end(); ++i) {
00126     SnowParticle* particle = (SnowParticle*) *i;
00127     float anchor_delta;
00128 
00129     // Falling
00130     particle->pos.y += particle->speed * elapsed_time;
00131     // Drifting (speed approaches wind at a rate dependent on flake size)
00132     particle->drift_speed += (gust_current_velocity - particle->drift_speed) / particle->flake_size + graphicsRandom.randf(-SNOW::EPSILON,SNOW::EPSILON);
00133     particle->anchorx += particle->drift_speed * elapsed_time;
00134     // Wobbling (particle approaches anchorx)
00135     particle->pos.x += particle->wobble * elapsed_time;
00136     anchor_delta = (particle->anchorx - particle->pos.x);
00137     particle->wobble += (SNOW::WOBBLE_FACTOR * anchor_delta) + graphicsRandom.randf(-SNOW::EPSILON, SNOW::EPSILON);
00138     particle->wobble *= SNOW::WOBBLE_DECAY;
00139     // Spinning
00140     particle->angle += particle->spin_speed * elapsed_time;
00141     particle->angle = fmodf(particle->angle, 360.0);
00142   }
00143 }

std::string SnowParticleSystem::type (  )  const [inline]

Definition at line 33 of file snow_particle_system.hpp.

00034   { return "SnowParticleSystem"; }

SnowParticleSystem& SnowParticleSystem::operator= ( const SnowParticleSystem  )  [private]


Member Data Documentation

State SnowParticleSystem::state [private]

Definition at line 72 of file snow_particle_system.hpp.

Referenced by update().

Timer SnowParticleSystem::timer [private]

Definition at line 76 of file snow_particle_system.hpp.

Referenced by SnowParticleSystem(), and update().

float SnowParticleSystem::gust_onset [private]

Definition at line 79 of file snow_particle_system.hpp.

Referenced by update().

float SnowParticleSystem::gust_current_velocity [private]

Definition at line 79 of file snow_particle_system.hpp.

Referenced by update().

SurfacePtr SnowParticleSystem::snowimages[3] [private]

Definition at line 83 of file snow_particle_system.hpp.

Referenced by SnowParticleSystem().


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