| 1 |
{ |
| 2 |
Copyright 2023-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, |
| 22 |
CastleVectors, CastleComponentSerialize, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleTransform, |
| 24 |
CastleViewport, CastleScene, 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, LabelDistance: TCastleLabel; |
| 33 |
MovingEnv, VerticalSizer: TCastleTransform; |
| 34 |
Car1, Car2: TCastlePlane; |
| 35 |
Car1CollideMarker, Car2CollideMarker: TCastleTransform; |
| 36 |
ButtonCar1Left, |
| 37 |
ButtonCar1Right, |
| 38 |
ButtonCar2Left, |
| 39 |
ButtonCar2Right, |
| 40 |
ButtonSpeed: TCastleButton; |
| 41 |
MainViewport: TCastleViewport; |
| 42 |
SoundCrash: TCastleSound; |
| 43 |
private |
| 44 |
Car1TargetX, Car2TargetX: Single; |
| 45 |
MovingEnvCopy: TCastleTransform; |
| 46 |
{ Traveled distance. } |
| 47 |
Distance: Double; |
| 48 |
{ When and how to move MoveEnv or MoveEnvCopy to keep the illusion |
| 49 |
of infinite street. } |
| 50 |
MovingEnvNextDistance: Double; |
| 51 |
MovingEnvNextTransform: TCastleTransform; |
| 52 |
StreetMoveSpeed: Single; |
| 53 |
procedure ClickCar1Left(Sender: TObject); |
| 54 |
procedure ClickCar1Right(Sender: TObject); |
| 55 |
procedure ClickCar2Left(Sender: TObject); |
| 56 |
procedure ClickCar2Right(Sender: TObject); |
| 57 |
procedure ButtonSpeedClick(Sender: TObject); |
| 58 |
{ X that positions the car exactly in the middle of the street column. |
| 59 |
Doesn't allow cars to move outside of the street (of 3 columns we have). } |
| 60 |
function CarNormalizeToColWidth(const X: Single): Single; |
| 61 |
public |
| 62 |
constructor Create(AOwner: TComponent); override; |
| 63 |
procedure Start; override; |
| 64 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 65 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 66 |
end; |
| 67 |
|
| 68 |
var |
| 69 |
ViewMain: TViewMain; |
| 70 |
|
| 71 |
implementation |
| 72 |
|
| 73 |
uses SysUtils, CastleUtils; |
| 74 |
|
| 75 |
const |
| 76 |
CarHorizontalMoveSpeed = 10.0; |
| 77 |
StreetColWidth = 175.0; |
| 78 |
StreetMoveSpeeds: array [ Boolean { fast? } ] of Single = (100.0, 1000.0); |
| 79 |
{ Each MovingEnv / MovingEnvCopy should be shifted by Y from each other. } |
| 80 |
MovingEnvSize = 2000.0; |
| 81 |
|
| 82 |
{ TViewMain ----------------------------------------------------------------- } |
| 83 |
|
| 84 |
constructor TViewMain.Create(AOwner: TComponent); |
| 85 |
begin |
| 86 |
inherited; |
| 87 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 88 |
end; |
| 89 |
|
| 90 |
procedure TViewMain.Start; |
| 91 |
begin |
| 92 |
inherited; |
| 93 |
|
| 94 |
// synchronize speed with ButtonSpeed.Pressed state |
| 95 |
StreetMoveSpeed := StreetMoveSpeeds[ButtonSpeed.Pressed]; |
| 96 |
|
| 97 |
Car1TargetX := - StreetColWidth; // car1 starts on left column |
| 98 |
|
| 99 |
MovingEnvCopy := ComponentClone(MovingEnv, FreeAtStop) as TCastleTransform; |
| 100 |
MovingEnvCopy.Translation := MovingEnv.Translation + Vector3(0, MovingEnvSize, 0); |
| 101 |
MainViewport.Items.Add(MovingEnvCopy); |
| 102 |
|
| 103 |
MovingEnv.InternalMovesPhysicsBodies := true; |
| 104 |
MovingEnvCopy.InternalMovesPhysicsBodies := true; |
| 105 |
|
| 106 |
MovingEnvNextDistance := MovingEnvSize; |
| 107 |
MovingEnvNextTransform := MovingEnv; |
| 108 |
|
| 109 |
ButtonCar1Left.OnClick := {$ifdef FPC}@{$endif} ClickCar1Left; |
| 110 |
ButtonCar1Right.OnClick := {$ifdef FPC}@{$endif} ClickCar1Right; |
| 111 |
ButtonCar2Left.OnClick := {$ifdef FPC}@{$endif} ClickCar2Left; |
| 112 |
ButtonCar2Right.OnClick := {$ifdef FPC}@{$endif} ClickCar2Right; |
| 113 |
ButtonSpeed.OnClick := {$ifdef FPC}@{$endif} ButtonSpeedClick; |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 117 |
|
| 118 |
procedure UpdateCarCollides(const Car: TCastlePlane; const CarCollideMarker: TCastleTransform); |
| 119 |
var |
| 120 |
OldCollides, Collides: Boolean; |
| 121 |
begin |
| 122 |
OldCollides := CarCollideMarker.Exists; |
| 123 |
Collides := Car.RigidBody.GetCollidingTransforms.Count <> 0; |
| 124 |
if not OldCollides and Collides then |
| 125 |
SoundEngine.Play(SoundCrash); |
| 126 |
CarCollideMarker.Exists := Collides; |
| 127 |
{ // does not work on mobile in CGE now |
| 128 |
if Collides then |
| 129 |
Car.RenderOptions.WireframeEffect := weSilhouette |
| 130 |
else |
| 131 |
Car.RenderOptions.WireframeEffect := weNormal; |
| 132 |
} |
| 133 |
end; |
| 134 |
|
| 135 |
procedure MoveCarHorizontally(const Car: TCastleTransform; const TargetX: Single); |
| 136 |
begin |
| 137 |
Car.Translation := Vector3( |
| 138 |
SmoothTowards(Car.Translation.X, TargetX, SecondsPassed, CarHorizontalMoveSpeed), |
| 139 |
Car.Translation.Y, |
| 140 |
Car.Translation.Z); |
| 141 |
end; |
| 142 |
|
| 143 |
var |
| 144 |
EnvMove: Single; |
| 145 |
MovingEnvPrevTransform: TCastleTransform; |
| 146 |
begin |
| 147 |
inherited; |
| 148 |
|
| 149 |
{ This virtual method is executed every frame (many times per second). } |
| 150 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 151 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 152 |
|
| 153 |
EnvMove := SecondsPassed * StreetMoveSpeed; |
| 154 |
Distance := Distance + EnvMove; |
| 155 |
MovingEnv.Translation := MovingEnv.Translation - Vector3(0, EnvMove, 0); |
| 156 |
MovingEnvCopy.Translation := MovingEnvCopy.Translation - Vector3(0, EnvMove, 0); |
| 157 |
|
| 158 |
if Distance >= MovingEnvNextDistance then |
| 159 |
begin |
| 160 |
// MovingEnvNextTransform swaps between MovingEnv and MovingEnvCopy |
| 161 |
if MovingEnvNextTransform = MovingEnv then |
| 162 |
MovingEnvPrevTransform := MovingEnvCopy |
| 163 |
else |
| 164 |
MovingEnvPrevTransform := MovingEnv; |
| 165 |
|
| 166 |
MovingEnvNextTransform.Translation := MovingEnvPrevTransform.Translation + Vector3(0, MovingEnvSize, 0); |
| 167 |
MovingEnvNextTransform := MovingEnvPrevTransform; |
| 168 |
MovingEnvNextDistance := MovingEnvNextDistance + MovingEnvSize; |
| 169 |
end; |
| 170 |
|
| 171 |
MoveCarHorizontally(Car1, Car1TargetX); |
| 172 |
MoveCarHorizontally(Car2, Car2TargetX); |
| 173 |
|
| 174 |
LabelDistance.Caption := FormatDot('%f', [Distance]); |
| 175 |
|
| 176 |
UpdateCarCollides(Car1, Car1CollideMarker); |
| 177 |
UpdateCarCollides(Car2, Car2CollideMarker); |
| 178 |
end; |
| 179 |
|
| 180 |
function TViewMain.CarNormalizeToColWidth(const X: Single): Single; |
| 181 |
begin |
| 182 |
if X < -StreetColWidth / 2 then |
| 183 |
Result := - StreetColWidth |
| 184 |
else |
| 185 |
if X > StreetColWidth / 2 then |
| 186 |
Result := StreetColWidth |
| 187 |
else |
| 188 |
Result := 0; |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TViewMain.ClickCar1Left(Sender: TObject); |
| 192 |
begin |
| 193 |
Car1TargetX := CarNormalizeToColWidth(Car1.Translation.X - StreetColWidth); |
| 194 |
end; |
| 195 |
|
| 196 |
procedure TViewMain.ClickCar1Right(Sender: TObject); |
| 197 |
begin |
| 198 |
Car1TargetX := CarNormalizeToColWidth(Car1.Translation.X + StreetColWidth); |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TViewMain.ClickCar2Left(Sender: TObject); |
| 202 |
begin |
| 203 |
Car2TargetX := CarNormalizeToColWidth(Car2.Translation.X - StreetColWidth); |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TViewMain.ClickCar2Right(Sender: TObject); |
| 207 |
begin |
| 208 |
Car2TargetX := CarNormalizeToColWidth(Car2.Translation.X + StreetColWidth); |
| 209 |
end; |
| 210 |
|
| 211 |
procedure TViewMain.ButtonSpeedClick(Sender: TObject); |
| 212 |
begin |
| 213 |
ButtonSpeed.Pressed := not ButtonSpeed.Pressed; |
| 214 |
StreetMoveSpeed := StreetMoveSpeeds[ButtonSpeed.Pressed]; |
| 215 |
end; |
| 216 |
|
| 217 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 218 |
begin |
| 219 |
Result := inherited; |
| 220 |
if Result then Exit; |
| 221 |
|
| 222 |
if Event.IsKey(keyA) then |
| 223 |
begin |
| 224 |
ClickCar1Left(nil); |
| 225 |
Exit(true); |
| 226 |
end; |
| 227 |
|
| 228 |
if Event.IsKey(keyS) then |
| 229 |
begin |
| 230 |
ClickCar1Right(nil); |
| 231 |
Exit(true); |
| 232 |
end; |
| 233 |
|
| 234 |
if Event.IsKey(keyK) then |
| 235 |
begin |
| 236 |
ClickCar2Left(nil); |
| 237 |
Exit(true); |
| 238 |
end; |
| 239 |
|
| 240 |
if Event.IsKey(keyL) then |
| 241 |
begin |
| 242 |
ClickCar2Right(nil); |
| 243 |
Exit(true); |
| 244 |
end; |
| 245 |
|
| 246 |
if Event.IsKey(keySpace) then |
| 247 |
begin |
| 248 |
ButtonSpeedClick(nil); |
| 249 |
Exit(true); |
| 250 |
end; |
| 251 |
end; |
| 252 |
|
| 253 |
end. |