'use strict';

const
    eventBus = require('./event-bus');

class Particle {

    constructor(parentGfx, texture, x, y, ttl, xenergy, yenergy) {
        this._parentGfx = parentGfx;
        this._gfx = new PIXI.Sprite(texture);
        parentGfx.addChild(this._gfx);

        this._gfx.x = x;
        this._gfx.y = y;
        this._ttl = ttl; // ms
        this._xenergy = xenergy;
        this._yenergy = yenergy;
    }

    /**
     * Return false if life <= 0
     */
    step(elapsed) {
        this._ttl -= elapsed;

        if (this._ttl <= 0) {
            this._remove();
            return false;
        } else {
            this._update();
            return true;
        }
    }

    get gfx() {
        return this._gfx;
    }

    _remove() {
        this._parentGfx.removeChild(this._gfx);
    }

    _update() {
        this._gfx.x -= this._xenergy;
        this._xenergy -= 0.15;

        this._gfx.y -= this._yenergy;
        this._yenergy -= 0.15;
    }
}

class ParticleEmitter {

    constructor(parentGfx) {
        this._parentGfx = parentGfx;
        this._gfx = new PIXI.ParticleContainer();
        parentGfx.addChild(this._gfx);

        let dot = new PIXI.Graphics();
        dot.beginFill(0xffffff, 0.75);
        dot.drawRect(0, 0, 2, 2);
        dot.endFill();
        this._sharedTexture = dot.generateTexture();

        this._particles = [];
    }

    start() {
        this._eatdotHandler = (args) => {
            this._emitDotCrumbs(args.x, args.y);
        };
        eventBus.register('event.action.eatdot', this._eatdotHandler);
    }

    step(elapsed) {
        this._particles.filter((particle) => {
            return particle.step(elapsed);
        });
    }

    stop() {
        eventBus.unregister('event.action.eatdot', this._eatdotHandler);
        // TODO: Remove and destroy graphics?
    }

    _emitDotCrumbs(x, y) {
        this._particles.push(new Particle(this._gfx, this._sharedTexture, x-3, y,   500,  2,   -0.5));
        this._particles.push(new Particle(this._gfx, this._sharedTexture, x+3, y,   400, -1.5,  1.0));
        this._particles.push(new Particle(this._gfx, this._sharedTexture, x,   y-3, 300,  1.0, -1.5));
        this._particles.push(new Particle(this._gfx, this._sharedTexture, x,   y+3, 200,  0.5,   2));
    }
}

module.exports = ParticleEmitter;
@@ -1227,12 +1227,9 @@
 ff, 
-0.75
+1
 );%0A 
@@ -1588,32 +1588,50 @@
 apsed) %7B%0A       
+ this._particles =
  this._particles
@@ -1875,19 +1875,27 @@
 Crumbs(x
-, y
+orig, yorig
 ) %7B%0A    
@@ -1902,449 +1902,1255 @@
     
-this._particles.push(new Particle(this._gfx, this._sharedTexture, x-3, y,   500,  2,   -0.5));%0A        this._particles.push(new Particle(this._gfx, this._sharedTexture, x+3, y,   400, -1.5,  1.0));%0A        this._particles.push(new Particle(this._gfx, this._sharedTexture, x,   y-3, 300,  1.0, -1.5))
+for (let count = 0; count %3C 4; count++) %7B%0A            let %7Bx, y, ttl, xenergy, yenergy%7D = generateRandomValues(%0A                xorig-4, xorig+4,%0A                yorig-4, yorig+4,%0A                500,%0A                -2, 2,%0A                -2, 2%0A            );%0A            console.log('test: ' + x + ' ' + y + ' ' + ttl + ' ' + xenergy + ' ' + yenergy);%0A            this._particles.push(%0A                new Particle(%0A                    this._gfx,%0A                    this._sharedTexture,%0A                    x,%0A                    y,%0A                    ttl,%0A                    xenergy,%0A                    yenergy%0A                )%0A            );%0A        %7D%0A    %7D%0A%7D%0A%0Amodule.exports = ParticleEmitter;%0A%0Afunction generateRandomValues(%0A    xmin, xmax,%0A    ymin, ymax,%0A    ttlmax,%0A    xminenergy, xmaxenergy,%0A    yminenergy, ymaxenergy%0A) %7B%0A    let x = (Math.random() * (xmax - xmin)) + xmin
 ;%0A    
- 
-   this._particles.push(new Particle(this._gfx, this._sharedTexture, x,   y+3, 200,  0.5,   2));%0A    %7D%0A%7D%0A%0Amodule.exports = ParticleEmitter;
+let y = (Math.random() * (ymax - ymin)) + ymin;%0A    let ttl = Math.random() * ttlmax;%0A    let xenergy = (Math.random() * (xmaxenergy - xminenergy)) + xminenergy;%0A    let yenergy = (Math.random() * (ymaxenergy - yminenergy)) + yminenergy;%0A%0A    return %7B%0A        x: x,%0A        y: y,%0A        ttl: ttl,%0A        xenergy: xenergy,%0A        yenergy: yenergy%0A    %7D%0A%7D

Add randomness to particle emitter