1 R.Engine.define({ 2 "class": "R.particles.effects.ExplosionParticle", 3 "requires": [ 4 "R.particles.AbstractParticle", 5 "R.math.Math2D" 6 ] 7 }); 8 9 /** 10 * @class An explosion particle 11 * 12 * @param pos {Point2D} The starting position of the particle. A 13 * velocity vector will be derived from this position. 14 * @param ttl {Number} Particle lifetime (time to live) in milliseconds 15 * @param decay {Number} A floating point which indicates the speed decay 16 */ 17 R.particles.effects.ExplosionParticle = function() { 18 return R.particles.AbstractParticle.extend(/** @scope R.particles.effects.ExplosionParticle.prototype */{ 19 20 velocityVector: null, 21 decay: 0, 22 inverseVelocity: null, 23 24 constructor: function(position, ttl, decay) { 25 this.base(ttl || 2000); 26 this.setPosition(position.x, position.y); 27 28 var emitAngle = Math.floor(R.lang.Math2.random() * 360); 29 30 if (this.inverseVelocity == null) { 31 // Another situation where it's better to keep this value, rather than destroying 32 // it after use. Since particles are short-lived, it's better to do this than 33 // create/destroy over and over. 34 this.inverseVelocity = R.math.Vector2D.create(0, 0); 35 } 36 37 if (this.velocityVector == null) { 38 // Same as above to save cycles... 39 this.velocityVector = R.math.Vector2D.create(0, 0); 40 } 41 42 R.math.Math2D.getDirectionVector(R.math.Point2D.ZERO, R.math.Vector2D.UP, emitAngle, this.velocityVector); 43 var vel = 1 + (R.lang.Math2.random() * 5); 44 this.velocityVector.mul(vel); 45 this.decay = decay; 46 }, 47 48 release: function() { 49 this.base(); 50 this.decay = 0; 51 this.renderFn = null; 52 }, 53 54 /** 55 * Called by the particle engine to draw the particle to the rendering 56 * context. 57 * 58 * @param renderContext {RenderContext} The rendering context 59 * @param time {Number} The engine time in milliseconds 60 * @param dt {Number} The delta between the world time and the last time the world was updated 61 * in milliseconds. 62 */ 63 draw: function(renderContext, time, dt) { 64 if (this.decay > 0 && this.velocityVector.len() > 0) { 65 this.inverseVelocity.set(this.velocityVector).neg(); 66 this.inverseVelocity.mul(this.decay); 67 this.velocityVector.add(this.inverseVelocity); 68 } 69 70 this.getPosition().add(this.velocityVector); 71 this.renderParticle(renderContext, time, dt); 72 }, 73 74 renderParticle: function(renderContext, time, dt) { 75 renderContext.setFillStyle("#fff"); 76 renderContext.drawPoint(this.getPosition()); 77 } 78 79 }, { 80 getClassName: function() { 81 return "R.particles.effects.ExplosionParticle"; 82 } 83 }); 84 }; 85 86 R.Engine.define({ 87 "class": "R.particles.effects.Explosion", 88 "requires": [ 89 "R.particles.Effect" 90 ] 91 }); 92 93 R.particles.effects.Explosion = function() { 94 return R.particles.Effect.extend({ 95 96 decayTime: R.lang.Math2.random() * 0.09, 97 98 decay: function(decayTime, variance) { 99 this.decayTime = decayTime * (variance ? R.lang.Math2.random() * variance : 1); 100 return this; 101 }, 102 103 generateParticles: function(particles, particleCount, particleLife) { 104 for (var x = 0; x < particleCount; x++) { 105 particles.add(this.particleClass.create(this.position, particleLife, this.decayTime)); 106 } 107 } 108 109 }, { 110 getClassName: function() { 111 return "R.particles.effects.Explosion"; 112 } 113 }); 114 }; 115 116