From de51923531f5ddc3a793f3cd8c11f9a78e5f4897 Mon Sep 17 00:00:00 2001 From: Ricardo Cruz Date: Mon, 13 Sep 2004 22:48:14 +0000 Subject: [PATCH] Added simple particle system object. I called it Particles cause there is already a ParticleSystem class. Weather would be a better name for that class. This particle system is just a rip off of one that I did for a game of mine. I guess that with more customization it would be pretty good. SVN-Revision: 1903 --- src/gameobjs.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/gameobjs.h | 22 ++++++++++++++++++++++ src/sector.cpp | 19 +++++++++++++++++-- src/sector.h | 3 +++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/gameobjs.cpp b/src/gameobjs.cpp index e8ea3058f..e2e0716ee 100644 --- a/src/gameobjs.cpp +++ b/src/gameobjs.cpp @@ -32,6 +32,7 @@ #include "resources.h" #include "sector.h" #include "tilemap.h" +#include "video/drawing_context.h" BouncyDistro::BouncyDistro(const Vector& pos) : position(pos) @@ -437,6 +438,53 @@ SmokeCloud::draw(DrawingContext& context) img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1); } +Particles::Particles(const Vector& epicenter, int number, Color color_, int size_, float velocity_, int life_time) + : color(color_), size(size_), velocity(velocity_) +{ + timer.start(life_time); + + // create particles + for(int p = 0; p < number; p++) + { + Particle* particle = new Particle; + particle->pos = epicenter; + particle->angle = (rand() % 360) * (M_PI / 180); // in radius + + particles.push_back(particle); + } +} + +Particles::~Particles() +{ + // free particles + for(std::vector::iterator i = particles.begin(); i < particles.end(); i++) + delete (*i); +} + +void +Particles::action(float elapsed_time) +{ + // update particles + for(int p = 0; p < particles.size(); p++) + { + particles[p]->pos.x += sin(particles[p]->angle) * velocity * elapsed_time; + particles[p]->pos.y += cos(particles[p]->angle) * velocity * elapsed_time; + } + + if(!timer.check()) + remove_me(); +} + +void +Particles::draw(DrawingContext& context) +{ + // draw particles + for(int p = 0; p < particles.size(); p++) + { + context.draw_filled_rect(particles[p]->pos, Vector(size,size), color, LAYER_OBJECTS+10); + } +} + void load_object_gfx() { img_trampoline = sprite_manager->load("trampoline"); diff --git a/src/gameobjs.h b/src/gameobjs.h index 04665be98..d5e779c96 100644 --- a/src/gameobjs.h +++ b/src/gameobjs.h @@ -176,6 +176,28 @@ private: Vector position; }; +class Particles : public GameObject +{ +public: + Particles(const Vector& epicenter, int number, Color color, int size, float velocity, int life_time); + ~Particles(); + + virtual void action(float elapsed_time); + virtual void draw(DrawingContext& context); + +private: + Color color; + float size; + float velocity; + Timer timer; + + struct Particle { + Vector pos; + float angle; + }; + std::vector particles; +}; + void load_object_gfx(); #endif diff --git a/src/sector.cpp b/src/sector.cpp index 9064af73d..3d0f3e55a 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -466,7 +466,13 @@ Sector::update_game_objects() std::remove(smoke_clouds.begin(), smoke_clouds.end(), smoke_cloud), smoke_clouds.end()); } - + Particles* particle = dynamic_cast (*i); + if(particle) { + particles.erase( + std::remove(particles.begin(), particles.end(), particle), + particles.end()); + } + delete *i; i = gameobjects.erase(i); } else { @@ -500,7 +506,9 @@ Sector::update_game_objects() SmokeCloud* smoke_cloud = dynamic_cast (*i); if(smoke_cloud) smoke_clouds.push_back(smoke_cloud); - + Particles* particle = dynamic_cast (*i); + if(particle) + particles.push_back(particle); gameobjects.push_back(*i); } @@ -728,6 +736,13 @@ Sector::add_smoke_cloud(const Vector& pos) return true; } +bool +Sector::add_particles(const Vector& epicenter, int number, Color color, int size, float velocity, int life_time) +{ + add_object(new Particles(epicenter, number, color, size, velocity, life_time)); + return true; +} + /* Break a brick: */ bool Sector::trybreakbrick(const Vector& pos, bool small) diff --git a/src/sector.h b/src/sector.h index e9c1206cd..5fd13ffc4 100644 --- a/src/sector.h +++ b/src/sector.h @@ -46,6 +46,7 @@ class TileMap; class Upgrade; class Bullet; class SmokeCloud; +class Particles; class BadGuy; class Tile; @@ -106,6 +107,7 @@ public: void add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind); bool add_bullet(const Vector& pos, float xm, Direction dir); bool add_smoke_cloud(const Vector& pos); + bool add_particles(const Vector& epicenter, int number, Color color, int size, float velocity, int life_time); /** Try to grab the coin at the given coordinates */ void trygrabdistro(const Vector& pos, int bounciness); @@ -159,6 +161,7 @@ private: std::vector upgrades; std::vector bullets; std::vector smoke_clouds; + std::vector particles; public: // ugly typedef std::vector InteractiveObjects; -- 2.11.0