/
gameviewplay.pas
243 lines (200 loc) · 6.55 KB
/
gameviewplay.pas
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
Copyright 2022-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.
----------------------------------------------------------------------------
}
{ Playing the game. }
unit GameViewPlay;
interface
uses Classes, CastleCameras,
CastleVectors, CastleUIControls, CastleControls, CastleKeysMouse,
CastleViewport, CastleSceneCore, X3DNodes, CastleScene, CastleSoundEngine,
CastleBehaviors, CastleNotifications, CastleTransform,
GameEnemy;
type
TViewPlay = class(TCastleView)
published
{ Components designed using CGE editor.
These fields will be automatically initialized at Start. }
WalkNavigation: TCastleWalkNavigation;
MainViewport: TCastleViewport;
SceneGun: TCastleScene;
SoundSourceFootsteps: TCastleSoundSource;
SoundShoot: TCastleSound;
BoxDieDetect, BoxWinDetect: TCastleTransform;
DesignHud: TCastleDesign;
MapCamera: TCastleCamera;
private
PersistentMouseLook: Boolean;
Enemies: TEnemyList;
{ components in DesignHud }
LabelFps: TCastleLabel;
MainNotifications: TCastleNotifications;
MapViewport: TCastleViewport;
procedure UpdateMouseLook;
procedure WeaponShootAnimationStop(const Scene: TCastleSceneCore;
const Animation: TTimeSensorNode);
public
constructor Create(AOwner: TComponent); override;
procedure Start; override;
procedure Stop; override;
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
function Press(const Event: TInputPressRelease): Boolean; override;
end;
var
ViewPlay: TViewPlay;
implementation
uses SysUtils, Math,
CastleComponentSerialize, CastleLog,
GameViewWin, GameViewDeath, GameViewOptions;
constructor TViewPlay.Create(AOwner: TComponent);
begin
inherited;
DesignUrl := 'castle-data:/gameviewplay.castle-user-interface';
end;
procedure TViewPlay.Start;
var
SceneName: String;
SceneEnemy: TCastleScene;
Enemy: TEnemy;
I: Integer;
begin
inherited;
{ initialize components in DesignHud }
LabelFps := DesignHud.DesignedComponent('LabelFps') as TCastleLabel;
MainNotifications := DesignHud.DesignedComponent('MainNotifications') as TCastleNotifications;
MapViewport := DesignHud.DesignedComponent('MapViewport') as TCastleViewport;
Enemies := TEnemyList.Create(true);
for I := 1 to 7 do
begin
SceneName := 'SceneKnight' + IntToStr(I);
try
SceneEnemy := DesignedComponent(SceneName) as TCastleScene;
Enemy := TEnemy.Create(FreeAtStop);
SceneEnemy.AddBehavior(Enemy);
except
on EComponentNotFound do
WritelnWarning('Cannot find "%s"', [SceneName]);
end;
end;
MapViewport.Items := MainViewport.Items;
MapViewport.Camera := MapCamera;
PersistentMouseLook := true;
end;
procedure TViewPlay.Stop;
begin
FreeAndNil(Enemies);
inherited;
end;
procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean);
var
DirectionHorizontal: TVector3;
GameActive: Boolean;
begin
inherited;
LabelFps.Caption := Container.Fps.ToString;
UpdateMouseLook;
GameActive := Container.FrontView = Self;
SoundSourceFootsteps.Volume := IfThen(WalkNavigation.IsWalkingOnTheGround and GameActive, 1, 0);
MainViewport.Items.Paused := not GameActive;
if GameActive then
begin
DirectionHorizontal := MainViewport.Camera.Direction;
if not VectorsParallel(DirectionHorizontal, MainViewport.Camera.GravityUp) then
MakeVectorsOrthoOnTheirPlane(DirectionHorizontal, MainViewport.Camera.GravityUp);
MapViewport.Camera.SetView(
MainViewport.Camera.WorldTranslation + Vector3(0, 30, 0),
Vector3(0, -1, 0),
DirectionHorizontal
);
if BoxWinDetect.WorldBoundingBox.Contains(MainViewport.Camera.WorldTranslation) then
begin
Container.PushView(ViewWin);
Exit;
end;
if BoxDieDetect.WorldBoundingBox.Contains(MainViewport.Camera.WorldTranslation) then
begin
Container.PushView(ViewDeath);
Exit;
end;
end;
end;
procedure TViewPlay.UpdateMouseLook;
begin
WalkNavigation.MouseLook := (Container.FrontView = Self) and
( PersistentMouseLook or
(buttonRight in Container.MousePressed) );
end;
procedure TViewPlay.WeaponShootAnimationStop(const Scene: TCastleSceneCore;
const Animation: TTimeSensorNode);
begin
Scene.PlayAnimation('idle', true);
end;
function TViewPlay.Press(const Event: TInputPressRelease): Boolean;
var
HitEnemy: TEnemy;
PlayAnimationParams: TPlayAnimationParameters;
begin
Result := inherited;
if Result then Exit; // allow the ancestor to handle keys
if Container.FrontView = Self then
begin
if Event.IsKey(keyF4) then
begin
PersistentMouseLook := not PersistentMouseLook;
UpdateMouseLook;
Exit(true);
end;
if Event.IsMouseButton(buttonLeft) then
begin
if MainViewport.TransformUnderMouse <> nil then
WritelnLog('Clicked on ' + MainViewport.TransformUnderMouse.Name);
PlayAnimationParams := TPlayAnimationParameters.Create;
try
PlayAnimationParams.Name := 'primary';
PlayAnimationParams.StopNotification := {$ifdef FPC}@{$endif} WeaponShootAnimationStop;
PlayAnimationParams.Loop := false;
SceneGun.PlayAnimation(PlayAnimationParams);
finally FreeAndNil(PlayAnimationParams) end;
SoundEngine.Play(SoundShoot);
{ We clicked on enemy if
- TransformUnderMouse indicates we hit something
- It has a behavior of TEnemy. }
if (MainViewport.TransformUnderMouse <> nil) and
(MainViewport.TransformUnderMouse.FindBehavior(TEnemy) <> nil) then
begin
HitEnemy := MainViewport.TransformUnderMouse.FindBehavior(TEnemy) as TEnemy;
HitEnemy.Hurt;
MainNotifications.Show('Killed ' + HitEnemy.Parent.Name);
end;
Exit(true);
end;
if Event.IsKey(keyEscape) then
begin
ViewOptions.OverGame := true;
Container.PushView(ViewOptions);
Exit(true);
end;
if Event.IsKey(keyP) then
begin
Container.PushView(ViewWin);
Exit(true);
end;
if Event.IsKey(keyO) then
begin
Container.PushView(ViewDeath);
Exit(true);
end;
end;
if Event.IsKey(keyF5) then
begin
MainNotifications.Show('Saved screenshot to ' + Container.SaveScreenToDefaultFile);
Exit(true);
end;
end;
end.