| 1 |
{ |
| 2 |
Copyright 2022-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 |
{ Playing the game. } |
| 17 |
unit GameViewPlay; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, CastleCameras, |
| 22 |
CastleVectors, CastleUIControls, CastleControls, CastleKeysMouse, |
| 23 |
CastleViewport, CastleSceneCore, X3DNodes, CastleScene, CastleSoundEngine, |
| 24 |
CastleBehaviors, CastleNotifications, CastleTransform, |
| 25 |
GameEnemy; |
| 26 |
|
| 27 |
type |
| 28 |
TViewPlay = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
WalkNavigation: TCastleWalkNavigation; |
| 33 |
MainViewport: TCastleViewport; |
| 34 |
SceneGun: TCastleScene; |
| 35 |
SoundSourceFootsteps: TCastleSoundSource; |
| 36 |
SoundShoot: TCastleSound; |
| 37 |
BoxDieDetect, BoxWinDetect: TCastleTransform; |
| 38 |
DesignHud: TCastleDesign; |
| 39 |
MapCamera: TCastleCamera; |
| 40 |
private |
| 41 |
PersistentMouseLook: Boolean; |
| 42 |
Enemies: TEnemyList; |
| 43 |
|
| 44 |
{ components in DesignHud } |
| 45 |
LabelFps: TCastleLabel; |
| 46 |
MainNotifications: TCastleNotifications; |
| 47 |
MapViewport: TCastleViewport; |
| 48 |
|
| 49 |
procedure UpdateMouseLook; |
| 50 |
procedure WeaponShootAnimationStop(const Scene: TCastleSceneCore; |
| 51 |
const Animation: TTimeSensorNode); |
| 52 |
public |
| 53 |
constructor Create(AOwner: TComponent); override; |
| 54 |
procedure Start; override; |
| 55 |
procedure Stop; override; |
| 56 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 57 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 58 |
end; |
| 59 |
|
| 60 |
var |
| 61 |
ViewPlay: TViewPlay; |
| 62 |
|
| 63 |
implementation |
| 64 |
|
| 65 |
uses SysUtils, Math, |
| 66 |
CastleComponentSerialize, CastleLog, |
| 67 |
GameViewWin, GameViewDeath, GameViewOptions; |
| 68 |
|
| 69 |
constructor TViewPlay.Create(AOwner: TComponent); |
| 70 |
begin |
| 71 |
inherited; |
| 72 |
DesignUrl := 'castle-data:/gameviewplay.castle-user-interface'; |
| 73 |
end; |
| 74 |
|
| 75 |
procedure TViewPlay.Start; |
| 76 |
var |
| 77 |
SceneName: String; |
| 78 |
SceneEnemy: TCastleScene; |
| 79 |
Enemy: TEnemy; |
| 80 |
I: Integer; |
| 81 |
begin |
| 82 |
inherited; |
| 83 |
|
| 84 |
{ initialize components in DesignHud } |
| 85 |
LabelFps := DesignHud.DesignedComponent('LabelFps') as TCastleLabel; |
| 86 |
MainNotifications := DesignHud.DesignedComponent('MainNotifications') as TCastleNotifications; |
| 87 |
MapViewport := DesignHud.DesignedComponent('MapViewport') as TCastleViewport; |
| 88 |
|
| 89 |
Enemies := TEnemyList.Create(true); |
| 90 |
|
| 91 |
for I := 1 to 7 do |
| 92 |
begin |
| 93 |
SceneName := 'SceneKnight' + IntToStr(I); |
| 94 |
try |
| 95 |
SceneEnemy := DesignedComponent(SceneName) as TCastleScene; |
| 96 |
Enemy := TEnemy.Create(FreeAtStop); |
| 97 |
SceneEnemy.AddBehavior(Enemy); |
| 98 |
except |
| 99 |
on EComponentNotFound do |
| 100 |
WritelnWarning('Cannot find "%s"', [SceneName]); |
| 101 |
end; |
| 102 |
end; |
| 103 |
|
| 104 |
MapViewport.Items := MainViewport.Items; |
| 105 |
MapViewport.Camera := MapCamera; |
| 106 |
|
| 107 |
PersistentMouseLook := true; |
| 108 |
end; |
| 109 |
|
| 110 |
procedure TViewPlay.Stop; |
| 111 |
begin |
| 112 |
FreeAndNil(Enemies); |
| 113 |
inherited; |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 117 |
var |
| 118 |
DirectionHorizontal: TVector3; |
| 119 |
GameActive: Boolean; |
| 120 |
begin |
| 121 |
inherited; |
| 122 |
LabelFps.Caption := Container.Fps.ToString; |
| 123 |
UpdateMouseLook; |
| 124 |
|
| 125 |
GameActive := Container.FrontView = Self; |
| 126 |
|
| 127 |
SoundSourceFootsteps.Volume := IfThen(WalkNavigation.IsWalkingOnTheGround and GameActive, 1, 0); |
| 128 |
|
| 129 |
MainViewport.Items.Paused := not GameActive; |
| 130 |
|
| 131 |
if GameActive then |
| 132 |
begin |
| 133 |
DirectionHorizontal := MainViewport.Camera.Direction; |
| 134 |
if not VectorsParallel(DirectionHorizontal, MainViewport.Camera.GravityUp) then |
| 135 |
MakeVectorsOrthoOnTheirPlane(DirectionHorizontal, MainViewport.Camera.GravityUp); |
| 136 |
|
| 137 |
MapViewport.Camera.SetView( |
| 138 |
MainViewport.Camera.WorldTranslation + Vector3(0, 30, 0), |
| 139 |
Vector3(0, -1, 0), |
| 140 |
DirectionHorizontal |
| 141 |
); |
| 142 |
|
| 143 |
if BoxWinDetect.WorldBoundingBox.Contains(MainViewport.Camera.WorldTranslation) then |
| 144 |
begin |
| 145 |
Container.PushView(ViewWin); |
| 146 |
Exit; |
| 147 |
end; |
| 148 |
|
| 149 |
if BoxDieDetect.WorldBoundingBox.Contains(MainViewport.Camera.WorldTranslation) then |
| 150 |
begin |
| 151 |
Container.PushView(ViewDeath); |
| 152 |
Exit; |
| 153 |
end; |
| 154 |
end; |
| 155 |
end; |
| 156 |
|
| 157 |
procedure TViewPlay.UpdateMouseLook; |
| 158 |
begin |
| 159 |
WalkNavigation.MouseLook := (Container.FrontView = Self) and |
| 160 |
( PersistentMouseLook or |
| 161 |
(buttonRight in Container.MousePressed) ); |
| 162 |
end; |
| 163 |
|
| 164 |
procedure TViewPlay.WeaponShootAnimationStop(const Scene: TCastleSceneCore; |
| 165 |
const Animation: TTimeSensorNode); |
| 166 |
begin |
| 167 |
Scene.PlayAnimation('idle', true); |
| 168 |
end; |
| 169 |
|
| 170 |
function TViewPlay.Press(const Event: TInputPressRelease): Boolean; |
| 171 |
var |
| 172 |
HitEnemy: TEnemy; |
| 173 |
PlayAnimationParams: TPlayAnimationParameters; |
| 174 |
begin |
| 175 |
Result := inherited; |
| 176 |
if Result then Exit; // allow the ancestor to handle keys |
| 177 |
|
| 178 |
if Container.FrontView = Self then |
| 179 |
begin |
| 180 |
if Event.IsKey(keyF4) then |
| 181 |
begin |
| 182 |
PersistentMouseLook := not PersistentMouseLook; |
| 183 |
UpdateMouseLook; |
| 184 |
Exit(true); |
| 185 |
end; |
| 186 |
|
| 187 |
if Event.IsMouseButton(buttonLeft) then |
| 188 |
begin |
| 189 |
if MainViewport.TransformUnderMouse <> nil then |
| 190 |
WritelnLog('Clicked on ' + MainViewport.TransformUnderMouse.Name); |
| 191 |
|
| 192 |
PlayAnimationParams := TPlayAnimationParameters.Create; |
| 193 |
try |
| 194 |
PlayAnimationParams.Name := 'primary'; |
| 195 |
PlayAnimationParams.StopNotification := {$ifdef FPC}@{$endif} WeaponShootAnimationStop; |
| 196 |
PlayAnimationParams.Loop := false; |
| 197 |
SceneGun.PlayAnimation(PlayAnimationParams); |
| 198 |
finally FreeAndNil(PlayAnimationParams) end; |
| 199 |
|
| 200 |
SoundEngine.Play(SoundShoot); |
| 201 |
|
| 202 |
{ We clicked on enemy if |
| 203 |
- TransformUnderMouse indicates we hit something |
| 204 |
- It has a behavior of TEnemy. } |
| 205 |
if (MainViewport.TransformUnderMouse <> nil) and |
| 206 |
(MainViewport.TransformUnderMouse.FindBehavior(TEnemy) <> nil) then |
| 207 |
begin |
| 208 |
HitEnemy := MainViewport.TransformUnderMouse.FindBehavior(TEnemy) as TEnemy; |
| 209 |
HitEnemy.Hurt; |
| 210 |
MainNotifications.Show('Killed ' + HitEnemy.Parent.Name); |
| 211 |
end; |
| 212 |
|
| 213 |
Exit(true); |
| 214 |
end; |
| 215 |
|
| 216 |
if Event.IsKey(keyEscape) then |
| 217 |
begin |
| 218 |
ViewOptions.OverGame := true; |
| 219 |
Container.PushView(ViewOptions); |
| 220 |
Exit(true); |
| 221 |
end; |
| 222 |
|
| 223 |
if Event.IsKey(keyP) then |
| 224 |
begin |
| 225 |
Container.PushView(ViewWin); |
| 226 |
Exit(true); |
| 227 |
end; |
| 228 |
|
| 229 |
if Event.IsKey(keyO) then |
| 230 |
begin |
| 231 |
Container.PushView(ViewDeath); |
| 232 |
Exit(true); |
| 233 |
end; |
| 234 |
end; |
| 235 |
|
| 236 |
if Event.IsKey(keyF5) then |
| 237 |
begin |
| 238 |
MainNotifications.Show('Saved screenshot to ' + Container.SaveScreenToDefaultFile); |
| 239 |
Exit(true); |
| 240 |
end; |
| 241 |
end; |
| 242 |
|
| 243 |
end. |