Debian Codegraph @

tests/code/testcases/testcastlescenemanager.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleSceneManager" -*-
2 {
3 Copyright 2020-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Test CastleSceneManager unit. }
18 unit TestCastleSceneManager;
19
20 interface
21
22 uses
23 Classes, SysUtils, CastleTester;
24
25 type
26 TTestCastleSceneManager = class(TCastleTestCase)
27 published
28 procedure TestNavigationCreating;
29 procedure TestLoadLevel;
30 end;
31
32 implementation
33
34 uses CastleCameras, CastleSceneManager, CastleScene, CastleSceneCore,
35 CastleFilesUtils, CastleVectors, CastleViewport;
36
37 procedure TTestCastleSceneManager.TestNavigationCreating;
38 var
39 SceneManager: TCastleSceneManager;
40 C: TCastleNavigation;
41 begin
42 SceneManager := TCastleSceneManager.Create(nil);
43 try
44 AssertTrue(SceneManager.Navigation = nil);
45 AssertTrue(SceneManager.WalkNavigation(false) = nil);
46 AssertTrue(SceneManager.ExamineNavigation(false) = nil);
47 AssertTrue(SceneManager.Navigation = nil);
48
49 SceneManager.NavigationType := ntWalk;
50 C := SceneManager.Navigation;
51 AssertTrue(C <> nil);
52 AssertTrue(SceneManager.NavigationType = ntWalk);
53
54 SceneManager.NavigationType := ntFly;
55 AssertTrue(C = SceneManager.Navigation); // ntWalk -> ntFly didn't change camera instance
56 AssertTrue(C = SceneManager.WalkNavigation);
57 AssertTrue(C = SceneManager.WalkNavigation(false));
58 AssertTrue(SceneManager.NavigationType = ntFly);
59
60 C.Radius := 100;
61 AssertTrue(SceneManager.Navigation.Radius = 100);
62
63 SceneManager.NavigationType := ntExamine;
64 AssertTrue(C <> SceneManager.Navigation); // ntFly -> ntExamine did change camera instance
65 AssertTrue(SceneManager.NavigationType = ntExamine);
66 // but the Radius was preserved when copying
67 AssertTrue(SceneManager.Navigation.Radius = 100);
68 finally FreeAndNil(SceneManager) end;
69
70 SceneManager := TCastleSceneManager.Create(nil);
71 try
72 AssertTrue(SceneManager.Navigation = nil);
73
74 SceneManager.WalkNavigation.Radius := 100;
75 AssertTrue(SceneManager.Navigation <> nil);
76 AssertTrue(SceneManager.Navigation = SceneManager.WalkNavigation);
77 AssertTrue(SceneManager.NavigationType = ntWalk);
78 AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForRotations);
79 AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForMoving);
80 AssertTrue(SceneManager.WalkNavigation.Gravity);
81
82 C := SceneManager.WalkNavigation;
83 SceneManager.NavigationType := ntFly;
84 AssertTrue(SceneManager.Navigation = SceneManager.WalkNavigation);
85 AssertTrue(C = SceneManager.WalkNavigation);
86 AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForRotations);
87 AssertFalse(SceneManager.WalkNavigation.PreferGravityUpForMoving);
88 AssertFalse(SceneManager.WalkNavigation.Gravity);
89
90 // changing to ExamineNavigation and back to WalkNavigation sets ntWalk again
91 SceneManager.ExamineNavigation;
92 SceneManager.WalkNavigation;
93 AssertTrue(SceneManager.NavigationType = ntWalk);
94 AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForRotations);
95 AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForMoving);
96 AssertTrue(SceneManager.WalkNavigation.Gravity);
97 finally FreeAndNil(SceneManager) end;
98 end;
99
100 procedure TTestCastleSceneManager.TestLoadLevel;
101 var
102 Scene: TCastleScene;
103 SceneManager: TCastleSceneManager;
104 Pos, Dir, Up, Pos2, Dir2, Up2: TVector3;
105 begin
106 SceneManager := TCastleSceneManager.Create(nil);
107 try
108 Scene := TCastleScene.Create(SceneManager);
109 Scene.Load('castle-data:/level1.x3d');
110 Scene.PreciseCollisions := true;
111 Scene.ProcessEvents := true;
112
113 SceneManager.Items.Add(Scene);
114 SceneManager.MainScene := Scene;
115
116 { Note that this depends on hack within RequiredNavigation:
117 We depend that RequiredNavigation calls EnsureCameraDetected,
118 thus Camera position/dir/up are correct after RequiredNavigation
119 (even though it's not guaranteed in docs). }
120
121 AssertTrue(SceneManager.NavigationType = ntNone);
122 SceneManager.RequiredNavigation;
123 AssertTrue(SceneManager.NavigationType = ntExamine);
124 SceneManager.Navigation.Camera.GetWorldView(Pos, Dir, Up);
125 AssertVectorEquals(Vector3(3.249692, 2.000000, -5.416155), Pos);
126 finally FreeAndNil(SceneManager) end;
127
128 SceneManager := TCastleSceneManager.Create(nil);
129 try
130 Scene := TCastleScene.Create(SceneManager);
131 Scene.Load('castle-data:/level1.x3d');
132 Scene.PreciseCollisions := true;
133 Scene.ProcessEvents := true;
134
135 SceneManager.Items.Add(Scene);
136 SceneManager.MainScene := Scene;
137
138 { Note that this depends on hack within RequiredNavigation:
139 We depend that RequiredNavigation calls EnsureCameraDetected,
140 thus Camera position/dir/up are correct after RequiredNavigation
141 (even though it's not guaranteed in docs). }
142
143 // changes Examine into Walk, preserving the camera view
144 SceneManager.NavigationType := ntWalk;
145 SceneManager.WalkNavigation.MoveSpeed := 10;
146 SceneManager.Navigation.Camera.GetWorldView(Pos2, Dir2, Up2);
147 AssertVectorEquals(Pos, Pos2);
148 AssertVectorEquals(Vector3(3.249692, 2.000000, -5.416155), Pos);
149 AssertVectorEquals(Dir, Dir2);
150 AssertVectorEquals(Up, Up2);
151 AssertTrue(SceneManager.NavigationType = ntWalk);
152
153 AssertTrue(
154 SceneManager.WalkNavigation.PreferredHeight >
155 SceneManager.WalkNavigation.Radius);
156
157 AssertSameValue(1.75, SceneManager.WalkNavigation.PreferredHeight);
158 AssertSameValue(0.25, SceneManager.WalkNavigation.Radius);
159 AssertSameValue(0.75, SceneManager.WalkNavigation.ClimbHeight);
160 finally FreeAndNil(SceneManager) end;
161 end;
162
163 initialization
164 RegisterTest(TTestCastleSceneManager);
165 end.