/**
 * @class BBParticleWorld
 * @author Sean Campbell
 * @version 1.0
 * Keeps track of particle emitters and
 * hyper voxels.
 */
function BBParticleWorld()
{
    this.canvas;
    this.ctx;
    
    this.emitters = new Array();
    this.hypervoxels = new Array();
    
    this.shouldDrawParticles = false;
    this.shouldDrawEmitters = false;
    
    this.isPlaying;
    this.timer;
    
    /**
     * Initiates the particle world and starts the timer.
     * @param {HTMLCanvasElement} canvas - canvas used to draw the particles in
     */
    this.init = function(canvas)
    {
        // Initiate values.
        this.canvas = canvas;
        this.ctx = canvas.getContext('2d');
        
        // Start the emitters.
        this.play();
    }
    
    /**
     * Start the emitter animation.
     */
    this.play = function()
    {
        this.isPlaying = true;
        // Set timer to refresh particles.
        var self = this;
        function callMethod() { self.nextFrame(); }
        this.timer = setInterval(callMethod, 1000 / BB.fps);
    }
    
    /**
     * Pause the emitter animation.
     */
    this.pause = function()
    {
        this.isPlaying = false;
        clearInterval(this.timer);
    }
    
    /**
     * Adds an emitter to the world.
     * @param {BBParticleEmitter} emitter - emitter to add
     */
    this.addEmitter = function(emitter)
    {
        this.emitters.push(emitter);
    }
    
    /**
     * Adds hyper voxels to the world.
     * @param {BBHyperVoxelSystem} hv - hyper voxels to add
     */
    this.addHyperVoxels = function(hv)
    {
        this.hypervoxels.push(hv);
    }

    /**
     * Advances particles by a frame and does
     * all drawing of particles, emitters,
     * and hyper voxels.
     */
    this.nextFrame = function()
    {
        // Draw scene that the particles are in.
        this.drawScene();
        this.ctx.strokeStyle = "black";
        this.ctx.fillStyle = "black";

        // Loop through particle emitters.
        for (var i = 0; i < this.emitters.length; i++) {
                this.emitters[i].updateParticles();
                
                // Draw particles (as dots) and emitters
                // (as boxes) if these options are set
                // to true.
                if (this.shouldDrawParticles)
                    this.emitters[i].drawParticles(this.ctx);
                if (this.shouldDrawEmitters)
                    this.emitters[i].draw(this.ctx);
        }

        // Loop through hyper voxels and render them.
        for (var j = 0; j < this.hypervoxels.length; j++) {
                this.hypervoxels[j].render(this.ctx);
        }
    }
    
    /**
     * Draws the scene. By default, simply clears the canvas
     * so it can be redrawn. However, this function can be
     * changed to draw whatever the scene is (ie a fish tank).
     */
    this.drawScene = function()
    {
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}
