Debian Codegraph @

examples/curves/use_designed_curve/code/gameviewmain.pas

raw
1 {
2 Copyright 2018-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleVectors, CastleComponentSerialize, CastleCurves,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene, CastleTimeUtils;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 CurveScene: TCastleScene;
33 SphereTransform: TCastleSphere;
34 private
35 Curves: TCurveList;
36 SpherePositionOnCurve: TFloatTime;
37 { Set SphereTransform.Translation based on SpherePositionOnCurve,
38 such that sphere follows along the curve. }
39 procedure UpdateSpherePosition;
40 public
41 constructor Create(AOwner: TComponent); override;
42 procedure Start; override;
43 procedure Stop; override;
44 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
45 end;
46
47 var
48 ViewMain: TViewMain;
49
50 implementation
51
52 uses SysUtils,
53 CastleLog, X3DNodes;
54
55 { TViewMain ----------------------------------------------------------------- }
56
57 constructor TViewMain.Create(AOwner: TComponent);
58 begin
59 inherited;
60 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
61 end;
62
63 procedure TViewMain.Start;
64
65 procedure TestEvaluateCurve;
66 var
67 FirstCurve: TCurve;
68 begin
69 if Curves.Count = 0 then
70 raise Exception.Create('No curves defined in file');
71
72 FirstCurve := Curves[0];
73
74 // That's it, you have the 1st curve from XML file.
75 // Write some initial curve points.
76 WritelnLog(FirstCurve.Point(0.0).ToString);
77 WritelnLog(FirstCurve.Point(0.1).ToString);
78 WritelnLog(FirstCurve.Point(0.2).ToString);
79 end;
80
81 function CreateNodesToDisplayCurve: TX3DRootNode;
82 var
83 CurveShape: TShapeNode;
84 I: Integer;
85 begin
86 Result := TX3DRootNode.Create;
87 for I := 0 to Curves.Count - 1 do
88 begin
89 CurveShape := TShapeNode.Create;
90 CurveShape.Geometry := Curves[I].GeometryNode(100);
91 Result.AddChildren(CurveShape);
92 end;
93 end;
94
95 begin
96 inherited;
97
98 Curves := TCurveList.Create(true { free children });
99 Curves.LoadFromFile('castle-data:/my_curves.xml');
100
101 TestEvaluateCurve;
102
103 CurveScene.Load(CreateNodesToDisplayCurve, true);
104
105 SpherePositionOnCurve := 0;
106 UpdateSpherePosition;
107 end;
108
109 procedure TViewMain.Stop;
110 begin
111 FreeAndNil(Curves);
112 inherited;
113 end;
114
115 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
116 begin
117 inherited;
118 { This virtual method is executed every frame (many times per second). }
119 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
120
121 SpherePositionOnCurve := SpherePositionOnCurve + SecondsPassed * 0.1;
122 UpdateSpherePosition;
123 end;
124
125 procedure TViewMain.UpdateSpherePosition;
126 begin
127 SphereTransform.Translation := Curves[0].Point(Frac(SpherePositionOnCurve));
128 end;
129
130 end.