-
Notifications
You must be signed in to change notification settings - Fork 0
/
UFireworks.pas
274 lines (223 loc) · 6.66 KB
/
UFireworks.pas
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
unit UFireworks;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics,
Dialogs, ExtCtrls, StdCtrls, BGRABitmap, BGRABitmapTypes, math;
type
//=============================================================
{ TParticle }
TParticle = class
pos : TPointF;
acc : TPointF;
vel : TPointF;
isFirework : Boolean;
lifespan : integer;
dec_lifespan: integer;
hu : integer;
public
constructor Create(x,y: single; v_hu: integer; v_isFirework: boolean);
procedure applyForce(force: TPointF);
procedure update;
function done: Boolean;
procedure Show(canvas: TBGRABitmap);
end;
{ TFirework }
TFirework = class
Owner: TObject;
hu: integer;
firework: TParticle;
exploded: Boolean;
particles: array of TParticle;
public
constructor Create(sender: TObject; width, height: integer);
function done: Boolean;
procedure Update;
procedure Explode;
procedure Show(canvas: TBGRABitmap);
end;
{ TFireworks }
TFireworks = class
gravity : TPointF;
fireworks : array of TFirework;
width : integer;
height : integer;
public
constructor Create(v_width, v_height: integer);
procedure Draw(canvas: TBGRABitmap);
end;
implementation
{ TFireworks }
constructor TFireworks.Create(v_width, v_height: integer);
begin
gravity.x:= 0;
gravity.y:= 0.2;
width:= v_width;
height:= v_height;
end;
procedure TFireworks.Draw(canvas: TBGRABitmap);
var i: integer;
begin
if random(100) < 3 then
begin
SetLength(fireworks, Length(fireworks)+1);
fireworks[Length(fireworks)-1]:= TFirework.Create(self, width, height);
end;
for i:=Length(fireworks)-1 downto 0 do
begin
fireworks[i].Update;
fireworks[i].Show(canvas);
if fireworks[i].done then
begin
fireworks[i].Free;
if i < Length(fireworks)-1 then
fireworks[i]:= fireworks[Length(fireworks)-1];
SetLength(fireworks, Length(fireworks)-1);
end;
end;
end;
{ TFirework }
constructor TFirework.Create(sender: TObject; width, height: integer);
begin
Owner:= sender;
hu:= random(65535);
firework:= TParticle.Create(random(width), height, hu, true);
exploded:= false;
SetLength(particles, 0);
end;
function TFirework.done: Boolean;
begin
if (exploded) and (Length(particles) = 0) then
Result:= true
else
Result:= false;
end;
procedure TFirework.Update;
var i: integer;
begin
if not exploded then
begin
firework.applyForce( TFireworks(Owner).gravity );
firework.update();
if firework.vel.y >= 0 then
begin
exploded:= true;
Explode();
end;
end;
for i:= Length(particles)-1 downto 0 do
begin
particles[i].applyForce( TFireworks(Owner).gravity );
particles[i].update();
if particles[i].done() then
begin
particles[i].Free;
if i < Length(particles)-1 then
particles[i]:= particles[Length(particles)-1];
SetLength(particles, Length(particles)-1);
end;
end;
end;
procedure TFirework.Explode;
var i: integer;
max_p: integer;
begin
max_p:= RandomRange(100, 300);
SetLength(particles, max_p);
for i:=0 to max_p-1 do
begin
particles[i]:= TParticle.Create(firework.pos.x, firework.pos.y, hu, false);
end;
end;
procedure TFirework.Show(canvas: TBGRABitmap);
var i: integer;
begin
if not exploded then
firework.Show(canvas);
for i:=0 to Length(particles)-1 do
particles[i].Show(canvas);
end;
{ TParticle }
constructor TParticle.Create(x, y: single; v_hu: integer; v_isFirework: boolean);
var range: integer;
begin
pos.x:= x;
pos.y:= y;
hu:= v_hu;
isFirework:= v_isFirework;
lifespan:= 255; // Opacity - wygaszanie widoczności
dec_lifespan:= RandomRange(2,5); // org 4 - co ile ma wygaszać
acc.SetLocation(0,0);
if isFirework then
begin
vel.x:= RandomRange(-3,3); // kierunek wystrzałów
vel.y:= RandomRange(-18, -8); // siła strzału
end
else
begin
range:= RandomRange(4, 20);
vel.x:= RandomRange(-100, 100)/ 100;
vel.y:= RandomRange(-100, 100)/ 100;
vel:= vel.Scale(range);
end;
end;
procedure TParticle.applyForce(force: TPointF);
begin
acc:= acc.Add(force);
end;
procedure TParticle.update;
begin
if not isFirework then
begin
vel:= vel.Scale(0.93); // rozproszenie
lifespan -= dec_lifespan;
end;
vel:= vel.Add(acc);
pos:= pos.Add(vel);
acc:= acc.Scale(0);
end;
function TParticle.done: Boolean;
begin
if lifespan < 0 then
Result:= true
else
Result:= false;
end;
procedure TParticle.Show(canvas: TBGRABitmap);
var posR: TPoint;
s: integer;
begin
canvas.CanvasBGRA.Brush.Style:= bsSolid;
canvas.CanvasBGRA.Pen.Style:= psClear;
posR:= pos.Round;
s:= RandomRange(1,4);
if not isFirework then
begin // rozbłyski
canvas.CanvasBGRA.Brush.BGRAColor.FromHSLAPixel(HSLA(hu, 65535, random(65535))); //65535, 32768));
canvas.CanvasBGRA.Brush.Opacity:= lifespan;
canvas.CanvasBGRA.Pen.Style:= psSolid;
canvas.CanvasBGRA.Pen.Width:= 1;
canvas.CanvasBGRA.Pen.BGRAColor.FromHSLAPixel(HSLA(hu, 65535, random(65535)));
canvas.CanvasBGRA.Pen.Opacity:= lifespan;
canvas.CanvasBGRA.MoveTo(posR.x-s-1, posR.y-s-1);
canvas.CanvasBGRA.LineTo(posR.x+s, posR.y+s);
canvas.CanvasBGRA.MoveTo(posR.x+s-1, posR.y-s-1);
canvas.CanvasBGRA.LineTo(posR.x-s-2, posR.y+s);
canvas.CanvasBGRA.Pen.Style:= psClear;
canvas.CanvasBGRA.EllipseC(posR.x, posR.y, s, s);
end
else
begin // rakieta
canvas.CanvasBGRA.Brush.BGRAColor.FromHSLAPixel(HSLA(hu, 65535, 32768));
canvas.CanvasBGRA.Brush.Opacity:= lifespan;
canvas.CanvasBGRA.EllipseC(posR.x, posR.y, 3, 3);
// dodatkowy cien po rakiecie
for s:=1 to 5 do
begin
canvas.CanvasBGRA.Brush.Opacity:= 200 div s;
posR:= posR.Subtract(vel.Round);
canvas.CanvasBGRA.EllipseC(posR.x, posR.y, 3, 3);
end;
end;
end;
end.