File: testcastlescenemanager.pas

package info (click to toggle)
castle-game-engine 7.0~alpha.3%2Bdfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 969,476 kB
  • sloc: pascal: 911,523; javascript: 28,186; cpp: 14,157; xml: 9,939; ansic: 9,229; java: 3,653; objc: 2,737; sh: 1,214; makefile: 657; php: 65; lisp: 21; ruby: 8
file content (165 lines) | stat: -rw-r--r-- 6,064 bytes parent folder | download | duplicates (2)
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
// -*- compile-command: "./test_single_testcase.sh TTestCastleSceneManager" -*-
{
  Copyright 2020-2023 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Test CastleSceneManager unit. }
unit TestCastleSceneManager;

interface

uses
  Classes, SysUtils, CastleTester;

type
  TTestCastleSceneManager = class(TCastleTestCase)
  published
    procedure TestNavigationCreating;
    procedure TestLoadLevel;
  end;

implementation

uses CastleCameras, CastleSceneManager, CastleScene, CastleSceneCore,
  CastleFilesUtils, CastleVectors, CastleViewport;

procedure TTestCastleSceneManager.TestNavigationCreating;
var
  SceneManager: TCastleSceneManager;
  C: TCastleNavigation;
begin
  SceneManager := TCastleSceneManager.Create(nil);
  try
    AssertTrue(SceneManager.Navigation = nil);
    AssertTrue(SceneManager.WalkNavigation(false) = nil);
    AssertTrue(SceneManager.ExamineNavigation(false) = nil);
    AssertTrue(SceneManager.Navigation = nil);

    SceneManager.NavigationType := ntWalk;
    C := SceneManager.Navigation;
    AssertTrue(C <> nil);
    AssertTrue(SceneManager.NavigationType = ntWalk);

    SceneManager.NavigationType := ntFly;
    AssertTrue(C = SceneManager.Navigation); // ntWalk -> ntFly didn't change camera instance
    AssertTrue(C = SceneManager.WalkNavigation);
    AssertTrue(C = SceneManager.WalkNavigation(false));
    AssertTrue(SceneManager.NavigationType = ntFly);

    C.Radius := 100;
    AssertTrue(SceneManager.Navigation.Radius = 100);

    SceneManager.NavigationType := ntExamine;
    AssertTrue(C <> SceneManager.Navigation); // ntFly -> ntExamine did change camera instance
    AssertTrue(SceneManager.NavigationType = ntExamine);
    // but the Radius was preserved when copying
    AssertTrue(SceneManager.Navigation.Radius = 100);
  finally FreeAndNil(SceneManager) end;

  SceneManager := TCastleSceneManager.Create(nil);
  try
    AssertTrue(SceneManager.Navigation = nil);

    SceneManager.WalkNavigation.Radius := 100;
    AssertTrue(SceneManager.Navigation <> nil);
    AssertTrue(SceneManager.Navigation = SceneManager.WalkNavigation);
    AssertTrue(SceneManager.NavigationType = ntWalk);
    AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForRotations);
    AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForMoving);
    AssertTrue(SceneManager.WalkNavigation.Gravity);

    C := SceneManager.WalkNavigation;
    SceneManager.NavigationType := ntFly;
    AssertTrue(SceneManager.Navigation = SceneManager.WalkNavigation);
    AssertTrue(C = SceneManager.WalkNavigation);
    AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForRotations);
    AssertFalse(SceneManager.WalkNavigation.PreferGravityUpForMoving);
    AssertFalse(SceneManager.WalkNavigation.Gravity);

    // changing to ExamineNavigation and back to WalkNavigation sets ntWalk again
    SceneManager.ExamineNavigation;
    SceneManager.WalkNavigation;
    AssertTrue(SceneManager.NavigationType = ntWalk);
    AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForRotations);
    AssertTrue(SceneManager.WalkNavigation.PreferGravityUpForMoving);
    AssertTrue(SceneManager.WalkNavigation.Gravity);
  finally FreeAndNil(SceneManager) end;
end;

procedure TTestCastleSceneManager.TestLoadLevel;
var
  Scene: TCastleScene;
  SceneManager: TCastleSceneManager;
  Pos, Dir, Up, Pos2, Dir2, Up2: TVector3;
begin
  SceneManager := TCastleSceneManager.Create(nil);
  try
    Scene := TCastleScene.Create(SceneManager);
    Scene.Load('castle-data:/level1.x3d');
    Scene.PreciseCollisions := true;
    Scene.ProcessEvents := true;

    SceneManager.Items.Add(Scene);
    SceneManager.MainScene := Scene;

    { Note that this depends on hack within RequiredNavigation:
      We depend that RequiredNavigation calls EnsureCameraDetected,
      thus Camera position/dir/up are correct after RequiredNavigation
      (even though it's not guaranteed in docs). }

    AssertTrue(SceneManager.NavigationType = ntNone);
    SceneManager.RequiredNavigation;
    AssertTrue(SceneManager.NavigationType = ntExamine);
    SceneManager.Navigation.Camera.GetWorldView(Pos, Dir, Up);
    AssertVectorEquals(Vector3(3.249692, 2.000000, -5.416155), Pos);
  finally FreeAndNil(SceneManager) end;

  SceneManager := TCastleSceneManager.Create(nil);
  try
    Scene := TCastleScene.Create(SceneManager);
    Scene.Load('castle-data:/level1.x3d');
    Scene.PreciseCollisions := true;
    Scene.ProcessEvents := true;

    SceneManager.Items.Add(Scene);
    SceneManager.MainScene := Scene;

    { Note that this depends on hack within RequiredNavigation:
      We depend that RequiredNavigation calls EnsureCameraDetected,
      thus Camera position/dir/up are correct after RequiredNavigation
      (even though it's not guaranteed in docs). }

    // changes Examine into Walk, preserving the camera view
    SceneManager.NavigationType := ntWalk;
    SceneManager.WalkNavigation.MoveSpeed := 10;
    SceneManager.Navigation.Camera.GetWorldView(Pos2, Dir2, Up2);
    AssertVectorEquals(Pos, Pos2);
    AssertVectorEquals(Vector3(3.249692, 2.000000, -5.416155), Pos);
    AssertVectorEquals(Dir, Dir2);
    AssertVectorEquals(Up, Up2);
    AssertTrue(SceneManager.NavigationType = ntWalk);

    AssertTrue(
      SceneManager.WalkNavigation.PreferredHeight >
      SceneManager.WalkNavigation.Radius);

    AssertSameValue(1.75, SceneManager.WalkNavigation.PreferredHeight);
    AssertSameValue(0.25, SceneManager.WalkNavigation.Radius);
    AssertSameValue(0.75, SceneManager.WalkNavigation.ClimbHeight);
  finally FreeAndNil(SceneManager) end;
end;

initialization
  RegisterTest(TTestCastleSceneManager);
end.