Debian Codegraph @

examples/physics/physics_persistent_forces_components/code/addforceatpositionbehavior.pas

raw
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 { TAddForceAtPositionBehavior }
17 unit AddForceAtPositionBehavior;
18
19 interface
20
21 uses
22 Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors,
23 CastleComponentSerialize, CastleClassUtils, AbstractTimeDurationBehavior;
24
25 type
26 TAddForceAtPositionBehavior = class(TAbstractTimeDurationBehavior)
27 private
28 FForce: TVector3;
29 FPosition: TVector3;
30
31 FForcePersistent: TCastleVector3Persistent;
32 FPositionPersistent: TCastleVector3Persistent;
33
34 function GetForceForPersistent: TVector3;
35 procedure SetForceForPersistent(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 Force: TVector3 read FForce write FForce;
47 property Position: TVector3 read FPosition write FPosition;
48 published
49 property ForcePersistent: TCastleVector3Persistent read FForcePersistent;
50 property PositionPersistent: TCastleVector3Persistent read FPositionPersistent;
51 end;
52
53 implementation
54
55 { TAddForceAtPositionBehavior ---------------------------------------------------------- }
56
57 function TAddForceAtPositionBehavior.GetForceForPersistent: TVector3;
58 begin
59 Result := Force;
60 end;
61
62 procedure TAddForceAtPositionBehavior.SetForceForPersistent(const AValue: TVector3);
63 begin
64 Force := AValue;
65 end;
66
67 function TAddForceAtPositionBehavior.GetPositionForPersistent: TVector3;
68 begin
69 Result := Position;
70 end;
71
72 procedure TAddForceAtPositionBehavior.SetPositionForPersistent(const AValue: TVector3);
73 begin
74 Position := AValue;
75 end;
76
77 constructor TAddForceAtPositionBehavior.Create(AOwner: TComponent);
78 begin
79 inherited Create(AOwner);
80
81 FForcePersistent := TCastleVector3Persistent.Create;
82 FForcePersistent.InternalGetValue := {$ifdef FPC}@{$endif}GetForceForPersistent;
83 FForcePersistent.InternalSetValue := {$ifdef FPC}@{$endif}SetForceForPersistent;
84 FForcePersistent.InternalDefaultValue := Force; // 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 TAddForceAtPositionBehavior.Destroy;
93 begin
94 FreeAndNil(FForcePersistent);
95 FreeAndNil(FPositionPersistent);
96 inherited;
97 end;
98
99 procedure TAddForceAtPositionBehavior.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.AddForceAtPosition(Force, Position);
113 RigidBody.WakeUp;
114 end;
115 end;
116
117 function TAddForceAtPositionBehavior.PropertySections(const PropertyName: String
118 ): TPropertySections;
119 begin
120 if (PropertyName = 'Force') or
121 (PropertyName = 'Position') then
122 Result := [psBasic]
123 else
124 Result := inherited PropertySections(PropertyName);
125 end;
126
127 initialization
128 RegisterSerializableComponent(TAddForceAtPositionBehavior, 'Add Force At Position Behavior');
129 end.