| 1 |
{ |
| 2 |
Copyright 2003-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 view, where most of the application logic takes place. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, Contnrs, |
| 22 |
CastleComponentSerialize, CastleUIControls, CastleControls, |
| 23 |
CastleKeysMouse, CastleTransform, CastleSoundEngine, CastleScene, CastleCameras, |
| 24 |
CastleViewport, CastleVectors, CastleBehaviors; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main view, where most of the application logic takes place. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
LabelFps: TCastleLabel; |
| 33 |
SoundKaboom, SoundRatSqueak: TCastleSound; |
| 34 |
HelpMessage: TCastleLabel; |
| 35 |
MuteImage: TCastleImageControl; |
| 36 |
CrosshairForMouseLook: TCastleCrosshair; |
| 37 |
TimerSpawnTnts: TCastleTimer; |
| 38 |
Viewport: TCastleViewport; |
| 39 |
Navigation: TCastleWalkNavigation; |
| 40 |
Rat: TCastleScene; |
| 41 |
SoundSourceRat: TCastleSoundSource; |
| 42 |
SceneLevel: TCastleScene; |
| 43 |
private |
| 44 |
const |
| 45 |
{ Max number of TNT items. } |
| 46 |
MaxTntsCount = 40; |
| 47 |
InitialTntsCount = MaxTntsCount; |
| 48 |
|
| 49 |
var |
| 50 |
RatAngle: Single; |
| 51 |
TntFactory: TCastleComponentFactory; |
| 52 |
Tnts: TComponentList; |
| 53 |
|
| 54 |
procedure NewTnt(const Y: Single); |
| 55 |
{ Update Rat.Translation based on RatAngle } |
| 56 |
procedure UpdateRatPosition; |
| 57 |
procedure DoTimerSpawnTnts(Sender: TObject); |
| 58 |
public |
| 59 |
constructor Create(AOwner: TComponent); override; |
| 60 |
procedure Start; override; |
| 61 |
procedure Stop; override; |
| 62 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 63 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 64 |
end; |
| 65 |
|
| 66 |
var |
| 67 |
ViewMain: TViewMain; |
| 68 |
|
| 69 |
implementation |
| 70 |
|
| 71 |
uses SysUtils, |
| 72 |
CastleUtils, CastleBoxes, CastleWindow, CastleSceneCore; |
| 73 |
|
| 74 |
{ TViewMain ----------------------------------------------------------------- } |
| 75 |
|
| 76 |
constructor TViewMain.Create(AOwner: TComponent); |
| 77 |
begin |
| 78 |
inherited; |
| 79 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 80 |
end; |
| 81 |
|
| 82 |
procedure TViewMain.Start; |
| 83 |
begin |
| 84 |
inherited; |
| 85 |
|
| 86 |
{ initialize Rat } |
| 87 |
UpdateRatPosition; |
| 88 |
|
| 89 |
{ initialize Tnt } |
| 90 |
Tnts := TComponentList.Create(false); |
| 91 |
|
| 92 |
TntFactory := TCastleComponentFactory.Create(FreeAtStop); |
| 93 |
TntFactory.Url := 'castle-data:/extra_objects/tnt_final.castle-transform'; |
| 94 |
while Tnts.Count < InitialTntsCount do |
| 95 |
NewTnt(0.0); |
| 96 |
|
| 97 |
TimerSpawnTnts.OnTimer := {$ifdef FPC}@{$endif}DoTimerSpawnTnts; |
| 98 |
end; |
| 99 |
|
| 100 |
procedure TViewMain.Stop; |
| 101 |
begin |
| 102 |
FreeAndNil(Tnts); |
| 103 |
inherited; |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TViewMain.NewTnt(const Y: Single); |
| 107 |
var |
| 108 |
TntExtent: Single; |
| 109 |
Tnt: TCastleTransform; |
| 110 |
LevelBox: TBox3D; |
| 111 |
begin |
| 112 |
Tnt := TntFactory.TransformLoad(FreeAtStop); |
| 113 |
TntExtent := Tnt.BoundingBox.MaxSize / 2; |
| 114 |
LevelBox := SceneLevel.BoundingBox; |
| 115 |
Tnt.Translation := Vector3( |
| 116 |
RandomFloatRange(LevelBox.Data[0].X + TntExtent, LevelBox.Data[1].X - TntExtent), |
| 117 |
Y + TntExtent, |
| 118 |
RandomFloatRange(LevelBox.Data[0].Z + TntExtent, LevelBox.Data[1].Z - TntExtent)); |
| 119 |
Viewport.Items.Add(Tnt); |
| 120 |
Tnts.Add(Tnt); |
| 121 |
end; |
| 122 |
|
| 123 |
procedure TViewMain.UpdateRatPosition; |
| 124 |
const |
| 125 |
RatCircleMiddle: TVector3 = (X: 0; Y: 0; Z: 0); |
| 126 |
RatCircleRadius = 3; |
| 127 |
var |
| 128 |
T: TVector3; |
| 129 |
begin |
| 130 |
T := RatCircleMiddle; |
| 131 |
T.X := T.X + (Cos(RatAngle) * RatCircleRadius); |
| 132 |
T.Z := T.Z + (Sin(RatAngle) * RatCircleRadius); |
| 133 |
Rat.Translation := T; |
| 134 |
end; |
| 135 |
|
| 136 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 137 |
|
| 138 |
function CylinderContains(const P: TVector3; |
| 139 |
const MiddleX, MiddleZ, Radius, MinY, MaxY: Single): boolean; |
| 140 |
begin |
| 141 |
Result := |
| 142 |
(Sqr(P.X-MiddleX) + Sqr(P.Z-MiddleZ) <= Sqr(Radius)) and |
| 143 |
(MinY <= P.Y) and (P.Y <= MaxY); |
| 144 |
end; |
| 145 |
|
| 146 |
var |
| 147 |
InMuteArea: boolean; |
| 148 |
begin |
| 149 |
inherited; |
| 150 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 151 |
|
| 152 |
{ update rat } |
| 153 |
RatAngle := RatAngle + 0.5 * SecondsPassed; |
| 154 |
UpdateRatPosition; |
| 155 |
|
| 156 |
{ update "mute area" } |
| 157 |
InMuteArea := CylinderContains(Viewport.Camera.WorldTranslation, 2, 0, 0.76, 0, 1.045640); |
| 158 |
if MuteImage <> nil then |
| 159 |
MuteImage.Exists := InMuteArea; |
| 160 |
if InMuteArea then |
| 161 |
SoundEngine.Volume := 0 |
| 162 |
else |
| 163 |
SoundEngine.Volume := 1; |
| 164 |
end; |
| 165 |
|
| 166 |
procedure TViewMain.DoTimerSpawnTnts(Sender: TObject); |
| 167 |
begin |
| 168 |
while Tnts.Count < MaxTntsCount do |
| 169 |
NewTnt(3.0); |
| 170 |
end; |
| 171 |
|
| 172 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 173 |
|
| 174 |
procedure TntHit(TntTransform: TCastleTransform); |
| 175 |
var |
| 176 |
TntSoundSource: TCastleSoundSource; |
| 177 |
SoundKaboomPlaying: TCastlePlayingSoundSource; |
| 178 |
begin |
| 179 |
TntSoundSource := TntTransform.FindBehavior(TCastleSoundSource) as TCastleSoundSource; |
| 180 |
|
| 181 |
{ Create TCastlePlayingSoundSource instance, because we want to adjust Follow |
| 182 |
from default true to false. |
| 183 |
This way sound will continue playing regardless if the TCastleSoundSource |
| 184 |
will be destroyed. } |
| 185 |
SoundKaboomPlaying := TCastlePlayingSoundSource.Create(FreeAtStop); |
| 186 |
SoundKaboomPlaying.FreeOnStop := true; |
| 187 |
SoundKaboomPlaying.Follow := false; |
| 188 |
SoundKaboomPlaying.Sound := SoundKaboom; |
| 189 |
TntSoundSource.Play(SoundKaboomPlaying); |
| 190 |
|
| 191 |
if PointsDistanceSqr(TntTransform.Translation, Rat.Translation) < 1.0 then |
| 192 |
SoundSourceRat.Play(SoundRatSqueak); |
| 193 |
|
| 194 |
FreeAndNil(TntTransform); // will automatically remove itself from Tnts list |
| 195 |
end; |
| 196 |
|
| 197 |
begin |
| 198 |
Result := inherited; |
| 199 |
if Result then Exit; // allow the ancestor to handle keys |
| 200 |
|
| 201 |
if Event.IsMouseButton(buttonLeft) then |
| 202 |
begin |
| 203 |
{ Detect clicks on TNT scene. |
| 204 |
We look at TransformUnderMouse.Parent, because that is how tnt_final.castle-transform |
| 205 |
was designed: we will detect clicks on SceneTntBox, which is the child of the root |
| 206 |
of tnt_final.castle-transform . } |
| 207 |
if (Viewport.TransformUnderMouse <> nil) and |
| 208 |
(Viewport.TransformUnderMouse.Parent <> nil) and |
| 209 |
(Tnts.IndexOf(Viewport.TransformUnderMouse.Parent) <> -1) then |
| 210 |
TntHit(Viewport.TransformUnderMouse.Parent); |
| 211 |
Exit(true); |
| 212 |
end; |
| 213 |
|
| 214 |
if Event.IsMouseButton(buttonRight) then |
| 215 |
begin |
| 216 |
Navigation.MouseLook := not Navigation.MouseLook; |
| 217 |
// crosshair makes sense only with mouse look |
| 218 |
CrosshairForMouseLook.Exists := Navigation.MouseLook; |
| 219 |
Exit(true); |
| 220 |
end; |
| 221 |
|
| 222 |
if Event.IsKey(keyF1) then |
| 223 |
begin |
| 224 |
HelpMessage.Exists := not HelpMessage.Exists; |
| 225 |
Exit(true); |
| 226 |
end; |
| 227 |
|
| 228 |
if Event.IsKey(keyF5) then |
| 229 |
begin |
| 230 |
Container.SaveScreenToDefaultFile; |
| 231 |
Exit(true); |
| 232 |
end; |
| 233 |
|
| 234 |
if Event.IsKey(keyEscape) then |
| 235 |
begin |
| 236 |
Application.Terminate; |
| 237 |
Exit(true); |
| 238 |
end; |
| 239 |
end; |
| 240 |
|
| 241 |
end. |