| 1 |
{ |
| 2 |
Copyright 2022-2022 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, |
| 22 |
CastleVectors, CastleComponentSerialize, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleTerrain, |
| 24 |
CastleCameras, CastleScene, CastleViewport, CastleTransform, CastleSoundEngine; |
| 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 |
WalkNavigation1: TCastleWalkNavigation; |
| 34 |
Viewport1: TCastleViewport; |
| 35 |
SceneDetonate, SceneDropMore: TCastleScene; |
| 36 |
SoundDetonate, SoundClick: TCastleSound; |
| 37 |
Boxes: TCastleTransform; |
| 38 |
SceneExplosionCenter: TCastleTransform; |
| 39 |
public |
| 40 |
constructor Create(AOwner: TComponent); override; |
| 41 |
procedure Start; 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 |
ViewMain: TViewMain; |
| 48 |
|
| 49 |
implementation |
| 50 |
|
| 51 |
uses SysUtils, Math; |
| 52 |
|
| 53 |
{ TViewMain ----------------------------------------------------------------- } |
| 54 |
|
| 55 |
constructor TViewMain.Create(AOwner: TComponent); |
| 56 |
begin |
| 57 |
inherited; |
| 58 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 59 |
end; |
| 60 |
|
| 61 |
procedure TViewMain.Start; |
| 62 |
begin |
| 63 |
inherited; |
| 64 |
end; |
| 65 |
|
| 66 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 67 |
begin |
| 68 |
inherited; |
| 69 |
{ This virtual method is executed every frame (many times per second). } |
| 70 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 71 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 72 |
end; |
| 73 |
|
| 74 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 75 |
const |
| 76 |
ExplosionStrength = 10; |
| 77 |
UpwardExplosion = 2; // makes sure we have some force upward, this just looks more impressive |
| 78 |
var |
| 79 |
NewBoxes: TCastleTransform; |
| 80 |
RBody: TCastleRigidBody; |
| 81 |
ExplosionCenter, ForceDir: TVector3; |
| 82 |
AllRigidBodies: TCastleBehaviorList; |
| 83 |
RBodyBehavior: TCastleBehavior; |
| 84 |
begin |
| 85 |
Result := inherited; |
| 86 |
if Result then Exit; // allow the ancestor to handle keys |
| 87 |
|
| 88 |
if Event.IsMouseButton(buttonLeft) then |
| 89 |
begin |
| 90 |
if Viewport1.TransformUnderMouse = SceneDetonate then |
| 91 |
begin |
| 92 |
SceneDetonate.PlayAnimation('detonate', false); |
| 93 |
SoundEngine.Play(SoundDetonate); |
| 94 |
|
| 95 |
{ for all rigid bodies in Boxes, apply impulse } |
| 96 |
ExplosionCenter := SceneExplosionCenter.WorldTranslation; |
| 97 |
AllRigidBodies := Boxes.FindAllBehaviors(TCastleRigidBody); |
| 98 |
try |
| 99 |
for RBodyBehavior in AllRigidBodies do |
| 100 |
begin |
| 101 |
RBody := RBodyBehavior as TCastleRigidBody; |
| 102 |
ForceDir := RBody.Parent.WorldTranslation - ExplosionCenter; |
| 103 |
ForceDir.Y := Max(ForceDir.Y, UpwardExplosion); |
| 104 |
RBody.ApplyImpulse( |
| 105 |
ForceDir.AdjustToLength(ExplosionStrength), |
| 106 |
ExplosionCenter); |
| 107 |
end; |
| 108 |
finally FreeAndNil(AllRigidBodies) end; |
| 109 |
|
| 110 |
Exit(true); |
| 111 |
end; |
| 112 |
|
| 113 |
if Viewport1.TransformUnderMouse = SceneDropMore then |
| 114 |
begin |
| 115 |
SceneDropMore.PlayAnimation('click', false); |
| 116 |
SoundEngine.Play(SoundClick); |
| 117 |
|
| 118 |
{ instantiate boxes.castle-transform } |
| 119 |
NewBoxes := TransformLoad('castle-data:/boxes.castle-transform', FreeAtStop); |
| 120 |
Boxes.Add(NewBoxes); |
| 121 |
|
| 122 |
Exit(true); |
| 123 |
end; |
| 124 |
end; |
| 125 |
|
| 126 |
if Event.IsMouseButton(buttonRight) then |
| 127 |
begin |
| 128 |
WalkNavigation1.MouseLook := not WalkNavigation1.MouseLook; |
| 129 |
Exit(true); |
| 130 |
end; |
| 131 |
end; |
| 132 |
|
| 133 |
end. |