| 1 |
{ |
| 2 |
Copyright 2021-2021 Andrzej Kilijański, 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 |
{ Moving Platform. } |
| 17 |
unit GameMovingPlatform; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, Generics.Collections, |
| 22 |
CastleVectors, CastleScene, CastleTransform; |
| 23 |
|
| 24 |
type |
| 25 |
TMovingPlatform = class(TCastleBehavior) |
| 26 |
strict private |
| 27 |
Scene: TCastleScene; |
| 28 |
RBody: TCastleRigidBody; |
| 29 |
MoveDirection: Integer; // Always 1 or -1 |
| 30 |
StartPoint: TVector3; |
| 31 |
StopPoint: TVector3; |
| 32 |
|
| 33 |
function IsVerticalMove: Boolean; |
| 34 |
public |
| 35 |
constructor Create(AOwner: TComponent); override; |
| 36 |
procedure ParentAfterAttach; override; |
| 37 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 38 |
end; |
| 39 |
|
| 40 |
TMovingPlatformList = {$ifdef FPC}specialize{$endif} TObjectList<TMovingPlatform>; |
| 41 |
|
| 42 |
implementation |
| 43 |
|
| 44 |
uses |
| 45 |
CastleLog, |
| 46 |
GameViewPlay; |
| 47 |
|
| 48 |
{ TMovingPlatform ------------------------------------------------------------ } |
| 49 |
|
| 50 |
function TMovingPlatform.IsVerticalMove: Boolean; |
| 51 |
begin |
| 52 |
Result := (Scene.Tag < 0) |
| 53 |
end; |
| 54 |
|
| 55 |
constructor TMovingPlatform.Create(AOwner: TComponent); |
| 56 |
begin |
| 57 |
inherited Create(AOwner); |
| 58 |
MoveDirection := -1; |
| 59 |
end; |
| 60 |
|
| 61 |
procedure TMovingPlatform.ParentAfterAttach; |
| 62 |
|
| 63 |
procedure ConfigurePhysics; |
| 64 |
var |
| 65 |
Collider: TCastleCollider; |
| 66 |
begin |
| 67 |
RBody := Parent.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 68 |
Assert(RBody <> nil, 'No TCastleRigidBody in TMovingPlatform Parent'); |
| 69 |
|
| 70 |
RBody.Dynamic := true; |
| 71 |
if Scene.Tag > 0 then |
| 72 |
RBody.LockTranslation := [1, 2] |
| 73 |
else |
| 74 |
if Scene.Tag < 0 then |
| 75 |
RBody.LockTranslation := [0, 2]; |
| 76 |
|
| 77 |
Collider := Scene.FindBehavior(TCastleCollider) as TCastleCollider; |
| 78 |
|
| 79 |
Assert(Collider <> nil, 'No Castle Collider in Parent'); |
| 80 |
Collider.Friction := 100; |
| 81 |
Collider.Restitution := 0; |
| 82 |
end; |
| 83 |
|
| 84 |
var |
| 85 |
Distance: Single; |
| 86 |
begin |
| 87 |
inherited; |
| 88 |
Scene := Parent as TCastleScene; |
| 89 |
|
| 90 |
if Scene.Scale.X < 0 then |
| 91 |
MoveDirection := 1 |
| 92 |
else |
| 93 |
MoveDirection := -1; |
| 94 |
|
| 95 |
StartPoint := Scene.Translation; |
| 96 |
|
| 97 |
Distance := Abs(Scene.Tag); |
| 98 |
|
| 99 |
if IsVerticalMove then |
| 100 |
StopPoint := StartPoint + Vector3(0, Distance, 0) // vertical move |
| 101 |
else |
| 102 |
StopPoint := StartPoint + Vector3(Distance, 0, 0); // horizontal move |
| 103 |
|
| 104 |
ConfigurePhysics; |
| 105 |
end; |
| 106 |
|
| 107 |
procedure TMovingPlatform.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 108 |
const |
| 109 |
MovingSpeed = 100; |
| 110 |
var |
| 111 |
Vel: TVector3; |
| 112 |
begin |
| 113 |
inherited; |
| 114 |
|
| 115 |
if IsVerticalMove then |
| 116 |
begin |
| 117 |
if Scene.Translation.Y > StopPoint.Y then |
| 118 |
MoveDirection := - 1 |
| 119 |
else |
| 120 |
if Scene.Translation.Y < StartPoint.Y then |
| 121 |
MoveDirection := 1; |
| 122 |
end else |
| 123 |
begin |
| 124 |
if Scene.Translation.X > StopPoint.X then |
| 125 |
MoveDirection := - 1 |
| 126 |
else |
| 127 |
if Scene.Translation.X < StartPoint.X then |
| 128 |
MoveDirection := 1; |
| 129 |
end; |
| 130 |
|
| 131 |
if RBody <> nil then |
| 132 |
begin |
| 133 |
Vel := RBody.LinearVelocity; |
| 134 |
|
| 135 |
if IsVerticalMove then |
| 136 |
Vel.Y := MoveDirection * MovingSpeed |
| 137 |
else |
| 138 |
Vel.X := MoveDirection * MovingSpeed; |
| 139 |
|
| 140 |
RBody.LinearVelocity := Vel; |
| 141 |
end; |
| 142 |
end; |
| 143 |
|
| 144 |
end. |