Particles Class Reference

#include <particles.hpp>

Inherits GameObject.

List of all members.

Public Member Functions

 Particles (const Vector &epicenter, int min_angle, int max_angle, const Vector &initial_velocity, const Vector &acceleration, int number, Color color, int size, float life_time, int drawing_layer)
 ~Particles ()
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
virtual void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.

Private Attributes

Vector accel
Timer timer
bool live_forever
Color color
float size
int drawing_layer
std::vector< Particle * > particles

Classes

struct  Particle


Detailed Description

Definition at line 25 of file particles.hpp.


Constructor & Destructor Documentation

Particles::Particles ( const Vector epicenter,
int  min_angle,
int  max_angle,
const Vector initial_velocity,
const Vector acceleration,
int  number,
Color  color,
int  size,
float  life_time,
int  drawing_layer 
)

Definition at line 27 of file particles.cpp.

References graphicsRandom, live_forever, particles, RandomGenerator::rand(), Timer::start(), timer, Vector::x, and Vector::y.

00029                                                                                    :
00030   accel(acceleration), 
00031   timer(),
00032   live_forever(),
00033   color(color_), 
00034   size(size_), 
00035   drawing_layer(drawing_layer_),
00036   particles()
00037 {
00038   if(life_time == 0) {
00039     live_forever = true;
00040   } else {
00041     live_forever = false;
00042     timer.start(life_time);
00043   }
00044 
00045   // create particles
00046   for(int p = 0; p < number; p++)
00047   {
00048     Particle* particle = new Particle;
00049     particle->pos = epicenter;
00050 
00051     float angle = graphicsRandom.rand(min_angle, max_angle)
00052       * (M_PI / 180);  // convert to radius (radians?)
00053     particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
00054     //    if(angle >= M_PI && angle < M_PI*2)
00055     //      particle->vel.x *= -1;  // work around to fix signal
00056     particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
00057     //    if(angle >= M_PI_2 && angle < 3*M_PI_2)
00058     //      particle->vel.y *= -1;
00059 
00060     particles.push_back(particle);
00061   }
00062 }

Particles::~Particles (  ) 

Definition at line 64 of file particles.cpp.

References particles.

00065 {
00066   // free particles
00067   for(std::vector<Particle*>::iterator i = particles.begin();
00068       i < particles.end(); i++)
00069     delete (*i);
00070 }


Member Function Documentation

void Particles::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 73 of file particles.cpp.

References accel, Sector::camera, scripting::camera(), Timer::check(), Sector::current(), Camera::get_translation(), live_forever, particles, GameObject::remove_me(), SCREEN_HEIGHT, SCREEN_WIDTH, timer, Vector::x, and Vector::y.

00074 {
00075   Vector camera = Sector::current()->camera->get_translation();
00076 
00077   // update particles
00078   for(std::vector<Particle*>::iterator i = particles.begin();
00079       i != particles.end(); ) {
00080     (*i)->pos.x += (*i)->vel.x * elapsed_time;
00081     (*i)->pos.y += (*i)->vel.y * elapsed_time;
00082 
00083     (*i)->vel.x += accel.x * elapsed_time;
00084     (*i)->vel.y += accel.y * elapsed_time;
00085 
00086     if((*i)->pos.x < camera.x || (*i)->pos.x > SCREEN_WIDTH + camera.x ||
00087        (*i)->pos.y < camera.y || (*i)->pos.y > SCREEN_HEIGHT + camera.y) {
00088       delete (*i);
00089       i = particles.erase(i);
00090     } else {
00091       ++i;
00092     }
00093   }
00094 
00095   if((timer.check() && !live_forever) || particles.size() == 0)
00096     remove_me();
00097 }

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

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

Implements GameObject.

Definition at line 100 of file particles.cpp.

References color, DrawingContext::draw_filled_rect(), drawing_layer, particles, and size.

00101 {
00102   // draw particles
00103   for(std::vector<Particle*>::iterator i = particles.begin();
00104       i != particles.end(); i++) {
00105     context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);
00106   }
00107 }


Member Data Documentation

Vector Particles::accel [private]

Definition at line 49 of file particles.hpp.

Referenced by update().

Timer Particles::timer [private]

Definition at line 50 of file particles.hpp.

Referenced by Particles(), and update().

bool Particles::live_forever [private]

Definition at line 51 of file particles.hpp.

Referenced by Particles(), and update().

Color Particles::color [private]

Definition at line 53 of file particles.hpp.

Referenced by draw().

float Particles::size [private]

Definition at line 54 of file particles.hpp.

Referenced by draw().

int Particles::drawing_layer [private]

Definition at line 55 of file particles.hpp.

Referenced by draw().

std::vector<Particle*> Particles::particles [private]

Definition at line 57 of file particles.hpp.

Referenced by draw(), Particles(), update(), and ~Particles().


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