| 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 |
{ TAddTorqueBehavior } |
| 17 |
unit AddTorqueBehavior; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors, |
| 23 |
CastleComponentSerialize, CastleClassUtils, AbstractTimeDurationBehavior; |
| 24 |
|
| 25 |
type |
| 26 |
TAddTorqueBehavior = class(TAbstractTimeDurationBehavior) |
| 27 |
private |
| 28 |
FTorque: TVector3; |
| 29 |
|
| 30 |
FTorquePersistent: TCastleVector3Persistent; |
| 31 |
|
| 32 |
function GetTorqueForPersistent: TVector3; |
| 33 |
procedure SetTorqueForPersistent(const AValue: TVector3); |
| 34 |
public |
| 35 |
constructor Create(AOwner: TComponent); override; |
| 36 |
destructor Destroy;override; |
| 37 |
|
| 38 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 39 |
|
| 40 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 41 |
|
| 42 |
property Torque: TVector3 read FTorque write FTorque; |
| 43 |
published |
| 44 |
property TorquePersistent: TCastleVector3Persistent read FTorquePersistent; |
| 45 |
end; |
| 46 |
|
| 47 |
implementation |
| 48 |
|
| 49 |
{ TAddTorqueBehavior --------------------------------------------------------- } |
| 50 |
|
| 51 |
function TAddTorqueBehavior.GetTorqueForPersistent: TVector3; |
| 52 |
begin |
| 53 |
Result := Torque; |
| 54 |
end; |
| 55 |
|
| 56 |
procedure TAddTorqueBehavior.SetTorqueForPersistent(const AValue: TVector3); |
| 57 |
begin |
| 58 |
Torque := AValue; |
| 59 |
end; |
| 60 |
|
| 61 |
constructor TAddTorqueBehavior.Create(AOwner: TComponent); |
| 62 |
begin |
| 63 |
inherited Create(AOwner); |
| 64 |
|
| 65 |
FTorquePersistent := TCastleVector3Persistent.Create; |
| 66 |
FTorquePersistent.InternalGetValue := {$ifdef FPC}@{$endif}GetTorqueForPersistent; |
| 67 |
FTorquePersistent.InternalSetValue := {$ifdef FPC}@{$endif}SetTorqueForPersistent; |
| 68 |
FTorquePersistent.InternalDefaultValue := Torque; // current value is default |
| 69 |
end; |
| 70 |
|
| 71 |
destructor TAddTorqueBehavior.Destroy; |
| 72 |
begin |
| 73 |
FreeAndNil(FTorquePersistent); |
| 74 |
inherited; |
| 75 |
end; |
| 76 |
|
| 77 |
procedure TAddTorqueBehavior.Update(const SecondsPassed: Single; |
| 78 |
var RemoveMe: TRemoveType); |
| 79 |
var |
| 80 |
RigidBody: TCastleRigidBody; |
| 81 |
begin |
| 82 |
inherited Update(SecondsPassed, RemoveMe); |
| 83 |
|
| 84 |
if not ShouldUpdate then |
| 85 |
Exit; |
| 86 |
|
| 87 |
RigidBody := Parent.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 88 |
if (RigidBody <> nil) and (RigidBody.ExistsInRoot) then |
| 89 |
begin |
| 90 |
RigidBody.AddTorque(Torque); |
| 91 |
RigidBody.WakeUp; |
| 92 |
end; |
| 93 |
end; |
| 94 |
|
| 95 |
function TAddTorqueBehavior.PropertySections(const PropertyName: String |
| 96 |
): TPropertySections; |
| 97 |
begin |
| 98 |
if (PropertyName = 'Torque') then |
| 99 |
Result := [psBasic] |
| 100 |
else |
| 101 |
Result := inherited PropertySections(PropertyName); |
| 102 |
end; |
| 103 |
|
| 104 |
initialization |
| 105 |
|
| 106 |
RegisterSerializableComponent(TAddTorqueBehavior, 'Add Torque Behavior'); |
| 107 |
|
| 108 |
end. |
| 109 |
|