Amazing-Python-Scripts

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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.