| 1 |
{ |
| 2 |
Copyright 2017-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 |
{ Main view, where most of the application logic takes place. } |
| 16 |
unit GameViewMain; |
| 17 |
|
| 18 |
interface |
| 19 |
|
| 20 |
uses Classes, |
| 21 |
CastleVectors, CastleComponentSerialize, CastleNotifications, |
| 22 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleViewport, CastleTransform; |
| 23 |
|
| 24 |
type |
| 25 |
{ Main view, where most of the application logic takes place. } |
| 26 |
TViewMain = class(TCastleView) |
| 27 |
published |
| 28 |
{ Components designed using CGE editor. |
| 29 |
These fields will be automatically initialized at Start. } |
| 30 |
LabelFps, LabelCollisionsInfo: TCastleLabel; |
| 31 |
Viewport: TCastleViewport; |
| 32 |
Plane: TCastleTransform; |
| 33 |
MainNotifications: TCastleNotifications; |
| 34 |
WaterRigidBody, PlaneRigidBody: TCastleRigidBody; |
| 35 |
private |
| 36 |
procedure UpdatePlanePosition(const EventPosition: TVector2); |
| 37 |
procedure WaterCollisionEnter(const CollisionDetails: TPhysicsCollisionDetails); |
| 38 |
procedure WaterCollisionExit(const CollisionDetails: TPhysicsCollisionDetails); |
| 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 |
function Motion(const Event: TInputMotion): Boolean; override; |
| 45 |
end; |
| 46 |
|
| 47 |
var |
| 48 |
ViewMain: TViewMain; |
| 49 |
|
| 50 |
implementation |
| 51 |
|
| 52 |
uses SysUtils, |
| 53 |
CastleStringUtils; |
| 54 |
|
| 55 |
{ TViewMain ----------------------------------------------------------------- } |
| 56 |
|
| 57 |
constructor TViewMain.Create(AOwner: TComponent); |
| 58 |
begin |
| 59 |
inherited; |
| 60 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 61 |
end; |
| 62 |
|
| 63 |
procedure TViewMain.Start; |
| 64 |
begin |
| 65 |
inherited; |
| 66 |
WaterRigidBody.OnCollisionEnter := {$ifdef FPC}@{$endif} WaterCollisionEnter; |
| 67 |
WaterRigidBody.OnCollisionExit := {$ifdef FPC}@{$endif} WaterCollisionExit; |
| 68 |
end; |
| 69 |
|
| 70 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 71 |
|
| 72 |
function CollisionsListStr(const RBody: TCastleRigidBody): String; |
| 73 |
var |
| 74 |
CollisionsList: TCastleTransformList; |
| 75 |
I: Integer; |
| 76 |
begin |
| 77 |
CollisionsList := RBody.GetCollidingTransforms; |
| 78 |
Result := ''; |
| 79 |
|
| 80 |
for I := 0 to CollisionsList.Count - 1 do |
| 81 |
Result := SAppendPart(Result, ',', CollisionsList[I].Name); |
| 82 |
|
| 83 |
if Result = '' then |
| 84 |
Result := 'nothing'; |
| 85 |
end; |
| 86 |
|
| 87 |
begin |
| 88 |
inherited; |
| 89 |
{ This virtual method is executed every frame (many times per second). } |
| 90 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 91 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 92 |
|
| 93 |
LabelCollisionsInfo.Caption := 'Plane collides now with: ' + |
| 94 |
CollisionsListStr(PlaneRigidBody); |
| 95 |
end; |
| 96 |
|
| 97 |
procedure TViewMain.UpdatePlanePosition(const EventPosition: TVector2); |
| 98 |
begin |
| 99 |
Plane.Translation := Vector3(Viewport.PositionTo2DWorld(EventPosition, true), 0); |
| 100 |
end; |
| 101 |
|
| 102 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 103 |
begin |
| 104 |
Result := inherited; |
| 105 |
if Result then Exit; // allow the ancestor to handle keys |
| 106 |
|
| 107 |
if Event.IsMouseButton(buttonLeft) then |
| 108 |
begin |
| 109 |
UpdatePlanePosition(Event.Position); |
| 110 |
Exit(true); |
| 111 |
end; |
| 112 |
end; |
| 113 |
|
| 114 |
function TViewMain.Motion(const Event: TInputMotion): Boolean; |
| 115 |
begin |
| 116 |
Result := inherited; |
| 117 |
if Result then Exit; // allow the ancestor to handle keys |
| 118 |
|
| 119 |
if buttonLeft in Event.Pressed then |
| 120 |
begin |
| 121 |
UpdatePlanePosition(Event.Position); |
| 122 |
Exit(true); |
| 123 |
end; |
| 124 |
end; |
| 125 |
|
| 126 |
procedure TViewMain.WaterCollisionEnter(const CollisionDetails: TPhysicsCollisionDetails); |
| 127 |
begin |
| 128 |
MainNotifications.Show('Water starts colliding with ' + CollisionDetails.OtherTransform.Name); |
| 129 |
end; |
| 130 |
|
| 131 |
procedure TViewMain.WaterCollisionExit(const CollisionDetails: TPhysicsCollisionDetails); |
| 132 |
begin |
| 133 |
MainNotifications.Show('Water stops colliding with ' + CollisionDetails.OtherTransform.Name); |
| 134 |
end; |
| 135 |
|
| 136 |
end. |