Amazing-Python-Scripts
136 строк · 3.9 Кб
1(function(){2
3// Generate the smoke sprite4var smokeCache = $('#smokesprite');5var ctx = smokeCache.getContext('2d');6
7// Soft gradient circle8// drawGradientCircle(ctx, 100, 100, 100, '150, 150, 150', 1);9
10// Hard edged circle11ctx.strokeStyle = 'rgba(150, 150, 150, 1)';12ctx.fillStyle = 'rgba(150, 150, 150, 1)';13drawCircle(ctx, 100, 100, 100, true, true);14
15var SMOKE_SPRITE = smokeCache;16
17// Reflection helper methods18var reflectVertical = function(incidence){19var r = 0 - (incidence + Math.PI - 0);20return -Math.PI - incidence;21}22var reflectHorizontal = function(incidence){23var r = 0.5*Math.PI - (incidence + Math.PI - 0.5*Math.PI);24r = 2 * Math.PI - incidence;25return -incidence;26}27
28window.Particle = function(options){29window.Particle.instances.push(this);30
31var self = this;32
33self.x = options.x;34self.y = options.y;35self.speedVariation = options.speedVariation || 0;36self.angleVariation = options.angleVariation || 0;37self.lifeVariation = options.lifeVariation || 200;38self.bounds = options.bounds;39
40// Allow the colour to be passed in, but default to white41var color = self.color = options.color || [255, 255, 255];42self._colorString = color[0]+', '+color[1]+', '+color[2];43
44self.particleLength = options.particleLength || 10;45
46self.radius = options.radius;47
48// Apply the speed variation49var speedAdjust = (Math.random() * self.speedVariation) - (self.speedVariation / 2);50self.speed = (options.speed || 10) + speedAdjust;51
52// Apply life variatino53var lifeAdjust = (Math.random() * self.lifeVariation) - (self.lifeVariation / 2);54self.life = (options.life || 300) + lifeAdjust;55
56// Add the angle variation57var angleAdjust = (Math.random() * self.angleVariation) - (self.angleVariation / 2);58var angle = options.angle + angleAdjust;59// Calculate the cos and sin values once up front based on the60// initial angle. The angle wont change here once created so no61// need to re-calculate each update.62var setAngle = function(angle){63self.angle = angle;64self._cos = Math.cos(angle);65self._sin = -Math.sin(angle);66}67setAngle(angle);68
69self._age = 0;70
71self.update = function(frameTime, delta){72self.speed -= (0.05 * delta);73var speed = self.speed * delta;74var bounds = self.bounds;75var x, y;76
77// Update origin based on the angle78var calculateCoordinates = function(){79x = self.x + speed * self._cos;80y = self.y + speed * self._sin;81}82calculateCoordinates();83
84// Handle vertical reflection when we go out of bounds85if(x < bounds.left || x > bounds.right){86setAngle(reflectVertical(self.angle));87calculateCoordinates()88}89
90// Handle horizontal reflection when we go out of bounds91if(y < bounds.top || y > bounds.bottom){92setAngle(reflectHorizontal(self.angle));93calculateCoordinates();94}95
96// Store the updated positions97self.x = x;98self.y = y;99
100// Remove if we've been alive too long101self._age += frameTime;102if(self._age > self.life)103self.destroy();104}105
106self.draw = function(ctx){107var x = self.x;108var y = self.y;109var opacity = 1-((1/self.life) * self._age);110var particleLength = self.particleLength;111
112if(self.radius){113var radius = (self.radius/2)+(((self.radius/2)/self.life) * self._age);114opacity = Math.round((opacity * 0.1)*100)/100;115ctx.save();116ctx.globalAlpha = opacity;117ctx.drawImage(SMOKE_SPRITE, x-radius, y-radius, (radius*2), (radius*2));118ctx.restore();119}else{120ctx.lineWidth = 1;121ctx.strokeStyle = 'rgba('+self._colorString+', '+opacity+')';122ctx.beginPath();123ctx.moveTo((x + particleLength * self._cos), (y + particleLength * self._sin));124ctx.lineTo(x, y);125ctx.stroke();126}127}128
129self.destroy = function(){130window.Particle.instances.splice(window.Bullet.instances.indexOf(self), 1);131self.assetList.remove(self);132}133}134
135window.Particle.instances = [];136})();