/
Explosion.as
144 lines (116 loc) · 3.26 KB
/
Explosion.as
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
package
{
import flash.display.BitmapData;
import flash.geom.ColorTransform;
public class Explosion
{
/*
EXPLOSION PARAMETERS
*/
//-- START VALUES
private const EXPLOSION_RADIUS: int = 30;
private const EXPLOSION_MAX_VELOCITY: Number = 2;
private const EXPLOSION_MAX_HEAT: int = -1; // causes particles to move up
private const PARTICLE_CHAOTIC_MOVE: Number = 0;
private const PARTICLE_DAMP_MOVE: Number = .95;
private const PARTICLE_DAMP_ENERGIE: Number = .999;
//-- AMOUNT
private const P_COUNT: int = 200;
private const PCOUNT_EACH_FRAME: int = 200;
private var t_particles: Array;
private var c_particles: Array;
private var energie: Number;
//private var transform: ColorTransform;
private var color:int = 0;
private var counter:int = 0;
private var dead:Boolean = false;
public function Explosion( x: Number, y: Number, fcolor:int )
{
init( x, y, fcolor );
}
public function getColor():int {
return color;
}
public function isDead():Boolean {
return dead;
}
private function init( x: Number, y: Number, fcolor:int ): void
{
color = fcolor;
t_particles = new Array();
c_particles = new Array();
energie = 1;
var a: Number;
var s: Number;
var l: Number;
var particle: Particle;
for( var i: int = 0 ; i < P_COUNT ; i++ )
{
a = Math.random() * Math.PI * 2;
s = Math.random() * EXPLOSION_MAX_VELOCITY + 1;
l = Math.random() * EXPLOSION_RADIUS;
particle = new Particle
(
//-- screen position
x + Math.sin( a ) * l,
y + Math.cos( a ) * l,
//-- velocity (x,y)
Math.sin( a ) * s,
Math.cos( a ) * s - Math.random() * EXPLOSION_MAX_HEAT,
//-- start energie, damp energie
1,
PARTICLE_DAMP_ENERGIE
);
t_particles.push( particle );
}
}
public function render( buffer: BitmapData ): void
{
counter++;
if (counter > 50)
dead = true;
if( t_particles.length >= PCOUNT_EACH_FRAME )
{
c_particles = c_particles.concat( t_particles.splice( -PCOUNT_EACH_FRAME ) );
}
else if( t_particles.length > 0 )
{
c_particles = t_particles.splice();
}
var particle: Particle;
var sx: Number;
var sy: Number;
var vx: Number;
var vy: Number;
var e: int;
var i: int = c_particles.length;
while( --i > -1 )
{
particle = c_particles[i];
sx = particle.sx;
sy = particle.sy;
vx = particle.vx;
vy = particle.vy;
//vx += Math.random() / ( .5 / PARTICLE_CHAOTIC_MOVE ) - PARTICLE_CHAOTIC_MOVE;
//vy += Math.random() / ( .5 / PARTICLE_CHAOTIC_MOVE ) - PARTICLE_CHAOTIC_MOVE;
if( particle.energie < .1 )
{
c_particles.splice( i, 1 );
}
particle.energie *= particle.energieDamp;
buffer.setPixel32( sx, sy, int( 0xff ) * 16843009 );
//buffer.setPixel32( sx+1, sy, int( particle.energie * 0xff ) * 16843009 );
//buffer.setPixel32( sx, sy+1, int( particle.energie * 0xff ) * 16843009 );
//buffer.setPixel32( sx+1, sy+1, int( particle.energie * 0xff ) * 16843009 );
sx += vx;
sy += vy;
vx *= PARTICLE_DAMP_MOVE;
vy *= PARTICLE_DAMP_MOVE;
particle.sx = sx;
particle.sy = sy;
particle.vx = vx;
particle.vy = vy;
}
}
}
}