Debian Codegraph @

tools/castle-editor/data/project_templates/3d_fps_game/files/code/gameviewplay.pas

raw
1 { Main "playing game" view, where most of the game logic takes place.
2
3 Feel free to use this code as a starting point for your own projects.
4 This template code is in public domain, unlike most other CGE code which
5 is covered by BSD or LGPL (see https://castle-engine.io/license). }
6 unit GameViewPlay;
7
8 interface
9
10 uses Classes,
11 CastleComponentSerialize, CastleUIControls, CastleControls,
12 CastleKeysMouse, CastleViewport, CastleScene, CastleVectors, CastleCameras,
13 CastleTransform,
14 GameEnemy;
15
16 type
17 { Main "playing game" view, where most of the game logic takes place. }
18 TViewPlay = class(TCastleView)
19 published
20 { Components designed using CGE editor.
21 These fields will be automatically initialized at Start. }
22 LabelFps: TCastleLabel;
23 MainViewport: TCastleViewport;
24 WalkNavigation: TCastleWalkNavigation;
25 SceneEnemy1, SceneEnemy2, SceneEnemy3, SceneEnemy4: TCastleScene;
26 SimpleCrosshair: TCastleCrosshair;
27 private
28 { Enemies behaviors }
29 Enemies: TEnemyList;
30 public
31 constructor Create(AOwner: TComponent); override;
32 procedure Start; override;
33 procedure Stop; override;
34 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
35 function Press(const Event: TInputPressRelease): Boolean; override;
36 end;
37
38 var
39 ViewPlay: TViewPlay;
40
41 implementation
42
43 uses SysUtils, Math,
44 CastleSoundEngine, CastleLog, CastleStringUtils, CastleFilesUtils,
45 GameViewMenu, GameSound;
46
47 { TViewPlay ----------------------------------------------------------------- }
48
49 constructor TViewPlay.Create(AOwner: TComponent);
50 begin
51 inherited;
52 DesignUrl := 'castle-data:/gameviewplay.castle-user-interface';
53 end;
54
55 procedure TViewPlay.Start;
56
57 procedure InitializeEnemy(const SceneEnemy: TCastleScene);
58 var
59 Enemy: TEnemy;
60 begin
61 { Below using nil as Owner of TEnemy, as the Enemies list already "owns"
62 instances of this class, i.e. it will free them. }
63 Enemy := TEnemy.Create(nil);
64 SceneEnemy.AddBehavior(Enemy);
65 Enemies.Add(Enemy);
66 end;
67
68 begin
69 inherited;
70
71 { Create TEnemy instances, add them to Enemies list }
72 Enemies := TEnemyList.Create(true);
73 InitializeEnemy(SceneEnemy1);
74 InitializeEnemy(SceneEnemy2);
75 InitializeEnemy(SceneEnemy3);
76 InitializeEnemy(SceneEnemy4);
77 end;
78
79 procedure TViewPlay.Stop;
80 begin
81 FreeAndNil(Enemies);
82 inherited;
83 end;
84
85 procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean);
86 begin
87 inherited;
88 { This virtual method is executed every frame (many times per second). }
89 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
90 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
91 end;
92
93 function TViewPlay.Press(const Event: TInputPressRelease): Boolean;
94 var
95 HitEnemy: TEnemy;
96 begin
97 Result := inherited;
98 if Result then Exit; // allow the ancestor to handle keys
99
100 { This virtual method is executed when user presses
101 a key, a mouse button, or touches a touch-screen.
102
103 Note that each UI control has also events like OnPress and OnClick.
104 These events can be used to handle the "press", if it should do something
105 specific when used in that UI control.
106 The TViewPlay.Press method should be used to handle keys
107 not handled in children controls.
108 }
109
110 if Event.IsMouseButton(buttonLeft) then
111 begin
112 SoundEngine.Play(NamedSound('Shoot'));
113
114 { We clicked on enemy if
115 - TransformUnderMouse indicates we hit something
116 - It has a behavior of TEnemy. }
117 if (MainViewport.TransformUnderMouse <> nil) and
118 (MainViewport.TransformUnderMouse.FindBehavior(TEnemy) <> nil) then
119 begin
120 HitEnemy := MainViewport.TransformUnderMouse.FindBehavior(TEnemy) as TEnemy;
121 HitEnemy.Hurt;
122 end;
123
124 Exit(true);
125 end;
126
127 if Event.IsMouseButton(buttonRight) then
128 begin
129 WalkNavigation.MouseLook := not WalkNavigation.MouseLook;
130
131 { This game allows to toggle WalkNavigation.MouseLook at run-time,
132 which changes:
133 - how can user rotate the camera (with mouse look -- just move the mouse
134 around),
135 - whether the mouse cursor is visible (mouse cursor is hidden when
136 "mouse look" is active, as we internally reposition it to ~roughly
137 the middle of the viewport all the time),
138 - what the MainViewport.TransformUnderMouse queries
139 (with mouse look -- it always makes a raycast from the exact middle of
140 the viewport).
141
142 The crosshair, at the middle of the screen, is thus useful
143 (and even necessary) only when "mouse look" is active.
144
145 It real games, it is usually simpler, because you use "mouse look"
146 always, or not at all.
147 In this demo, we wanted to have both options. }
148 SimpleCrosshair.Exists := WalkNavigation.MouseLook;
149
150 Exit(true);
151 end;
152
153 if Event.IsKey(keyF5) then
154 begin
155 Container.SaveScreenToDefaultFile;
156 Exit(true);
157 end;
158
159 if Event.IsKey(keyEscape) then
160 begin
161 Container.View := ViewMenu;
162 Exit(true);
163 end;
164 end;
165
166 end.