Debian Codegraph @

examples/physics/physics_persistent_forces_components/code/addforcebehavior.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 { TAddForceBehavior }
17 unit AddForceBehavior;
18
19 interface
20
21 uses
22 Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors,
23 CastleComponentSerialize, CastleClassUtils, AbstractTimeDurationBehavior;
24
25 type
26 TAddForceBehavior = class(TAbstractTimeDurationBehavior)
27 private
28 FForce: TVector3;
29
30 FForcePersistent: TCastleVector3Persistent;
31
32 function GetForceForPersistent: TVector3;
33 procedure SetForceForPersistent(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 Force: TVector3 read FForce write FForce;
43 published
44 property ForcePersistent: TCastleVector3Persistent read FForcePersistent;
45 end;
46
47 implementation
48
49 { TAddForceBehavior --------------------------------------------------- }
50
51 function TAddForceBehavior.GetForceForPersistent: TVector3;
52 begin
53 Result := Force;
54 end;
55
56 procedure TAddForceBehavior.SetForceForPersistent(const AValue: TVector3);
57 begin
58 Force := AValue;
59 end;
60
61 constructor TAddForceBehavior.Create(AOwner: TComponent);
62 begin
63 inherited Create(AOwner);
64
65 FForcePersistent := TCastleVector3Persistent.Create;
66 FForcePersistent.InternalGetValue := {$ifdef FPC}@{$endif}GetForceForPersistent;
67 FForcePersistent.InternalSetValue := {$ifdef FPC}@{$endif}SetForceForPersistent;
68 FForcePersistent.InternalDefaultValue := Force; // current value is default
69 end;
70
71 destructor TAddForceBehavior.Destroy;
72 begin
73 FreeAndNil(FForcePersistent);
74 inherited;
75 end;
76
77 procedure TAddForceBehavior.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.AddForce(Force, false);
91 RigidBody.WakeUp;
92 end;
93 end;
94
95 function TAddForceBehavior.PropertySections(const PropertyName: String
96 ): TPropertySections;
97 begin
98 if PropertyName = 'Force' then
99 Result := [psBasic]
100 else
101 Result := inherited PropertySections(PropertyName);
102 end;
103
104 initialization
105 RegisterSerializableComponent(TAddForceBehavior, 'Add Force Behavior');
106 end.