| 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 |
{ TApplyImpulseBehavior } |
| 17 |
unit ApplyImpulseBehavior; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors, |
| 23 |
CastleComponentSerialize, CastleClassUtils, AbstractTimeDurationBehavior; |
| 24 |
|
| 25 |
type |
| 26 |
TApplyImpulseBehavior = class(TAbstractTimeDurationBehavior) |
| 27 |
private |
| 28 |
FImpulse: TVector3; |
| 29 |
FPosition: TVector3; |
| 30 |
|
| 31 |
FImpulsePersistent: TCastleVector3Persistent; |
| 32 |
FPositionPersistent: TCastleVector3Persistent; |
| 33 |
|
| 34 |
function GetImpulseForPersistent: TVector3; |
| 35 |
procedure SetImpulseForPersistent(const AValue: TVector3); |
| 36 |
function GetPositionForPersistent: TVector3; |
| 37 |
procedure SetPositionForPersistent(const AValue: TVector3); |
| 38 |
public |
| 39 |
constructor Create(AOwner: TComponent); override; |
| 40 |
destructor Destroy;override; |
| 41 |
|
| 42 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 43 |
|
| 44 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 45 |
|
| 46 |
property Impulse: TVector3 read FImpulse write FImpulse; |
| 47 |
property Position: TVector3 read FPosition write FPosition; |
| 48 |
published |
| 49 |
property ImpulsePersistent: TCastleVector3Persistent read FImpulsePersistent; |
| 50 |
property PositionPersistent: TCastleVector3Persistent read FPositionPersistent; |
| 51 |
end; |
| 52 |
|
| 53 |
implementation |
| 54 |
|
| 55 |
{ TApplyImpulseBehavior ------------------------------------------------------ } |
| 56 |
|
| 57 |
function TApplyImpulseBehavior.GetImpulseForPersistent: TVector3; |
| 58 |
begin |
| 59 |
Result := Impulse; |
| 60 |
end; |
| 61 |
|
| 62 |
procedure TApplyImpulseBehavior.SetImpulseForPersistent(const AValue: TVector3); |
| 63 |
begin |
| 64 |
Impulse := AValue; |
| 65 |
end; |
| 66 |
|
| 67 |
function TApplyImpulseBehavior.GetPositionForPersistent: TVector3; |
| 68 |
begin |
| 69 |
Result := Position; |
| 70 |
end; |
| 71 |
|
| 72 |
procedure TApplyImpulseBehavior.SetPositionForPersistent(const AValue: TVector3); |
| 73 |
begin |
| 74 |
Position := AValue; |
| 75 |
end; |
| 76 |
|
| 77 |
constructor TApplyImpulseBehavior.Create(AOwner: TComponent); |
| 78 |
begin |
| 79 |
inherited Create(AOwner); |
| 80 |
|
| 81 |
FImpulsePersistent := TCastleVector3Persistent.Create; |
| 82 |
FImpulsePersistent.InternalGetValue := {$ifdef FPC}@{$endif}GetImpulseForPersistent; |
| 83 |
FImpulsePersistent.InternalSetValue := {$ifdef FPC}@{$endif}SetImpulseForPersistent; |
| 84 |
FImpulsePersistent.InternalDefaultValue := Impulse; // current value is default |
| 85 |
|
| 86 |
FPositionPersistent := TCastleVector3Persistent.Create; |
| 87 |
FPositionPersistent.InternalGetValue := {$ifdef FPC}@{$endif}GetPositionForPersistent; |
| 88 |
FPositionPersistent.InternalSetValue := {$ifdef FPC}@{$endif}SetPositionForPersistent; |
| 89 |
FPositionPersistent.InternalDefaultValue := Position; // current value is default |
| 90 |
end; |
| 91 |
|
| 92 |
destructor TApplyImpulseBehavior.Destroy; |
| 93 |
begin |
| 94 |
FreeAndNil(FImpulsePersistent); |
| 95 |
FreeAndNil(FPositionPersistent); |
| 96 |
inherited; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TApplyImpulseBehavior.Update(const SecondsPassed: Single; |
| 100 |
var RemoveMe: TRemoveType); |
| 101 |
var |
| 102 |
RigidBody: TCastleRigidBody; |
| 103 |
begin |
| 104 |
inherited Update(SecondsPassed, RemoveMe); |
| 105 |
|
| 106 |
if not ShouldUpdate then |
| 107 |
Exit; |
| 108 |
|
| 109 |
RigidBody := Parent.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 110 |
if (RigidBody <> nil) and (RigidBody.ExistsInRoot) then |
| 111 |
begin |
| 112 |
RigidBody.ApplyImpulse(Impulse, Position); |
| 113 |
RigidBody.WakeUp; |
| 114 |
end; |
| 115 |
end; |
| 116 |
|
| 117 |
function TApplyImpulseBehavior.PropertySections(const PropertyName: String |
| 118 |
): TPropertySections; |
| 119 |
begin |
| 120 |
if (PropertyName = 'Impulse') or |
| 121 |
(PropertyName = 'Position') then |
| 122 |
Result := [psBasic] |
| 123 |
else |
| 124 |
Result := inherited PropertySections(PropertyName); |
| 125 |
end; |
| 126 |
|
| 127 |
initialization |
| 128 |
|
| 129 |
RegisterSerializableComponent(TApplyImpulseBehavior, 'Apply Impulse Behavior'); |
| 130 |
|
| 131 |
end. |
| 132 |
|