| 1 |
{ |
| 2 |
Copyright 2021-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, Math, |
| 22 |
CastleComponentSerialize, CastleUIControls, CastleControls, |
| 23 |
CastleKeysMouse, CastleVectors; |
| 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 |
ImagePlayer: TCastleImageControl; |
| 33 |
public |
| 34 |
constructor Create(AOwner: TComponent); override; |
| 35 |
procedure Start; override; |
| 36 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 37 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 38 |
end; |
| 39 |
|
| 40 |
var |
| 41 |
ViewMain: TViewMain; |
| 42 |
|
| 43 |
implementation |
| 44 |
|
| 45 |
uses SysUtils; |
| 46 |
|
| 47 |
{ TViewMain ----------------------------------------------------------------- } |
| 48 |
|
| 49 |
constructor TViewMain.Create(AOwner: TComponent); |
| 50 |
begin |
| 51 |
inherited; |
| 52 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 53 |
end; |
| 54 |
|
| 55 |
procedure TViewMain.Start; |
| 56 |
begin |
| 57 |
inherited; |
| 58 |
end; |
| 59 |
|
| 60 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 61 |
const |
| 62 |
MoveSpeed = 800; |
| 63 |
var |
| 64 |
PlayerPosition: TVector2; |
| 65 |
begin |
| 66 |
inherited; |
| 67 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 68 |
|
| 69 |
PlayerPosition := ImagePlayer.Translation; |
| 70 |
|
| 71 |
if Container.Pressed[keyArrowLeft] then |
| 72 |
PlayerPosition := PlayerPosition + Vector2(-MoveSpeed * SecondsPassed, 0); |
| 73 |
if Container.Pressed[keyArrowRight] then |
| 74 |
PlayerPosition := PlayerPosition + Vector2( MoveSpeed * SecondsPassed, 0); |
| 75 |
if Container.Pressed[keyArrowDown] then |
| 76 |
PlayerPosition := PlayerPosition + Vector2(0, -MoveSpeed * SecondsPassed); |
| 77 |
if Container.Pressed[keyArrowUp] then |
| 78 |
PlayerPosition := PlayerPosition + Vector2(0, MoveSpeed * SecondsPassed); |
| 79 |
|
| 80 |
{ update player position to fall down } |
| 81 |
PlayerPosition.Y := Max(PlayerPosition.Y - SecondsPassed * 400, 0); |
| 82 |
ImagePlayer.Translation := PlayerPosition; |
| 83 |
end; |
| 84 |
|
| 85 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 86 |
begin |
| 87 |
Result := inherited; |
| 88 |
if Result then Exit; // allow the ancestor to handle keys |
| 89 |
|
| 90 |
{ This virtual method is executed when user presses |
| 91 |
a key, a mouse button, or touches a touch-screen. |
| 92 |
|
| 93 |
Note that each UI control has also events like OnPress and OnClick. |
| 94 |
These events can be used to handle the "press", if it should do something |
| 95 |
specific when used in that UI control. |
| 96 |
The TViewMain.Press method should be used to handle keys |
| 97 |
not handled in children controls. |
| 98 |
} |
| 99 |
|
| 100 |
if Event.IsKey(keySpace) then |
| 101 |
begin |
| 102 |
ImagePlayer.Color := Vector4(Random, Random, Random, 1); |
| 103 |
Exit(true); // event was handled |
| 104 |
end; |
| 105 |
|
| 106 |
if Event.IsMouseButton(buttonLeft) then |
| 107 |
begin |
| 108 |
ImagePlayer.Translation := ImagePlayer.Parent.ContainerToLocalPosition(Event.Position); |
| 109 |
Exit(true); // event was handled |
| 110 |
end; |
| 111 |
end; |
| 112 |
|
| 113 |
end. |