Debian Codegraph @

examples/animations/animate_transform_by_code_2/code/gameviewmain.pas

raw
1 {
2 Copyright 2020-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 { Main view, where most of the application logic takes place. }
16 unit GameViewMain;
17
18 interface
19
20 uses Classes,
21 CastleVectors, CastleComponentSerialize,
22 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene, CastleTimeUtils,
23 X3DNodes;
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 MainScene: TCastleScene;
33 private
34 const
35 XCount = 15;
36 YCount = 15;
37 var
38 Time: TFloatTime;
39 Transform: array [0 .. XCount - 1, 0 .. YCount - 1] of TTransformNode;
40 public
41 constructor Create(AOwner: TComponent); override;
42 procedure Start; override;
43 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
44 end;
45
46 var
47 ViewMain: TViewMain;
48
49 implementation
50
51 uses SysUtils;
52
53 { TViewMain ----------------------------------------------------------------- }
54
55 constructor TViewMain.Create(AOwner: TComponent);
56 begin
57 inherited;
58 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
59 end;
60
61 procedure TViewMain.Start;
62
63 function BuildSceneNode: TX3DRootNode;
64 var
65 Shape: TShapeNode;
66 Mat: TMaterialNode;
67 I, J: Integer;
68 begin
69 Result := TX3DRootNode.Create;
70
71 Mat := TMaterialNode.Create;
72 Mat.DiffuseColor := Vector3(1, 1, 0);
73
74 Shape := TShapeNode.Create;
75 Shape.Appearance := TAppearanceNode.Create;
76 Shape.Appearance.Material := Mat;
77 Shape.Geometry := TBoxNode.Create;
78
79 for I := 0 to XCount - 1 do
80 for J := 0 to YCount - 1 do
81 begin
82 Transform[I, J] := TTransformNode.Create;
83 Transform[I, J].Translation := Vector3(I * 2, J * 2, 0);
84 Transform[I, J].AddChildren(Shape);
85
86 Result.AddChildren(Transform[I, J]);
87 end;
88 end;
89
90 begin
91 inherited;
92 MainScene.Load(BuildSceneNode, { MainScene owns new node } true);
93 end;
94
95 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
96 var
97 I, J: Integer;
98 T: TVector3;
99 begin
100 inherited;
101 { This virtual method is executed every frame (many times per second). }
102 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
103
104 Time := Time + Container.Fps.SecondsPassed;
105
106 for I := 0 to XCount - 1 do
107 for J := 0 to YCount - 1 do
108 begin
109 T := Transform[I, J].Translation;
110 T.Z := 2 *
111 Sin(I / 2 + Time) *
112 Cos(J / 2 + Time);
113 Transform[I, J].Translation := T;
114 end;
115 end;
116
117 end.