Debian Codegraph @

tests/code/testcases/testcastlegame.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestGame" -*-
2 {
3 Copyright 2004-2022 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 unit TestCastleGame;
18
19 interface
20
21 uses
22 Classes, SysUtils, CastleTester;
23
24 type
25 TTestGame = class(TCastleTestCase)
26 procedure TestGameData;
27 end;
28
29 implementation
30
31 uses CastleVectors, CastleLevels, CastleResources, CastleSoundEngine, CastlePlayer,
32 CastleInternalMaterialProperties, CastleCreatures, CastleShapes, CastleTransform,
33 CastleUriUtils;
34
35 procedure TTestGame.TestGameData;
36
37 procedure AssertFloat(const A, B: Single);
38 begin
39 AssertSameValue(A, B, 0.01);
40 end;
41
42 procedure AssertURL(const A, B: string);
43 begin
44 { When reading XML files, we make URLs absolute. For comparison,
45 strip directory part. }
46 AssertEquals(ExtractURIName(A), B);
47 end;
48
49 procedure AssertSound(const A: TCastleSound; const B: string);
50 begin
51 AssertTrue(A = SoundEngine.SoundFromName(B));
52 end;
53
54 procedure AssertVector(const A: TVector3; const BX, BY, BZ: Single);
55 begin
56 AssertVectorEquals(A, Vector3(BX, BY, BZ), 0.01);
57 end;
58
59 var
60 Player: TPlayer;
61 RemovePlayer: TRemoveType;
62 //SoundType: TCastleSound;
63 C: TWalkAttackCreatureResource;
64 MatProp: TMaterialProperty;
65 begin
66 SoundEngine.RepositoryURL := 'castle-data:/game/sounds.xml';
67
68 Resources.LoadFromFiles('castle-data:/game/');
69
70 AssertTrue(Resources.Count = 1);
71 AssertTrue(Resources[0].Name = 'TestCreature');
72 AssertTrue(Resources[0].ClassType = TWalkAttackCreatureResource);
73 C := Resources[0] as TWalkAttackCreatureResource;
74 AssertFloat(C.KnockBackSpeed, 1.2);
75 AssertFloat(C.KnockBackDistance, 3.4);
76 AssertTrue(C.Flying = true);
77 AssertTrue(C.SoundDieTiedToCreature = true);
78 AssertFloat(C.DefaultMaxLife, 5.6);
79 AssertFloat(C.RadiusOverride, 7.8);
80 AssertFloat(C.MiddleHeight, 6.7);
81 AssertSound(C.SoundSuddenPain, 'test_sound_6');
82 AssertSound(C.SoundDie, 'test_sound_7');
83 AssertFloat(C.MoveSpeed, 1.2);
84 AssertFloat(C.MinLifeLossToHurt, 3.4);
85 AssertFloat(C.ChanceToHurt, 0.56);
86 AssertFloat(C.MaxHeightAcceptableToFall, 5.6);
87 AssertFloat(C.RandomWalkDistance, 7.8);
88 AssertTrue(C.RemoveDead = true);
89 AssertFloat(C.PreferredDistance, 9.1);
90 AssertTrue(C.AlwaysPrepared = true);
91 { change AlwaysPrepared to false,
92 to avoid crashing tests that actually do LoadLevel,
93 since 3D models referenced by this resource do not exist. }
94 C.ConfigAlwaysPrepared := false;
95 AssertFloat(C.FallSpeed, 1.2);
96 AssertFloat(C.GrowSpeed, 3.4);
97 AssertTrue(C.ReceiveShadowVolumes = false);
98 AssertTrue(C.CastShadowVolumes = false);
99 AssertURL(C.ModelURL, 'main.x3d');
100 AssertTrue(C.IdleAnimation.Defined);
101 // AssertURL(C.IdleAnimation.URL, 'idle.x3d')); // private
102 // AssertTrue(C.IdleAnimation.TimeSensor = 'TimeSensorIdle'); // private
103 AssertTrue(C.IdleToWalkAnimation.Defined);
104 AssertTrue(C.WalkAnimation.Defined);
105 AssertTrue(C.FireMissileAnimation.Defined);
106 AssertTrue(C.AttackAnimation.Defined);
107 AssertTrue(C.DieAnimation.Defined);
108 AssertTrue(C.DieBackAnimation.Defined);
109 AssertTrue(C.HurtAnimation.Defined);
110 AssertFloat(C.AttackKnockbackDistance, 4.5);
111 AssertFloat(C.AttackTime, 7.8);
112 AssertFloat(C.AttackMaxDistance, 9.1);
113 AssertFloat(C.AttackMaxAngle, 2.3);
114 AssertFloat(C.AttackMinDelay, 4.5);
115 AssertSound(C.AttackSoundHit, 'test_sound_6');
116 AssertSound(C.AttackSoundStart, 'test_sound_7');
117 AssertFloat(C.AttackDamageConst, 9.1);
118 AssertFloat(C.AttackDamageRandom, 2.3);
119 AssertFloat(C.FireMissileTime, 1.2);
120 AssertFloat(C.FireMissileMaxDistance, 3.4);
121 AssertFloat(C.FireMissileMaxAngle, 5.6);
122 AssertFloat(C.FireMissileMinDelay, 7.8);
123 AssertSound(C.FireMissileSound, 'test_sound_8');
124 AssertTrue(C.FireMissileName = 'TestMissileCreature');
125 AssertFloat(C.FireMissileHeight, 0.12);
126 AssertFloat(C.FallMinHeightToSound, 7.8);
127 AssertSound(C.FallSound, 'test_sound_5');
128 AssertFloat(C.FallMinHeightToDamage, 1.2);
129 AssertFloat(C.FallDamageScaleMin, 3.4);
130 AssertFloat(C.FallDamageScaleMax, 5.6);
131 AssertFloat(C.RunAwayLife, 1.2);
132 AssertFloat(C.RunAwayDistance, 3.4);
133 AssertFloat(C.VisibilityAngle, 5.6);
134
135 Levels.LoadFromFiles('castle-data:/game/');
136
137 AssertTrue(Levels[0].Name = 'my_level');
138 AssertTrue(Levels[0].LogicClass = TLevelLogic);
139 AssertURL(Levels[0].SceneURL, 'scene.x3d');
140 AssertTrue(Levels[0].Title = 'My Level');
141 AssertTrue(Levels[0].Number = 123);
142 AssertTrue(Levels[0].Demo = true);
143 AssertTrue(Levels[0].TitleHint = 'Title Hint');
144 AssertTrue(Levels[0].DefaultPlayed = true);
145 AssertTrue({$ifndef FPC}@{$endif}Levels[0].PlaceholderName = {$ifndef FPC}@{$endif}PlaceholderNames['blender']);
146 AssertTrue(not Levels[0].LoadingImage.IsEmpty);
147 AssertTrue(Levels[0].LoadingImage.Width = 16);
148 AssertTrue(Levels[0].LoadingImage.Height = 16);
149 AssertFloat(Levels[0].LoadingBarYPosition, 1.2);
150 AssertVector(Levels[0].PlaceholderReferenceDirection, 1, 2, 3);
151 AssertSound(Levels[0].MusicSound, 'test_sound_2');
152
153 MaterialProperties.URL := 'castle-data:/game/material_properties.xml';
154
155 // not exposed anymore
156 //AssertTrue(MaterialProperties.Count = 2);
157
158 MatProp := MaterialProperties.FindTextureBaseName('test_texture');
159 AssertSound(MatProp.FootstepsSound, 'test_sound_4');
160 AssertTrue(MatProp.Toxic = true);
161 AssertFloat(MatProp.ToxicDamageConst, 1.2);
162 AssertFloat(MatProp.ToxicDamageRandom, 3.4);
163 AssertFloat(MatProp.ToxicDamageTime, 5.6);
164 AssertURL(MatProp.NormalMap, '');
165
166 MatProp := MaterialProperties.FindTextureBaseName('test_texture_2');
167 AssertTrue(MatProp.TextureBaseName = 'test_texture_2');
168 AssertTrue(MatProp.FootstepsSound = nil);
169 AssertTrue(MatProp.Toxic = false);
170 AssertFloat(MatProp.ToxicDamageConst, 0.0);
171 AssertFloat(MatProp.ToxicDamageRandom, 0.0);
172 AssertFloat(MatProp.ToxicDamageTime, 0.0);
173 AssertURL(MatProp.NormalMap, 'test_normal_map.png');
174
175 Player := TPlayer.Create(nil);
176 try
177 Player.LoadFromFile('castle-data:/game/player.xml');
178
179 AssertFloat(Player.KnockBackSpeed, 1.2);
180 AssertFloat(Player.WalkNavigation.HeadBobbingTime, 9.1);
181 AssertFloat(Player.HeadBobbing, 2.3);
182 AssertFloat(Player.SickProjectionSpeed, 4.5);
183 AssertFloat(Player.WalkNavigation.JumpMaxHeight, 3.4);
184 AssertFloat(Player.WalkNavigation.JumpHorizontalSpeedMultiply, 5.6);
185 AssertFloat(Player.WalkNavigation.JumpTime, 7.8);
186 AssertFloat(Player.FallMinHeightToSound, 6.7);
187 AssertSound(Player.FallSound, 'test_sound_1');
188 AssertFloat(Player.FallMinHeightToDamage, 8.9);
189 AssertFloat(Player.FallDamageScaleMin, 1.2);
190 AssertFloat(Player.FallDamageScaleMax, 3.4);
191 AssertFloat(Player.SwimBreath, 5.6);
192 AssertFloat(Player.SwimSoundPause, 6.7);
193 AssertFloat(Player.DrownPause, 7.8);
194 AssertFloat(Player.DrownDamageConst, 9.1);
195 AssertFloat(Player.DrownDamageRandom, 2.3);
196
197 RemovePlayer := rtNone;
198 Player.Update(0, RemovePlayer);
199 // ignore resulting RemovePlayer
200
201 { some properties are applied to Camera with delay }
202 AssertFloat(Player.WalkNavigation.HeadBobbing, 2.3);
203
204 finally FreeAndNil(Player) end;
205
206 { clear global stuff, so that other tests (or this one test, run 2nd time)
207 go without errors }
208 SoundEngine.RepositoryURL := '';
209 MaterialProperties.URL := '';
210 Resources.Clear;
211 Levels.Clear;
212 end;
213
214 initialization
215 RegisterTest(TTestGame);
216 end.