| 1 |
{ Main "playing game" view, where most of the game logic takes place. |
| 2 |
|
| 3 |
Feel free to use this code as a starting point for your own projects. |
| 4 |
This template code is in public domain, unlike most other CGE code which |
| 5 |
is covered by BSD or LGPL (see https://castle-engine.io/license). } |
| 6 |
unit GameViewPlay; |
| 7 |
|
| 8 |
interface |
| 9 |
|
| 10 |
uses Classes, |
| 11 |
CastleComponentSerialize, CastleUIControls, CastleControls, |
| 12 |
CastleKeysMouse, CastleViewport, CastleScene, CastleVectors; |
| 13 |
|
| 14 |
type |
| 15 |
{ Main "playing game" view, where most of the game logic takes place. } |
| 16 |
TViewPlay = class(TCastleView) |
| 17 |
published |
| 18 |
{ Components designed using CGE editor. |
| 19 |
These fields will be automatically initialized at Start. } |
| 20 |
LabelFps: TCastleLabel; |
| 21 |
MainViewport: TCastleViewport; |
| 22 |
SceneDragon: TCastleScene; |
| 23 |
CheckboxCameraFollow: TCastleCheckbox; |
| 24 |
private |
| 25 |
{ DragonFlying and DragonFlyingTarget manage currect dragon (SceneDragon) |
| 26 |
animation and it's movement. } |
| 27 |
DragonFlying: Boolean; |
| 28 |
DragonFlyingTarget: TVector2; |
| 29 |
public |
| 30 |
constructor Create(AOwner: TComponent); override; |
| 31 |
procedure Start; override; |
| 32 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 33 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 34 |
end; |
| 35 |
|
| 36 |
var |
| 37 |
ViewPlay: TViewPlay; |
| 38 |
|
| 39 |
implementation |
| 40 |
|
| 41 |
uses SysUtils, Math, |
| 42 |
GameViewMenu; |
| 43 |
|
| 44 |
{ TViewPlay ----------------------------------------------------------------- } |
| 45 |
|
| 46 |
constructor TViewPlay.Create(AOwner: TComponent); |
| 47 |
begin |
| 48 |
inherited; |
| 49 |
DesignUrl := 'castle-data:/gameviewplay.castle-user-interface'; |
| 50 |
end; |
| 51 |
|
| 52 |
procedure TViewPlay.Start; |
| 53 |
begin |
| 54 |
inherited; |
| 55 |
end; |
| 56 |
|
| 57 |
procedure TViewPlay.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 58 |
const |
| 59 |
DragonSpeed: TVector2 = (X: 3000; Y: 1500); |
| 60 |
var |
| 61 |
T: TVector2; |
| 62 |
CamPos: TVector3; |
| 63 |
begin |
| 64 |
inherited; |
| 65 |
{ This virtual method is executed every frame (many times per second). } |
| 66 |
|
| 67 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 68 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 69 |
|
| 70 |
if DragonFlying then |
| 71 |
begin |
| 72 |
{ Update SceneDragon.TranslationXY to reach DragonFlyingTarget. } |
| 73 |
T := SceneDragon.TranslationXY; |
| 74 |
if T.X < DragonFlyingTarget.X then |
| 75 |
T.X := Min(DragonFlyingTarget.X, T.X + DragonSpeed.X * SecondsPassed) |
| 76 |
else |
| 77 |
T.X := Max(DragonFlyingTarget.X, T.X - DragonSpeed.X * SecondsPassed); |
| 78 |
if T.Y < DragonFlyingTarget.Y then |
| 79 |
T.Y := Min(DragonFlyingTarget.Y, T.Y + DragonSpeed.Y * SecondsPassed) |
| 80 |
else |
| 81 |
T.Y := Max(DragonFlyingTarget.Y, T.Y - DragonSpeed.Y * SecondsPassed); |
| 82 |
SceneDragon.TranslationXY := T; |
| 83 |
|
| 84 |
{ Check did we reach the DragonFlyingTarget. Note that we can compare floats |
| 85 |
using exact "=" operator (no need to use SameValue), because |
| 86 |
our Min/Maxes above make sure that we will reach the *exact* DragonFlyingTarget |
| 87 |
value. } |
| 88 |
if (T.X = DragonFlyingTarget.X) and |
| 89 |
(T.Y = DragonFlyingTarget.Y) then |
| 90 |
begin |
| 91 |
DragonFlying := false; |
| 92 |
SceneDragon.PlayAnimation('idle', true); |
| 93 |
end else |
| 94 |
{ If we're still flying then |
| 95 |
update SceneDragon.Scale to reflect direction we're flying to. |
| 96 |
Flipping Scale.X is an easy way to flip 2D objects. } |
| 97 |
if DragonFlyingTarget.X > SceneDragon.Translation.X then |
| 98 |
SceneDragon.Scale := Vector3(-1, 1, 1) |
| 99 |
else |
| 100 |
SceneDragon.Scale := Vector3(1, 1, 1); |
| 101 |
end; |
| 102 |
|
| 103 |
if CheckboxCameraFollow.Checked then |
| 104 |
begin |
| 105 |
CamPos := MainViewport.Camera.Translation; |
| 106 |
CamPos.X := SceneDragon.Translation.X; |
| 107 |
MainViewport.Camera.Translation := CamPos; |
| 108 |
end; |
| 109 |
end; |
| 110 |
|
| 111 |
function TViewPlay.Press(const Event: TInputPressRelease): Boolean; |
| 112 |
begin |
| 113 |
Result := inherited; |
| 114 |
if Result then Exit; // allow the ancestor to handle keys |
| 115 |
|
| 116 |
{ This virtual method is executed when user presses |
| 117 |
a key, a mouse button, or touches a touch-screen. |
| 118 |
|
| 119 |
Note that each UI control has also events like OnPress and OnClick. |
| 120 |
These events can be used to handle the "press", if it should do something |
| 121 |
specific when used in that UI control. |
| 122 |
The TViewPlay.Press method should be used to handle keys |
| 123 |
not handled in children controls. |
| 124 |
} |
| 125 |
|
| 126 |
if Event.IsMouseButton(buttonLeft) then |
| 127 |
begin |
| 128 |
DragonFlyingTarget := MainViewport.PositionTo2DWorld(Event.Position, true); |
| 129 |
if not DragonFlying then |
| 130 |
begin |
| 131 |
SceneDragon.PlayAnimation('flying', true); |
| 132 |
DragonFlying := true; |
| 133 |
end; |
| 134 |
|
| 135 |
Exit(true); // click was handled |
| 136 |
end; |
| 137 |
|
| 138 |
if Event.IsKey(keyF5) then |
| 139 |
begin |
| 140 |
Container.SaveScreenToDefaultFile; |
| 141 |
Exit(true); |
| 142 |
end; |
| 143 |
|
| 144 |
if Event.IsKey(keyEscape) then |
| 145 |
begin |
| 146 |
Container.View := ViewMenu; |
| 147 |
Exit(true); |
| 148 |
end; |
| 149 |
end; |
| 150 |
|
| 151 |
end. |