Debian Codegraph @

examples/eye_of_beholder/code/gameviewplay.pas

raw
1 {
2 Copyright 2023-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
16 { Main "playing game" view, where most of the game logic takes place. }
17 unit GameViewPlay;
18
19 interface
20
21 uses Classes,
22 CastleComponentSerialize, CastleUIControls, CastleControls,
23 CastleKeysMouse, CastleViewport, CastleScene, CastleVectors, CastleCameras,
24 CastleTransform;
25
26 type
27 { Direction expressed a 4 possible horizontal directions.
28 In the clockwise order. }
29 TDirection = (dirNorth, dirEast, dirSouth, dirWest);
30
31 const
32 { Convert TDirection to MainViewport.Camera.Direction vector.
33 Check these values looking at MainViewport.Camera.Direction (in "All" tab)
34 in editor. }
35 DirectionVector: array [TDirection] of TVector3 = (
36 (X: 0; Y: 0; Z: -1),
37 (X: 1; Y: 0; Z: 0),
38 (X: 0; Y: 0; Z: 1),
39 (X: -1; Y: 0; Z: 0)
40 );
41
42 DirectionName: array [TDirection] of String = (
43 'North',
44 'East',
45 'South',
46 'West'
47 );
48
49 { Time, in seconds, to perform move or rotation. }
50 ActionDuration = 0.25;
51
52 type
53 { Main "playing game" view, where most of the game logic takes place. }
54 TViewPlay = class(TCastleView)
55 published
56 { Components designed using CGE editor.
57 These fields will be automatically initialized at Start. }
58 LabelFps: TCastleLabel;
59 MainViewport: TCastleViewport;
60 MainFog: TCastleFog;
61 ButtonMoveLeft: TCastleButton;
62 ButtonMoveBackward: TCastleButton;
63 ButtonMoveRight: TCastleButton;
64 ButtonMoveForward: TCastleButton;
65 ButtonRotateLeft: TCastleButton;
66 ButtonRotateRight: TCastleButton;
67 LabelDirection: TCastleLabel;
68 private
69 { Always synchronized with MainViewport.Camera.Direction. }
70 Direction: TDirection;
71
72 { Calculate "Value + Increase"
73 doing typecasting as necessary between enums and integers,
74 and making sure result is in TDir range. }
75 function IncreaseDirection(const Value: TDirection; const Increase: Integer): TDirection;
76
77 procedure Move(const MoveDirection: TDirection);
78 procedure Rotate(const RotationChange: Integer);
79 procedure ClickMoveLeft(Sender: TObject);
80 procedure ClickMoveBackward(Sender: TObject);
81 procedure ClickMoveRight(Sender: TObject);
82 procedure ClickMoveForward(Sender: TObject);
83 procedure ClickRotateLeft(Sender: TObject);
84 procedure ClickRotateRight(Sender: TObject);
85 public
86 constructor Create(AOwner: TComponent); override;
87 procedure Start; override;
88 procedure Stop; override;
89 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
90 function Press(const Event: TInputPressRelease): Boolean; override;
91 end;
92
93 var
94 ViewPlay: TViewPlay;
95
96 implementation
97
98 uses SysUtils, Math,
99 CastleLog, CastleStringUtils, CastleFilesUtils, CastleUtils,
100 GameViewMenu;
101
102 { TViewPlay ----------------------------------------------------------------- }
103
104 constructor TViewPlay.Create(AOwner: TComponent);
105 begin
106 inherited;
107 DesignUrl := 'castle-data:/gameviewplay.castle-user-interface';
108 end;
109
110 procedure TViewPlay.Start;
111 begin
112 inherited;
113
114 { Make sure Dir, MainViewport.Camera.Direction and LabelDirection are initially synchronized. }
115 Direction := dirNorth;
116 MainViewport.Camera.Direction := DirectionVector[Direction];
117 LabelDirection.Caption := 'Looking at: ' + DirectionName[Direction];
118
119 { At design-time, we keep MainFog.VisibilityRange larger,
120 otherwise it makes it hard to actually see level at design-time from top. }
121 MainFog.VisibilityRange := 5;
122
123 { Assign events }
124 ButtonMoveLeft.OnClick := {$ifdef FPC}@{$endif} ClickMoveLeft;
125 ButtonMoveBackward.OnClick := {$ifdef FPC}@{$endif} ClickMoveBackward;
126 ButtonMoveRight.OnClick := {$ifdef FPC}@{$endif} ClickMoveRight;
127 ButtonMoveForward.OnClick := {$ifdef FPC}@{$endif} ClickMoveForward;
128 ButtonRotateLeft.OnClick := {$ifdef FPC}@{$endif} ClickRotateLeft;
129 ButtonRotateRight.OnClick := {$ifdef FPC}@{$endif} ClickRotateRight;
130 end;
131
132 procedure TViewPlay.Stop;
133 begin
134 inherited;
135 end;
136
137 procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean);
138 begin
139 inherited;
140 { This virtual method is executed every frame (many times per second). }
141 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
142 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
143
144 { Disable buttons to move/rotate when we're during camera animation.
145 The Move/Rotate methods would ignore the call anyway,
146 but disabling the buttons allows to show it visually. }
147 ButtonMoveLeft.Enabled := not MainViewport.Camera.Animation;
148 ButtonMoveBackward.Enabled := not MainViewport.Camera.Animation;
149 ButtonMoveRight.Enabled := not MainViewport.Camera.Animation;
150 ButtonMoveForward.Enabled := not MainViewport.Camera.Animation;
151 ButtonRotateLeft.Enabled := not MainViewport.Camera.Animation;
152 ButtonRotateRight.Enabled := not MainViewport.Camera.Animation;
153 end;
154
155 function TViewPlay.IncreaseDirection(const Value: TDirection; const Increase: Integer): TDirection;
156 begin
157 Result := TDirection(ChangeIntCycle(Ord(Value), Increase, Ord(High(TDirection))));
158 end;
159
160 procedure TViewPlay.Move(const MoveDirection: TDirection);
161 const
162 GridSize = 1;
163 var
164 NewPos, Pos, Dir, Up: TVector3;
165 begin
166 if MainViewport.Camera.Animation then
167 Exit;
168 MainViewport.Camera.GetWorldView(Pos, Dir, Up);
169 NewPos := Pos + DirectionVector[MoveDirection] * GridSize;
170 if not MainViewport.Items.WorldSegmentCollision(Pos, NewPos) then
171 MainViewport.Camera.AnimateTo(NewPos, Dir, Up, ActionDuration);
172 end;
173
174 procedure TViewPlay.Rotate(const RotationChange: Integer);
175 var
176 Pos, Dir, Up: TVector3;
177 begin
178 if MainViewport.Camera.Animation then
179 Exit;
180 MainViewport.Camera.GetWorldView(Pos, Dir, Up);
181 Direction := IncreaseDirection(Direction, RotationChange);
182 LabelDirection.Caption := 'Looking at: ' + DirectionName[Direction];
183 Dir := DirectionVector[Direction];
184 MainViewport.Camera.AnimateTo(Pos, Dir, Up, ActionDuration);
185 end;
186
187 function TViewPlay.Press(const Event: TInputPressRelease): Boolean;
188 begin
189 Result := inherited;
190 if Result then Exit; // allow the ancestor to handle keys
191
192 if not MainViewport.Camera.Animation then
193 begin
194 if Event.IsKey(keyW) then
195 begin
196 Move(IncreaseDirection(Direction, 0)); // Move along Direction
197 Exit(true);
198 end;
199 if Event.IsKey(keyS) then
200 begin
201 Move(IncreaseDirection(Direction, 2)); // Move along the opposite of Direction
202 Exit(true);
203 end;
204 if Event.IsKey(keyD) then
205 begin
206 Move(IncreaseDirection(Direction, 1)); // Move along Direction rotated to right (i.e. strafe right)
207 Exit(true);
208 end;
209 if Event.IsKey(keyA) then
210 begin
211 Move(IncreaseDirection(Direction, -1)); // Move along Direction rotated to left (i.e. strafe left)
212 Exit(true);
213 end;
214
215 if Event.IsKey(keyQ) then
216 begin
217 Rotate(-1);
218 Exit(true);
219 end;
220 if Event.IsKey(keyE) then
221 begin
222 Rotate(1);
223 Exit(true);
224 end;
225 end;
226
227 if Event.IsKey(keyF5) then
228 begin
229 Container.SaveScreenToDefaultFile;
230 Exit(true);
231 end;
232
233 if Event.IsKey(keyEscape) then
234 begin
235 Container.View := ViewMenu;
236 Exit(true);
237 end;
238 end;
239
240 procedure TViewPlay.ClickMoveLeft(Sender: TObject);
241 begin
242 Move(IncreaseDirection(Direction, -1));
243 end;
244
245 procedure TViewPlay.ClickMoveBackward(Sender: TObject);
246 begin
247 Move(IncreaseDirection(Direction, 2));
248 end;
249
250 procedure TViewPlay.ClickMoveRight(Sender: TObject);
251 begin
252 Move(IncreaseDirection(Direction, 1));
253 end;
254
255 procedure TViewPlay.ClickMoveForward(Sender: TObject);
256 begin
257 Move(IncreaseDirection(Direction, 0));
258 end;
259
260 procedure TViewPlay.ClickRotateLeft(Sender: TObject);
261 begin
262 Rotate(-1);
263 end;
264
265 procedure TViewPlay.ClickRotateRight(Sender: TObject);
266 begin
267 Rotate(1);
268 end;
269
270 end.