Debian Codegraph @

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