1 R.Engine.define({
  2     "class": "R.particles.Effect",
  3     "requires": [
  4         "R.engine.PooledObject"
  5     ]
  6 });
  7 
  8 R.particles.Effect = function() {
  9     return R.engine.PooledObject.extend({
 10 
 11         particleCount: 30,
 12         particleCountVariance: 1,
 13         particleLifetime: 1000,
 14         particleLifetimeVariance: 500,
 15         position: null,
 16         particleClass: null,
 17         ttl: 0,
 18         run: false,
 19         emitFrequency: 0,
 20         emitFrequencyVariance: 0,
 21         lastTime: 0,
 22 
 23         constructor: function(origin) {
 24             this.position = R.math.Point2D.create(origin);
 25             this.run = false;
 26             this.ttl = 0;
 27             this.emitFrequency = 0;
 28             this.emitFrequencyVariance = 0;
 29             this.lastTime = 0;
 30             return this;
 31         },
 32 
 33         release: function() {
 34             this.run = false;
 35             this.lastTime = 0;
 36             this.emitFrequency = 0;
 37             this.emitFrequencyVariance = 0;
 38             this.ttl = 0;
 39         },
 40 
 41         quantity: function(particleCount, particleCountVariance) {
 42             this.particleCount = particleCount;
 43             this.particleCountVariance = particleCountVariance || 1;
 44             return this;
 45         },
 46 
 47         lifespan: function(ttl) {
 48             this.ttl = ttl;
 49             return this;
 50         },
 51 
 52         frequency: function(emitFrequency, frequencyVariance) {
 53             this.emitFrequency = emitFrequency;
 54             this.emitFrequencyVariance = frequencyVariance || 0;
 55             return this;
 56         },
 57 
 58         particleLife: function(lifetime, variance) {
 59             this.particleLifetime = lifetime;
 60             this.particleLifetimeVariance = variance || 500;
 61             return this;
 62         },
 63 
 64         particle: function(particleClass) {
 65             this.particleClass = particleClass;
 66             return this;
 67         },
 68 
 69         runEffect: function(particleEngine, time, dt) {
 70             var p = R.struct.Container.create();
 71             var numParticles = this.particleCountVariance == 1 ? this.particleCount :
 72                 Math.floor(this.particleCount * R.lang.Math2.randomRange(0, this.particleCountVariance));
 73             var particleLife = this.particleLifetime + R.lang.Math2.randomRange(0, this.particleLifetimeVariance, true);
 74             var emitFreq = this.emitFrequency + R.lang.Math2.randomRange(0, this.emitFrequencyVariance, true);
 75 
 76             if (!this.run || (this.run && time - this.lastTime > emitFreq)) {
 77                 this.generateParticles(p, numParticles, particleLife, time, dt);
 78                 this.lastTime = time;
 79             }
 80 
 81             particleEngine.addParticles(p);
 82             this.run = true;
 83         },
 84 
 85         generateParticles: function(particles, particleCount, particleLife) {
 86             // ABSTRACT
 87         },
 88 
 89         getLifespan: function(dt) {
 90             this.ttl -= dt;
 91             return Math.max(this.ttl, 0);
 92         },
 93 
 94         hasRun: function() {
 95             return this.run;
 96         }
 97 
 98     }, {
 99         getClassName: function() {
100             return "R.particles.Effect";
101         }
102     });
103 };