Debian Codegraph @

examples/creature_behaviors/code/gameviewplay.pas

raw
1 {
2 Copyright 2020-2024 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, CastleBehaviors, CastleClassUtils;
25
26 type
27 { Main "playing game" view, where most of the game logic takes place. }
28 TViewPlay = class(TCastleView)
29 published
30 { Components designed using CGE editor.
31 These fields will be automatically initialized at Start. }
32 LabelFps: TCastleLabel;
33 MainViewport: TCastleViewport;
34 WalkNavigation: TCastleWalkNavigation;
35 private
36 Enemies: TCastleTransformList;
37 PlayerAlive: TCastleAliveBehavior;
38 public
39 constructor Create(AOwner: TComponent); override;
40 procedure Start; override;
41 procedure Stop; override;
42 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
43 function Press(const Event: TInputPressRelease): Boolean; override;
44 end;
45
46 var
47 ViewPlay: TViewPlay;
48
49 implementation
50
51 uses SysUtils, Math,
52 CastleSoundEngine, CastleLog, CastleStringUtils, CastleFilesUtils,
53 GameViewMenu, GameSounds;
54
55 { TViewPlay ----------------------------------------------------------------- }
56
57 constructor TViewPlay.Create(AOwner: TComponent);
58 begin
59 inherited;
60 DesignUrl := 'castle-data:/gameviewplay.castle-user-interface';
61 end;
62
63 procedure TViewPlay.Start;
64
65 procedure AddEnemy(const EnemyScene: TCastleScene);
66 // TODO MoveAttackBehavior: TCastleMoveAttack;
67 begin
68 Enemies.Add(EnemyScene);
69
70 // TODO: TCastleMoveAttack should take care of this
71 EnemyScene.PlayAnimation('walk', true);
72
73 EnemyScene.AddBehavior(TCastleAliveBehavior.Create(FreeAtStop));
74
75 // TODO
76 // MoveAttackBehavior := TCastleMoveAttack.Create(FreeAtStop);
77 // MoveAttackBehavior.Enemy := PlayerAlive;
78 // EnemyScene.AddBehavior(MoveAttackBehavior);
79 end;
80
81 var
82 I: Integer;
83 begin
84 inherited;
85
86 PlayerAlive := TCastleAliveBehavior.Create(FreeAtStop);
87 MainViewport.Camera.AddBehavior(PlayerAlive);
88
89 { Initialize Enemies }
90 Enemies := TCastleTransformList.Create(false);
91 for I := 1 to 4 do
92 AddEnemy(DesignedComponent('SceneSoldier' + IntToStr(I)) as TCastleScene);
93 for I := 1 to 3 do
94 AddEnemy(DesignedComponent('SceneWolf' + IntToStr(I)) as TCastleScene);
95 end;
96
97 procedure TViewPlay.Stop;
98 begin
99 FreeAndNil(Enemies);
100 inherited;
101 end;
102
103 procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean);
104 begin
105 inherited;
106 { This virtual method is executed every frame (many times per second). }
107 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
108 WalkNavigation.MouseLook := buttonRight in Container.MousePressed;
109 end;
110
111 function TViewPlay.Press(const Event: TInputPressRelease): Boolean;
112 var
113 HitAlive: TCastleAliveBehavior;
114 EnemyScene: TCastleScene;
115 EnemySoundSource: TCastleSoundSource;
116 begin
117 Result := inherited;
118 if Result then Exit; // allow the ancestor to handle keys
119
120 { This virtual method is executed when user presses
121 a key, a mouse button, or touches a touch-screen.
122
123 Note that each UI control has also events like OnPress and OnClick.
124 These events can be used to handle the "press", if it should do something
125 specific when used in that UI control.
126 The TViewPlay.Press method should be used to handle keys
127 not handled in children controls.
128 }
129
130 if Event.IsMouseButton(buttonLeft) then
131 begin
132 SoundEngine.Play(SoundShoot);
133
134 { We clicked on enemy if
135 - TransformUnderMouse indicates we hit something
136 - It has a behavior of TCastleAliveBehavior. }
137 if (MainViewport.TransformUnderMouse <> nil) and
138 (MainViewport.TransformUnderMouse.FindBehavior(TCastleAliveBehavior) <> nil) then
139 begin
140 // TODO: TCastleMoveAttack should take care of this
141 HitAlive := MainViewport.TransformUnderMouse.FindBehavior(TCastleAliveBehavior) as TCastleAliveBehavior;
142 HitAlive.Hurt(1000, MainViewport.Camera.Direction);
143 if HitAlive.Dead then
144 begin
145 EnemyScene := HitAlive.Parent as TCastleScene;
146
147 // play die animation
148 if EnemyScene.HasAnimation('die') then
149 EnemyScene.PlayAnimation('die', false);
150 if EnemyScene.HasAnimation('sit') then // wolf has 'sit', not 'die'
151 EnemyScene.PlayAnimation('sit', false);
152
153 // stop wolf howling
154 EnemySoundSource := EnemyScene.FindBehavior(TCastleSoundSource) as TCastleSoundSource;
155 if EnemySoundSource <> nil then
156 EnemySoundSource.Sound := nil;
157
158 // dead corpse no longer collides
159 EnemyScene.Pickable := false;
160 EnemyScene.Collides := false;
161 end;
162 end;
163
164 Exit(true);
165 end;
166
167 if Event.IsKey(keyF5) then
168 begin
169 Container.SaveScreenToDefaultFile;
170 Exit(true);
171 end;
172
173 if Event.IsKey(keyEscape) then
174 begin
175 Container.View := ViewMenu;
176 Exit(true);
177 end;
178 end;
179
180 end.