| 1 |
{ |
| 2 |
Copyright 2022-2022 Michalis Kamburelis, Andrzej Kilijański. |
| 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, CastleTransform; |
| 24 |
|
| 25 |
type |
| 26 |
{ Main view, where most of the application logic takes place. } |
| 27 |
TViewMain = class(TCastleView) |
| 28 |
published |
| 29 |
{ Components designed using CGE editor. |
| 30 |
These fields will be automatically initialized at Start. } |
| 31 |
LabelFps: TCastleLabel; |
| 32 |
SpherePlayer: TCastleTransform; |
| 33 |
SphereDynamic: TCastleTransform; |
| 34 |
ButtonMoveByTranslate: TCastleButton; |
| 35 |
ButtonMoveByVelocity: TCastleButton; |
| 36 |
ButtonMoveByAnimateTranslation: TCastleButton; |
| 37 |
CheckboxContinuousCollisionDetection: TCastleCheckbox; |
| 38 |
CheckboxContinuousCollisionDetectionSphereDynamic: TCastleCheckbox; |
| 39 |
ButtonBodyAnimated: TCastleButton; |
| 40 |
ButtonBodyDynamic: TCastleButton; |
| 41 |
PlayerBody, TestBody: TCastleRigidBody; |
| 42 |
private |
| 43 |
procedure ClickMoveByTranslate(Sender: TObject); |
| 44 |
procedure ClickMoveByVelocity(Sender: TObject); |
| 45 |
procedure ClickMoveByAnimateTranslation(Sender: TObject); |
| 46 |
|
| 47 |
procedure CheckboxContinuousCollisionDetectionChange(Sender: TObject); |
| 48 |
procedure CheckboxContinuousCollisionDetectionSphereDynamicChange(Sender: TObject); |
| 49 |
|
| 50 |
procedure ClickBodyDynamic(Sender: TObject); |
| 51 |
procedure ClickBodyAnimated(Sender: TObject); |
| 52 |
public |
| 53 |
constructor Create(AOwner: TComponent); override; |
| 54 |
procedure Start; override; |
| 55 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 56 |
end; |
| 57 |
|
| 58 |
var |
| 59 |
ViewMain: TViewMain; |
| 60 |
|
| 61 |
implementation |
| 62 |
|
| 63 |
uses SysUtils, CastleLog; |
| 64 |
|
| 65 |
{ TViewMain ----------------------------------------------------------------- } |
| 66 |
|
| 67 |
constructor TViewMain.Create(AOwner: TComponent); |
| 68 |
begin |
| 69 |
inherited; |
| 70 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 71 |
end; |
| 72 |
|
| 73 |
procedure TViewMain.Start; |
| 74 |
begin |
| 75 |
inherited; |
| 76 |
|
| 77 |
ButtonMoveByTranslate.OnClick := {$ifdef FPC}@{$endif}ClickMoveByTranslate; |
| 78 |
ButtonMoveByVelocity.OnClick := {$ifdef FPC}@{$endif}ClickMoveByVelocity; |
| 79 |
ButtonMoveByAnimateTranslation.OnClick := {$ifdef FPC}@{$endif}ClickMoveByAnimateTranslation; |
| 80 |
|
| 81 |
CheckboxContinuousCollisionDetection.OnChange := {$ifdef FPC}@{$endif}CheckboxContinuousCollisionDetectionChange; |
| 82 |
CheckboxContinuousCollisionDetectionSphereDynamic.OnChange := {$ifdef FPC}@{$endif}CheckboxContinuousCollisionDetectionSphereDynamicChange; |
| 83 |
|
| 84 |
ButtonBodyDynamic.OnClick := {$ifdef FPC}@{$endif}ClickBodyDynamic; |
| 85 |
ButtonBodyAnimated.OnClick := {$ifdef FPC}@{$endif}ClickBodyAnimated; |
| 86 |
|
| 87 |
// initialize UI and state consistently |
| 88 |
ClickBodyAnimated(nil); |
| 89 |
ClickMoveByTranslate(nil); |
| 90 |
end; |
| 91 |
|
| 92 |
procedure TViewMain.ClickMoveByTranslate(Sender: TObject); |
| 93 |
begin |
| 94 |
ButtonMoveByTranslate.Pressed := true; |
| 95 |
ButtonMoveByAnimateTranslation.Pressed := false; |
| 96 |
ButtonMoveByVelocity.Pressed := false; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TViewMain.ClickMoveByVelocity(Sender: TObject); |
| 100 |
begin |
| 101 |
ButtonMoveByVelocity.Pressed := true; |
| 102 |
ButtonMoveByAnimateTranslation.Pressed := false; |
| 103 |
ButtonMoveByTranslate.Pressed := false; |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TViewMain.ClickMoveByAnimateTranslation(Sender: TObject); |
| 107 |
begin |
| 108 |
ButtonMoveByAnimateTranslation.Pressed := true; |
| 109 |
ButtonMoveByTranslate.Pressed := false; |
| 110 |
ButtonMoveByVelocity.Pressed := false; |
| 111 |
end; |
| 112 |
|
| 113 |
procedure TViewMain.CheckboxContinuousCollisionDetectionChange(Sender: TObject); |
| 114 |
begin |
| 115 |
if CheckboxContinuousCollisionDetection.Checked then |
| 116 |
PlayerBody.CollisionDetection := cdContinuous |
| 117 |
else |
| 118 |
PlayerBody.CollisionDetection := cdDiscrete; |
| 119 |
end; |
| 120 |
|
| 121 |
procedure TViewMain.CheckboxContinuousCollisionDetectionSphereDynamicChange(Sender: TObject); |
| 122 |
begin |
| 123 |
if CheckboxContinuousCollisionDetectionSphereDynamic.Checked then |
| 124 |
TestBody.CollisionDetection := cdContinuous |
| 125 |
else |
| 126 |
TestBody.CollisionDetection := cdDiscrete; |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TViewMain.ClickBodyDynamic(Sender: TObject); |
| 130 |
begin |
| 131 |
ButtonBodyAnimated.Pressed := false; |
| 132 |
ButtonBodyDynamic.Pressed := true; |
| 133 |
|
| 134 |
PlayerBody.Animated := false; |
| 135 |
PlayerBody.Dynamic := true; |
| 136 |
end; |
| 137 |
|
| 138 |
procedure TViewMain.ClickBodyAnimated(Sender: TObject); |
| 139 |
begin |
| 140 |
ButtonBodyAnimated.Pressed := true; |
| 141 |
ButtonBodyDynamic.Pressed := false; |
| 142 |
|
| 143 |
PlayerBody.Animated := true; |
| 144 |
PlayerBody.Dynamic := false; |
| 145 |
end; |
| 146 |
|
| 147 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 148 |
|
| 149 |
procedure MovePlayer(const X, Y: Single); |
| 150 |
const |
| 151 |
PlayerSpeed = 500; |
| 152 |
var |
| 153 |
V: TVector2; |
| 154 |
begin |
| 155 |
V := Vector2(X, Y) * PlayerSpeed * SecondsPassed; |
| 156 |
if ButtonMoveByTranslate.Pressed then |
| 157 |
SpherePlayer.Translate(Vector3(V, 0)) |
| 158 |
else |
| 159 |
if ButtonMoveByVelocity.Pressed then |
| 160 |
PlayerBody.LinearVelocity := PlayerBody.LinearVelocity + Vector3(V, 0) |
| 161 |
//else |
| 162 |
//if CheckboxMoveByAnimateTranslation.Checked then |
| 163 |
// |
| 164 |
end; |
| 165 |
|
| 166 |
begin |
| 167 |
inherited; |
| 168 |
{ This virtual method is executed every frame (many times per second). } |
| 169 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 170 |
|
| 171 |
if Container.Pressed.Items[keyA] or Container.Pressed.Items[keyArrowLeft] then |
| 172 |
MovePlayer(-1, 0) |
| 173 |
else |
| 174 |
if Container.Pressed.Items[keyD] or Container.Pressed.Items[keyArrowRight] then |
| 175 |
MovePlayer(1, 0) |
| 176 |
else |
| 177 |
if Container.Pressed.Items[keyW] or Container.Pressed.Items[keyArrowUp] then |
| 178 |
MovePlayer(0, 1) |
| 179 |
else |
| 180 |
if Container.Pressed.Items[keyS] or Container.Pressed.Items[keyArrowDown] then |
| 181 |
MovePlayer(0, -1) |
| 182 |
else |
| 183 |
PlayerBody.LinearVelocity := Vector3(0, 0, 0); |
| 184 |
end; |
| 185 |
|
| 186 |
end. |