Debian Codegraph @

examples/deprecated_to_upgrade/fixed_camera_game/code/gameviewplay.pas

raw
1 {
2 Copyright 2008-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 { View in which you actually play the game (TViewPlay). }
17 unit GameViewPlay;
18
19 interface
20
21 uses
22 CastleUIControls, CastleViewport, X3DNodes, CastleProjection,
23 CastleTransform, CastleVectors, CastleKeysMouse, CastleControls,
24 GameCreatures, GameLocations;
25
26 type
27 { View in which you actually play the game. }
28 TViewPlay = class(TCastleView)
29 strict private
30 Player: TPlayer;
31 CurrentLocation: TLocation;
32 Viewport: TCastleViewport;
33 InfoLabel: TCastleLabel;
34 public
35 procedure Start; override;
36 procedure Stop; override;
37 procedure Resize; override;
38 function Press(const Event: TInputPressRelease): boolean; override;
39 procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
40 end;
41
42 var
43 ViewPlay: TViewPlay;
44
45 implementation
46
47 uses Math, SysUtils,
48 CastleGLUtils, CastleStringUtils, CastleUtils, CastleCameras,
49 CastleFilesUtils, CastleImages, CastleScene,
50 CastleGameNotifications, CastleRectangles, CastleColors,
51 GameViewMainMenu;
52
53 { TViewPlay ----------------------------------------------------------------------- }
54
55 procedure TViewPlay.Resize;
56 begin
57 inherited;
58 CurrentLocation.ImageTransform.ViewportRect := Viewport.RenderRect.Round;
59 end;
60
61 procedure TViewPlay.Start;
62 var
63 Location: TLocation;
64 CreatureKind: TCreatureKind;
65 LightForShadows: TCastleDirectionalLight;
66 begin
67 inherited;
68 CurrentLocation := Locations.StartLocation;
69
70 Notifications.Clear;
71 InsertFront(Notifications);
72
73 Viewport := TCastleViewport.Create(FreeAtStop);
74 Viewport.FullSize := true;
75 Viewport.AutoCamera := true;
76 InsertFront(Viewport);
77
78 { TODO: light type, direction, should be configured in .castle-transform
79 that also contains location model.
80 This is prevented now because it needs a special TLocationScene to render. }
81 LightForShadows := TCastleDirectionalLight.Create(FreeAtStop);
82 LightForShadows.Shadows := true;
83 LightForShadows.Direction := Vector3(-1, 1, 1);
84 Viewport.Items.Add(LightForShadows);
85
86 for Location in Locations do
87 begin
88 Location.Load(Viewport.PrepareParams);
89 end;
90 for CreatureKind in CreatureKinds do
91 begin
92 CreatureKind.Load(Viewport.PrepareParams);
93 end;
94
95 Viewport.Items.Add(CurrentLocation.ImageTransform);
96 Viewport.Items.Add(CurrentLocation.Scene);
97 { set as MainScene, to allow location VRML / X3D file to determine
98 headlight, viewpoint, shadow volumes light... }
99 Viewport.Items.MainScene := CurrentLocation.Scene;
100
101 Player := TPlayer.Create(CreatureKinds.PlayerKind);
102 Player.SetView(
103 CurrentLocation.PlayerPosition,
104 CurrentLocation.PlayerDirection,
105 // hardcode up vector to +Y, this is easier for moving calculations
106 Vector3(0, 1, 0));
107 Player.LocationChanged;
108 Viewport.Items.Add(Player);
109
110 InfoLabel := TCastleLabel.Create(FreeAtStop);
111 InfoLabel.Color := White;
112 InfoLabel.Anchor(hpRight, -5);
113 InfoLabel.Anchor(vpTop, -5);
114 InsertFront(InfoLabel);
115 end;
116
117 procedure TViewPlay.Stop;
118 begin
119 FreeAndNil(Player);
120 inherited;
121 end;
122
123 procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: boolean);
124 begin
125 inherited;
126 InfoLabel.Caption :=
127 'FPS: ' + Container.Fps.ToString + NL +
128 '[Click] to move.' + NL +
129 '[F2] toggles debug rendering.' + NL +
130 '[F5] to make screenshot.' + NL +
131 '[Escape] exit.';
132 end;
133
134 function TViewPlay.Press(const Event: TInputPressRelease): boolean;
135 var
136 Url: String;
137 begin
138 Result := inherited;
139
140 case Event.EventType of
141 itKey:
142 if Event.KeyCharacter = CharEscape then
143 Container.View := ViewMainMenu
144 else
145 { Debug keys }
146 case Event.Key of
147 keyF2:
148 CurrentLocation.RenderInternalModel :=
149 not CurrentLocation.RenderInternalModel;
150 keyF5:
151 begin
152 Url := Container.SaveScreenToDefaultFile;
153 if Url <> '' then
154 Notifications.Show(Format('Saved screenshot to "%s"', [Url]));
155 end;
156 end;
157 itMouseButton:
158 begin
159 if Event.MouseButton = buttonLeft then
160 begin
161 if Viewport.MouseRayHit <> nil then
162 Player.WantsToWalk(Viewport.MouseRayHit.Last.Point);
163 end;
164 end;
165 end;
166 end;
167
168 end.