add code to debug collision rectangles
[supertux.git] / src / object / particlesystem.cpp
index e0500f8..3f505e6 100644 (file)
 #include <iostream>
 #include <cmath>
 
-#include "particlesystem.h"
-#include "video/drawing_context.h"
-#include "lisp/parser.h"
-#include "lisp/lisp.h"
-#include "lisp/writer.h"
-#include "resources.h"
-#include "main.h"
+#include "particlesystem.hpp"
+#include "video/drawing_context.hpp"
+#include "lisp/parser.hpp"
+#include "lisp/lisp.hpp"
+#include "lisp/writer.hpp"
+#include "resources.hpp"
+#include "main.hpp"
+#include "object/camera.hpp"
 
 ParticleSystem::ParticleSystem()
 {
@@ -73,9 +74,9 @@ void ParticleSystem::draw(DrawingContext& context)
 
 SnowParticleSystem::SnowParticleSystem()
 {
-    snowimages[0] = new Surface(datadir+"/images/objects/particles/snow0.png", true);
-    snowimages[1] = new Surface(datadir+"/images/objects/particles/snow1.png", true);
-    snowimages[2] = new Surface(datadir+"/images/objects/particles/snow2.png", true);
+    snowimages[0] = new Surface("images/objects/particles/snow0.png");
+    snowimages[1] = new Surface("images/objects/particles/snow1.png");
+    snowimages[2] = new Surface("images/objects/particles/snow2.png");
 
     virtual_width = SCREEN_WIDTH * 2;
 
@@ -83,8 +84,8 @@ SnowParticleSystem::SnowParticleSystem()
     size_t snowflakecount = size_t(virtual_width/10.0);
     for(size_t i=0; i<snowflakecount; ++i) {
         SnowParticle* particle = new SnowParticle;
-        particle->pos.x = rand() % int(virtual_width);
-        particle->pos.y = rand() % SCREEN_HEIGHT;
+        particle->pos.x = fmodf(rand(), virtual_width);
+        particle->pos.y = fmodf(rand(), SCREEN_HEIGHT);
         int snowsize = rand() % 3;
         particle->texture = snowimages[snowsize];
         do {
@@ -122,6 +123,65 @@ void SnowParticleSystem::update(float elapsed_time)
     for(i = particles.begin(); i != particles.end(); ++i) {
         SnowParticle* particle = (SnowParticle*) *i;
         particle->pos.y += particle->speed * elapsed_time;
+        if(particle->pos.y > SCREEN_HEIGHT + Sector::current()->camera->get_translation().y) {
+            particle->pos.y = fmodf(particle->pos.y , virtual_height);
+            particle->pos.x = rand() % int(virtual_width);
+        }
+    }
+}
+
+//FIXME: Sometimes both ghosts have the same image
+//       Ghosts don't change their movement pattern - not random
+GhostParticleSystem::GhostParticleSystem()
+{
+    ghosts[0] = new Surface("images/objects/particles/ghost0.png");
+    ghosts[1] = new Surface("images/objects/particles/ghost1.png");
+
+    virtual_width = SCREEN_WIDTH * 2;
+
+    // create two ghosts
+    size_t ghostcount = 2;
+    for(size_t i=0; i<ghostcount; ++i) {
+        GhostParticle* particle = new GhostParticle;
+        particle->pos.x = fmodf(rand(), virtual_width);
+        particle->pos.y = fmodf(rand(), SCREEN_HEIGHT);
+        int size = rand() % 2;
+        particle->texture = ghosts[size];
+        do {
+            particle->speed = size*.2 + (float(rand()%10)*.4);
+        } while(particle->speed < 1);
+        particle->speed *= 50;
+        particles.push_back(particle);
+    }
+}
+
+void
+GhostParticleSystem::parse(const lisp::Lisp& reader)
+{
+  reader.get("layer", layer);
+}
+
+void
+GhostParticleSystem::write(lisp::Writer& writer)
+{
+  writer.start_list("particles-ghosts");
+  writer.write_int("layer", layer);
+  writer.end_list("particles-ghosts");
+}
+
+GhostParticleSystem::~GhostParticleSystem()
+{
+  for(int i=0;i<2;++i)
+    delete ghosts[i];
+}
+
+void GhostParticleSystem::update(float elapsed_time)
+{
+    std::vector<Particle*>::iterator i;
+    for(i = particles.begin(); i != particles.end(); ++i) {
+        GhostParticle* particle = (GhostParticle*) *i;
+        particle->pos.y -= particle->speed * elapsed_time;
+        particle->pos.x -= particle->speed * elapsed_time;
         if(particle->pos.y > SCREEN_HEIGHT) {
             particle->pos.y = fmodf(particle->pos.y , virtual_height);
             particle->pos.x = rand() % int(virtual_width);
@@ -131,7 +191,7 @@ void SnowParticleSystem::update(float elapsed_time)
 
 CloudParticleSystem::CloudParticleSystem()
 {
-    cloudimage = new Surface(datadir + "/images/objects/particles/cloud.png", true);
+    cloudimage = new Surface("images/objects/particles/cloud.png");
 
     virtual_width = 2000.0;