| 1 |
{ |
| 2 |
Copyright 2022-2022 Michalis Kamburelis, Andrzej Kilijański. |
| 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 |
{ TAbstractIterateRigidBodyBehavior } |
| 17 |
unit AbstractIterateRigidBodyBehavior; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, CastleTransform, AbstractTimeDurationBehavior; |
| 23 |
|
| 24 |
type |
| 25 |
{ Inherit from this class if you want update all other rigid bodies in world. |
| 26 |
It searches for TCastleRigidBody in world items list and its childern if |
| 27 |
castle transform doesn't have rigid body. All you need to do is |
| 28 |
override UpdateRigidBody(). } |
| 29 |
TAbstractIterateRigidBodyBehavior = class(TAbstractTimeDurationBehavior) |
| 30 |
private |
| 31 |
{ Check for rigid bodies in transform children and call on it |
| 32 |
UpdateRigidBody method. } |
| 33 |
procedure UpdateTransformChildren(const ParentTransform: TCastleTransform; const SecondsPassed: Single; |
| 34 |
var RemoveMe: TRemoveType); |
| 35 |
protected |
| 36 |
{ Override it to update found rigid bodies. } |
| 37 |
procedure UpdateRigidBody(const RigidBody: TCastleRigidBody; const SecondsPassed: Single; |
| 38 |
var RemoveMe: TRemoveType); virtual; abstract; |
| 39 |
public |
| 40 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 41 |
end; |
| 42 |
|
| 43 |
implementation |
| 44 |
|
| 45 |
{ TAbstractIterateRigidBodyBehavior ------------------------------------------ } |
| 46 |
|
| 47 |
procedure TAbstractIterateRigidBodyBehavior.UpdateTransformChildren( |
| 48 |
const ParentTransform: TCastleTransform; const SecondsPassed: Single; |
| 49 |
var RemoveMe: TRemoveType); |
| 50 |
var |
| 51 |
I: Integer; |
| 52 |
Transform: TCastleTransform; |
| 53 |
RigidBody: TCastleRigidBody; |
| 54 |
begin |
| 55 |
for I := 0 to ParentTransform.Count -1 do |
| 56 |
begin |
| 57 |
Transform := ParentTransform.Items[I]; |
| 58 |
|
| 59 |
if Transform = Parent then |
| 60 |
continue; |
| 61 |
|
| 62 |
if not Transform.ExistsInRoot then |
| 63 |
continue; |
| 64 |
|
| 65 |
RigidBody := Transform.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 66 |
if (RigidBody <> nil) and (RigidBody.ExistsInRoot) then |
| 67 |
UpdateRigidBody(RigidBody, SecondsPassed, RemoveMe) |
| 68 |
else |
| 69 |
UpdateTransformChildren(Transform, SecondsPassed, RemoveMe); |
| 70 |
end; |
| 71 |
end; |
| 72 |
|
| 73 |
procedure TAbstractIterateRigidBodyBehavior.Update(const SecondsPassed: Single; |
| 74 |
var RemoveMe: TRemoveType); |
| 75 |
begin |
| 76 |
inherited Update(SecondsPassed, RemoveMe); |
| 77 |
|
| 78 |
if not ShouldUpdate then |
| 79 |
Exit; |
| 80 |
|
| 81 |
UpdateTransformChildren(World, SecondsPassed, RemoveMe); |
| 82 |
end; |
| 83 |
|
| 84 |
end. |
| 85 |
|