Debian Codegraph @

examples/physics/physics_persistent_forces_components/code/abstracttimedurationbehavior.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 { TAbstractTimeDurationBehavior }
17 unit AbstractTimeDurationBehavior;
18
19 interface
20
21 uses
22 Classes, SysUtils,
23 CastleTransform, CastleBehaviors, CastleVectors, CastleClassUtils;
24
25 type
26 { Functions used in many physics simulation behaviors }
27 TAbstractTimeDurationBehavior = class(TCastleBehavior)
28 private
29 FStartTime: Single;
30 FExpiredTimeToStart: Single;
31 FDurationTime: Single;
32 FExpiredDurationTime: Single;
33 FOneShot: Boolean;
34 FWasShot: Boolean;
35 protected
36 { Checks Start Time expired }
37 function ShouldStart: Boolean;
38 { Checks Duration Time expired }
39 function ShouldStop: Boolean;
40 { Check should we run our code in Update }
41 function ShouldUpdate: Boolean;
42 { If OneShot = true make a shot }
43 procedure Shot;
44 public
45 constructor Create(AOwner: TComponent); override;
46
47 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
48
49 property WasShot: Boolean read FWasShot;
50
51 function PropertySections(const PropertyName: String): TPropertySections; override;
52
53 published
54 property OneShot: Boolean read FOneShot write FOneShot default false;
55 property DurationTime: Single read FDurationTime write FDurationTime {$ifdef FPC}default 10{$endif};
56 property StartTime: Single read FStartTime write FStartTime {$ifdef FPC}default 0{$endif};
57 end;
58
59 implementation
60
61 { TAbstractTimeDurationBehavior ---------------------------------------------- }
62
63 function TAbstractTimeDurationBehavior.ShouldStart: Boolean;
64 begin
65 Result := FExpiredTimeToStart >= FStartTime;
66 end;
67
68 function TAbstractTimeDurationBehavior.ShouldStop: Boolean;
69 begin
70 Result := FExpiredDurationTime >= FDurationTime;
71 end;
72
73 function TAbstractTimeDurationBehavior.ShouldUpdate: Boolean;
74 begin
75 if OneShot then
76 begin
77 if WasShot then
78 Exit(false)
79 else
80 Shot;
81 end else
82 if (not ShouldStart) or (ShouldStop) then
83 Exit(false);
84
85 Result := true;
86 end;
87
88 procedure TAbstractTimeDurationBehavior.Shot;
89 begin
90 FWasShot := true;
91 end;
92
93 constructor TAbstractTimeDurationBehavior.Create(AOwner: TComponent);
94 begin
95 inherited Create(AOwner);
96
97 FDurationTime := 10;
98 FStartTime := 0;
99 FExpiredTimeToStart := 0;
100 FExpiredDurationTime := 0;
101 end;
102
103 procedure TAbstractTimeDurationBehavior.Update(const SecondsPassed: Single;
104 var RemoveMe: TRemoveType);
105 begin
106 inherited Update(SecondsPassed, RemoveMe);
107
108 if ShouldStart then
109 FExpiredDurationTime := FExpiredDurationTime + SecondsPassed
110 else
111 FExpiredTimeToStart := FExpiredTimeToStart + SecondsPassed;
112 end;
113
114 function TAbstractTimeDurationBehavior.PropertySections(
115 const PropertyName: String): TPropertySections;
116 begin
117 if (PropertyName = 'OneShot') or
118 (PropertyName = 'DurationTime') or
119 (PropertyName = 'StartTime') then
120 Result := [psBasic]
121 else
122 Result := inherited PropertySections(PropertyName);
123 end;
124
125 end.
126