-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuMainForm.pas
More file actions
490 lines (421 loc) · 13.2 KB
/
uMainForm.pas
File metadata and controls
490 lines (421 loc) · 13.2 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
unit uMainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
{$IFDEF MSWINDOWS}
Winapi.MMSystem,
{$ENDIF}
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.Ani, FMX.Media, FMX.Effects, System.Generics.Collections;
type
TBulbState = (bsNormal, bsBroken, bsFalling);
TChristmasBulb = class
private
FImage: TImage;
FState: TBulbState;
FVelocityY: Single;
FOriginalY: Single;
FOriginalRotation: Single;
FOrnamentFile: string;
FBrokenImage: TImage;
public
constructor Create(AOwner: TFmxObject; X, Y: Single; const OrnamentFile: string);
destructor Destroy; override;
procedure UpdateFall(DeltaTime: Single);
procedure ShowBrokenOnFloor(FloorY: Single);
property Image: TImage read FImage;
property State: TBulbState read FState write FState;
property VelocityY: Single read FVelocityY write FVelocityY;
property OriginalY: Single read FOriginalY;
end;
TMainForm = class(TForm)
Timer1: TTimer;
MediaPlayer1: TMediaPlayer;
FallTimer: TTimer;
TreeImage: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FallTimerTimer(Sender: TObject);
private
FBulbs: TObjectList<TChristmasBulb>;
FLastTime: TDateTime;
FGravity: Single;
FFallingBulb: TChristmasBulb;
FOrnamentFiles: TStringList;
FBreakSoundFiles: TStringList;
{$IFDEF DEBUG}
FShownDocsPath: Boolean;
{$ENDIF}
function GetResourcePath: string;
procedure LoadTreeImage;
procedure LoadOrnamentFiles;
procedure LoadBreakSounds;
procedure GetRandomTreePosition(out X, Y: Single);
procedure CreateBulbs;
procedure OnBulbClick(Sender: TObject);
procedure PlayBreakSound;
end;
var
MainForm: TMainForm;
implementation
{$R *.fmx}
uses
System.Math, System.DateUtils, System.IOUtils;
const
TREE_COLOR = $FF2D5016; // Dark green
STAR_COLOR = $FFFFD700; // Gold
GRAVITY = 500.0; // pixels per second squared
{ TChristmasBulb }
constructor TChristmasBulb.Create(AOwner: TFmxObject; X, Y: Single; const OrnamentFile: string);
begin
FOrnamentFile := OrnamentFile;
FBrokenImage := nil;
FImage := TImage.Create(AOwner);
if AOwner is TFmxObject then
FImage.Parent := TFmxObject(AOwner);
// Load ornament image
if FileExists(OrnamentFile) then
FImage.Bitmap.LoadFromFile(OrnamentFile);
// Set size - scaled for phone screen (480x800)
FImage.Width := 25;
FImage.Height := 35;
FImage.Position.X := X - FImage.Width / 2; // Center on X position
FImage.Position.Y := Y - FImage.Height / 2; // Center on Y position
FState := bsNormal;
FVelocityY := 0;
FOriginalY := Y;
FOriginalRotation := Random * 360; // Random starting rotation
FImage.RotationAngle := FOriginalRotation;
end;
destructor TChristmasBulb.Destroy;
begin
FImage.Free;
if Assigned(FBrokenImage) then
FBrokenImage.Free;
inherited;
end;
procedure TChristmasBulb.UpdateFall(DeltaTime: Single);
begin
if FState = bsFalling then
begin
FVelocityY := FVelocityY + (GRAVITY * DeltaTime);
FImage.Position.Y := FImage.Position.Y + (FVelocityY * DeltaTime);
// Add rotation as it falls
FImage.RotationAngle := FImage.RotationAngle + (200 * DeltaTime);
// Don't fade out - we want to see it fall
end;
end;
procedure TChristmasBulb.ShowBrokenOnFloor(FloorY: Single);
var
BrokenFile: string;
Owner: TFmxObject;
Form: TForm;
ScaleY: Single;
FloorAreaStart: Single;
begin
// Hide the falling ornament
FImage.Visible := False;
// Look for broken version: ornament_red.png -> ornament_red_broken.png
BrokenFile := StringReplace(FOrnamentFile, '.png', '_broken.png', [rfIgnoreCase]);
if FileExists(BrokenFile) then
begin
Owner := TFmxObject(FImage.Parent);
FBrokenImage := TImage.Create(Owner);
FBrokenImage.Parent := Owner;
// Load broken ornament image
FBrokenImage.Bitmap.LoadFromFile(BrokenFile);
// Get form reference to calculate floor area
Form := nil;
if Owner is TForm then
Form := TForm(Owner);
// Position in the floor area (between bottom of tree and bottom of screen)
// Tree bottom is at Y=1390 in original image, scaled to screen
if Assigned(Form) then
begin
ScaleY := Form.ClientHeight / 1575.0;
FloorAreaStart := 1390 * ScaleY; // Bottom of tree ornament area
// Position broken ornament in floor area
FBrokenImage.Width := 40; // Size for broken pieces
FBrokenImage.Height := 30;
FBrokenImage.Position.X := FImage.Position.X + FImage.Width / 2 - FBrokenImage.Width / 2;
// Random Y position in floor area
FBrokenImage.Position.Y := FloorAreaStart + Random(Trunc(Form.ClientHeight - FloorAreaStart - FBrokenImage.Height));
end
else
begin
// Fallback if form not found
FBrokenImage.Width := 40;
FBrokenImage.Height := 30;
FBrokenImage.Position.X := FImage.Position.X + FImage.Width / 2 - FBrokenImage.Width / 2;
FBrokenImage.Position.Y := FloorY - FBrokenImage.Height;
end;
FBrokenImage.HitTest := False;
FBrokenImage.BringToFront;
end;
end;
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
begin
// Set up form
Self.Fill.Color := TAlphaColorRec.Midnightblue;
FBulbs := TObjectList<TChristmasBulb>.Create(True);
FOrnamentFiles := TStringList.Create;
FBreakSoundFiles := TStringList.Create;
FGravity := GRAVITY;
FFallingBulb := nil;
LoadTreeImage;
LoadOrnamentFiles;
LoadBreakSounds;
CreateBulbs;
FLastTime := Now;
Timer1.Interval := 16; // ~60 FPS
Timer1.Enabled := True;
FallTimer.Interval := 100;
FallTimer.Enabled := False;
{$IFDEF DEBUG}
FShownDocsPath := False;
{$ENDIF}
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
FBulbs.Free;
FOrnamentFiles.Free;
FBreakSoundFiles.Free;
end;
function TMainForm.GetResourcePath: string;
begin
{$IF DEFINED(ANDROID) OR DEFINED(IOS)}
// Android: resources in assets/internal folder
// iOS: resources in app's documents
Result := TPath.GetDocumentsPath;
{$IFDEF DEBUG}
if not FShownDocsPath then begin
ShowMessage({$IFDEF ANDROID} 'Android' {$ELSE} 'iOS' {$ENDIF} + ' docs path: ' + Result);
FShownDocsPath := True;
end;
{$ENDIF}
{$ELSEIF DEFINED(MSWINDOWS) OR DEFINED(OSX)}
// Windows: resources in same directory as executable
// Mac OSX: resources in Content\MacOS structure
Result := TPath.GetAppPath;
{$ELSE}
{$MESSAGE FATAL 'Unsupported platform'}
{$ENDIF}
end;
procedure TMainForm.LoadBreakSounds;
var
SearchRec: TSearchRec;
FullPath: string;
SearchPattern: string;
SearchPath: string;
begin
// Platform-specific sound file formats
{$IFDEF MSWINDOWS}
SearchPattern := 'glass-break-*.wav'; // Windows uses WAV
{$ELSEIF DEFINED(ANDROID)}
SearchPattern := 'glass-break-*.mp3'; // Android uses MP3
{$ELSEIF DEFINED(IOS) OR DEFINED(OSX)}
SearchPattern := 'glass-break-*.caf'; // Apple uses CAF
{$ENDIF}
// Find all glass-break sound files from platform-appropriate path
SearchPath := TPath.Combine(GetResourcePath, SearchPattern);
if FindFirst(SearchPath, faAnyFile, SearchRec) = 0 then
begin
repeat
FullPath := TPath.Combine(GetResourcePath, SearchRec.Name);
FBreakSoundFiles.Add(FullPath);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
end;
procedure TMainForm.PlayBreakSound;
var
RandomIndex: Integer;
SoundFile: string;
begin
if FBreakSoundFiles.Count > 0 then
begin
RandomIndex := Random(FBreakSoundFiles.Count);
SoundFile := FBreakSoundFiles[RandomIndex];
{$IFDEF MSWINDOWS}
// On Windows, use sndPlaySound for faster playback
sndPlaySound(PChar(SoundFile), SND_ASYNC or SND_NODEFAULT);
{$ELSE}
// On other platforms, use MediaPlayer1
MediaPlayer1.Stop;
MediaPlayer1.FileName := SoundFile;
MediaPlayer1.Play;
{$ENDIF}
end;
end;
procedure TMainForm.LoadTreeImage;
const
TREE_FILENAME = 'ChristmasTree.png';
var
TreeFile: string;
begin
// Load the Christmas tree image from platform-appropriate path
TreeFile := TPath.Combine(GetResourcePath, TREE_FILENAME);
if FileExists(TreeFile) then
begin
TreeImage.Bitmap.LoadFromFile(TreeFile);
TreeImage.SendToBack; // Make sure image is behind ornaments and lights
end
else
ShowMessage(TREE_FILENAME + ' not found in: ' + GetResourcePath);
end;
procedure TMainForm.LoadOrnamentFiles;
var
SearchRec: TSearchRec;
SearchPath: string;
FullPath: string;
begin
// Find all ornament PNG files (excluding broken versions)
SearchPath := TPath.Combine(GetResourcePath, 'ornament*.png');
if FindFirst(SearchPath, faAnyFile, SearchRec) = 0 then
begin
repeat
// Only add non-broken ornament files
if Pos('_broken', LowerCase(SearchRec.Name)) = 0 then
begin
FullPath := TPath.Combine(GetResourcePath, SearchRec.Name);
FOrnamentFiles.Add(FullPath);
end;
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
if FOrnamentFiles.Count = 0 then
ShowMessage('No ornament PNG files found in: ' + GetResourcePath);
end;
procedure TMainForm.GetRandomTreePosition(out X, Y: Single);
var
TreeTop: Single;
TreeBottom: Single;
TreeLeft: Single;
TreeRight: Single;
TreeTopX: Single;
YFromTop: Single;
HalfWidth: Single;
ScaleX, ScaleY: Single;
OrigX, OrigY: Single;
RandomValue: Single;
begin
// Tree dimensions in edited image (cut 150 top, 100 bottom, scaled 50%):
// New image size: 1050 x 1575
// Top (star): X=514, Y=28
// Bottom left: X=80, Y=1540
// Bottom right: X=930, Y=1540
// Skip top 150 pixels (scaled from 300) for star area
// Move bottom up 150 pixels to avoid floor area
TreeTopX := 514;
TreeTop := 28 + 150; // Skip 150 pixels for star
TreeBottom := 1540 - 150; // Move bottom up 150 pixels
TreeLeft := 80;
TreeRight := 930;
// Pick random Y within tree height, weighted heavily towards bottom
// Use power function inverted to skew distribution towards bottom (larger Y values)
RandomValue := Random;
RandomValue := 1 - RandomValue; // Invert
RandomValue := RandomValue * RandomValue; // Square to weight towards 0
RandomValue := 1 - RandomValue; // Invert back - now weighted towards 1.0 (bottom)
OrigY := TreeTop + RandomValue * (TreeBottom - TreeTop);
// Calculate how wide the tree is at this Y position
// Tree is a triangle, so width increases linearly from top to bottom
YFromTop := OrigY - 28; // Distance from actual top (not adjusted top)
HalfWidth := (YFromTop / (TreeBottom - 28)) * ((TreeRight - TreeLeft) / 2);
// Random X within the triangle at this Y
OrigX := TreeTopX + (Random * 2 - 1) * HalfWidth;
// Scale to current form size (480x800 from edited 1050x1575)
ScaleX := Self.ClientWidth / 1050.0;
ScaleY := Self.ClientHeight / 1575.0;
X := OrigX * ScaleX;
Y := OrigY * ScaleY;
end;
procedure TMainForm.CreateBulbs;
var
i: Integer;
X, Y: Single;
Bulb: TChristmasBulb;
OrnamentFile: string;
begin
if FOrnamentFiles.Count = 0 then
Exit; // No ornament files found
// Add ornamental bulbs scattered across the tree
for i := 0 to 29 do // Increased to 30 ornaments
begin
// Get random position within tree triangle
GetRandomTreePosition(X, Y);
// Pick random ornament file
OrnamentFile := FOrnamentFiles[Random(FOrnamentFiles.Count)];
Bulb := TChristmasBulb.Create(Self, X, Y, OrnamentFile);
Bulb.Image.OnClick := OnBulbClick;
Bulb.Image.HitTest := True;
Bulb.Image.Cursor := crHandPoint;
Bulb.Image.BringToFront; // Make sure bulbs appear on top of tree image
FBulbs.Add(Bulb);
end;
end;
procedure TMainForm.OnBulbClick(Sender: TObject);
var
ClickedImage: TImage;
Bulb: TChristmasBulb;
begin
if not (Sender is TImage) then Exit;
ClickedImage := TImage(Sender);
// Find the bulb that owns this image
for Bulb in FBulbs do
begin
if Bulb.Image = ClickedImage then
begin
if Bulb.State = bsNormal then
begin
Bulb.State := bsBroken;
// Store reference and start timer for delayed fall
FFallingBulb := Bulb;
FallTimer.Enabled := True;
end;
Break;
end;
end;
end;
procedure TMainForm.FallTimerTimer(Sender: TObject);
begin
FallTimer.Enabled := False;
if Assigned(FFallingBulb) then
begin
FFallingBulb.State := bsFalling;
FFallingBulb.VelocityY := 0;
FFallingBulb := nil;
end;
end;
procedure TMainForm.Timer1Timer(Sender: TObject);
var
CurrentTime: TDateTime;
DeltaTime: Single;
Bulb: TChristmasBulb;
begin
CurrentTime := Now;
DeltaTime := MilliSecondsBetween(CurrentTime, FLastTime) / 1000.0;
FLastTime := CurrentTime;
// Limit delta time to prevent huge jumps
if DeltaTime > 0.1 then
DeltaTime := 0.1;
// Update falling bulbs
for Bulb in FBulbs do
begin
if Bulb.State = bsFalling then
begin
Bulb.UpdateFall(DeltaTime);
// Check if bulb hit the bottom of the tree area (Y=1390 in original, scaled)
var TreeBottomY := 1390 * (Self.ClientHeight / 1575.0);
if Bulb.Image.Position.Y + Bulb.Image.Height >= TreeBottomY then
begin
PlayBreakSound; // Play sound when it hits the floor
Bulb.ShowBrokenOnFloor(TreeBottomY);
Bulb.State := bsNormal; // Done falling, now broken on floor
end;
end;
end;
end;
end.