# Code coverage report for src/particle/Emitter.js

# all files / src/particle/ Emitter.js

17.14% Statements 6/35
0% Branches 0/18
0% Functions 0/3
17.14% Lines 6/35

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162

    1×

    1×

    1×

    1×

    1×

    1×

     import Base from '../core/Base';
    import Vector3 from '../math/Vector3';
    import Particle from './Particle';
    import Value from '../math/Value';
    import glMatrix from '../dep/glmatrix';
    var vec3 =  glMatrix.vec3;

    /**
     * @constructor clay.particle.Emitter
     * @extends clay.core.Base
     */
    var Emitter = Base.extend(
    /** @lends clay.particle.Emitter# */
    {
        /**
         * Maximum number of particles created by this emitter
         * @type {number}
         */
        max: 1000,
        /**
         * Number of particles created by this emitter each shot
         * @type {number}
         */
        amount: 20,

        // Init status for each particle
        /**
         * Particle life generator
         * @type {?clay.Value.<number>}
         */
        life: null,
        /**
         * Particle position generator
         * @type {?clay.Value.<clay.Vector3>}
         */
        position: null,
        /**
         * Particle rotation generator
         * @type {?clay.Value.<clay.Vector3>}
         */
        rotation: null,
        /**
         * Particle velocity generator
         * @type {?clay.Value.<clay.Vector3>}
         */
        velocity: null,
        /**
         * Particle angular velocity generator
         * @type {?clay.Value.<clay.Vector3>}
         */
        angularVelocity: null,
        /**
         * Particle sprite size generator
         * @type {?clay.Value.<number>}
         */
        spriteSize: null,
        /**
         * Particle weight generator
         * @type {?clay.Value.<number>}
         */
        weight: null,

        _particlePool: null

    }, function() {

        this._particlePool = [];

        // TODO Reduce heap memory
        for (var i = 0; i < this.max; i++) {
            var particle = new Particle();
            particle.emitter = this;
            this._particlePool.push(particle);

            if (this.velocity) {
                particle.velocity = new Vector3();
            }
            if (this.angularVelocity) {
                particle.angularVelocity = new Vector3();
            }
        }
    },
    /** @lends clay.particle.Emitter.prototype */
    {
        /**
         * Emitter number of particles and push them to a given particle list. Emmit number is defined by amount property
         * @param  {Array.<clay.particle.Particle>} out
         */
        emit: function(out) {
            var amount = Math.min(this._particlePool.length, this.amount);

            var particle;
            for (var i = 0; i < amount; i++) {
                particle = this._particlePool.pop();
                // Initialize particle status
                if (this.position) {
                    this.position.get(particle.position);
                }
                if (this.rotation) {
                    this.rotation.get(particle.rotation);
                }
                if (this.velocity) {
                    this.velocity.get(particle.velocity);
                }
                if (this.angularVelocity) {
                    this.angularVelocity.get(particle.angularVelocity);
                }
                if (this.life) {
                    particle.life = this.life.get();
                }
                if (this.spriteSize) {
                    particle.spriteSize = this.spriteSize.get();
                }
                if (this.weight) {
                    particle.weight = this.weight.get();
                }
                particle.age = 0;

                out.push(particle);
            }
        },
        /**
         * Kill a dead particle and put it back in the pool
         * @param  {clay.particle.Particle} particle
         */
        kill: function(particle) {
            this._particlePool.push(particle);
        }
    });

    /**
     * Create a constant 1d value generator. Alias for {@link clay.Value.constant}
     * @function clay.particle.Emitter.constant
     */
    Emitter.constant = Value.constant;

    /**
     * Create a constant vector value(2d or 3d) generator. Alias for {@link clay.Value.vector}
     * @function clay.particle.Emitter.vector
     */
    Emitter.vector = Value.vector;

    /**
     * Create a random 1d value generator. Alias for {@link clay.Value.random1D}
     * @function clay.particle.Emitter.random1D
     */
    Emitter.random1D = Value.random1D;

    /**
     * Create a random 2d value generator. Alias for {@link clay.Value.random2D}
     * @function clay.particle.Emitter.random2D
     */
    Emitter.random2D = Value.random2D;

    /**
     * Create a random 3d value generator. Alias for {@link clay.Value.random3D}
     * @function clay.particle.Emitter.random3D
     */
    Emitter.random3D = Value.random3D;

    export default Emitter;