| 1 |
{%MainUnit castletransform.pas} |
| 2 |
{ |
| 3 |
Copyright 2020-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
{ TCastleBehavior. } |
| 17 |
|
| 18 |
{$ifdef read_interface} |
| 19 |
TCastleBehaviorClass = class of TCastleBehavior; |
| 20 |
|
| 21 |
{ Behaviors can be attached to TCastleTransform to perform specific logic, |
| 22 |
for example implement creature movement. |
| 23 |
This implements a simple component-system for TCastleTransform. |
| 24 |
Using behaviors allows to attach independent functionality like |
| 25 |
TCastleBillboard, TCastleSoundSource, creature AI and more. |
| 26 |
CGE provides implementation for some behaviors, and you can create your own too. |
| 27 |
|
| 28 |
You implemement a descendant of TCastleBehavior, typically overriding |
| 29 |
its @link(Update) method, and add it to TCastleTransform by @link(TCastleTransform.AddBehavior). |
| 30 |
Inside TCastleBehavior, access the TCastleTransform instances by @link(Parent). |
| 31 |
|
| 32 |
The API of TCastleBehavior is deliberately a subset of the TCastleTransform API, |
| 33 |
for example both have @link(Update) method. |
| 34 |
Using TCastleBehavior is just simpler and more efficient, as TCastleBehavior doesn't have |
| 35 |
own transformation, children and it cannot render something. |
| 36 |
If this is too limiting, remember you can always implement more powerful behaviors |
| 37 |
by creating TCastleTransform descendants instead of TCastleBehavior descendants, |
| 38 |
and accessing the @link(TCastleTransform.Parent) from a child. |
| 39 |
} |
| 40 |
TCastleBehavior = class(TCastleComponent) |
| 41 |
strict private |
| 42 |
FListenWorldChange: Boolean; |
| 43 |
procedure SetListenWorldChange(const Value: Boolean); |
| 44 |
private |
| 45 |
FParent: TCastleTransform; |
| 46 |
protected |
| 47 |
{ Continuously occuring event, for various tasks. |
| 48 |
@param(RemoveMe Set this to rtRemove or rtRemoveAndFree to remove |
| 49 |
this component from @link(Parent) after this call finished. |
| 50 |
rtRemoveAndFree additionally will free this item. |
| 51 |
Initially it's rtNone when this method is called.) } |
| 52 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); virtual; |
| 53 |
|
| 54 |
{ Event called right before rendering the parent TCastleTransform. |
| 55 |
|
| 56 |
Override this is you have to adjust the parent transformation strictly |
| 57 |
before the rendering, and doing it in @link(Update) is not enough. |
| 58 |
|
| 59 |
E.g. this makes sense to implement billboard, because user must never |
| 60 |
see artifacts caused by billboard not perfectly aligned with |
| 61 |
the camera (blending sort along camera direction in sort3D is not perfect |
| 62 |
if billboard doesn't exactly match camera direction). |
| 63 |
|
| 64 |
Overriding this also allows to adjust transformation before rendering |
| 65 |
each occurrence of TCastleTransform in the viewport tree. |
| 66 |
This matters if given TCastleTransform may occur multiple times |
| 67 |
in the viewport tree, directly or through @link(TCastleTransformReference). |
| 68 |
This also matters if given TCastleTransform may be present in multiple |
| 69 |
viewports (see https://castle-engine.io/multiple_viewports_to_display_one_world). |
| 70 |
|
| 71 |
@param( |
| 72 |
ParentParentWorldTransformation |
| 73 |
|
| 74 |
World transformation of the parent of the parent of this behavior. |
| 75 |
The name is kind of convoluted, but consistent: |
| 76 |
When the parent of this behavior has exactly one parent TCastleTransform |
| 77 |
(not zero, not more) then ParentParentWorldTransformation.Transform |
| 78 |
is equivalent to Parent.Parent.WorldTransform. |
| 79 |
) |
| 80 |
|
| 81 |
@param( |
| 82 |
ParentWorldTransformation |
| 83 |
|
| 84 |
World transformation of the parent of this behavior. |
| 85 |
ParentWorldTransformation.Transform is equal to |
| 86 |
Parent.WorldTransform, if the Parent exists exactly |
| 87 |
once in the viewport. |
| 88 |
) |
| 89 |
|
| 90 |
@param(CameraView |
| 91 |
Current camera vectors, in world coordinates, used to render the scene |
| 92 |
now.) |
| 93 |
} |
| 94 |
procedure BeforeRender( |
| 95 |
const CameraView: TViewVectors; |
| 96 |
const ParentParentWorldTransformation, ParentWorldTransformation: TTransformation); virtual; |
| 97 |
|
| 98 |
{ Called after @link(Parent) changed, at the end of @link(TCastleTransform.AddBehavior), |
| 99 |
@link(TCastleTransform.RemoveBehavior). } |
| 100 |
procedure ParentChanged; virtual; |
| 101 |
deprecated 'Use ParentAfterAttach / ParentBeforeDetach or WorldAfterAttach / WorldBeforeDetach'; |
| 102 |
|
| 103 |
{ Called after @link(Parent) changed, e.g. at the end of @link(TCastleTransform.AddBehavior). } |
| 104 |
procedure ParentAfterAttach; virtual; |
| 105 |
|
| 106 |
{ Called before @link(Parent) changed, e.g. at the beginning of @link(TCastleTransform.RemoveBehavior). |
| 107 |
|
| 108 |
This is also called at the destruction of behavior (more precisely from BeforeDestruction now). |
| 109 |
Even in this case, we guarantee that @link(Parent) is still valid during this call, |
| 110 |
and not in half-destroyed state. |
| 111 |
Before the actual destructor of TCastleBehavior we do BeforeParentDetach and BeforeWorldDetach |
| 112 |
and set Parent to @nil. } |
| 113 |
procedure ParentBeforeDetach; virtual; |
| 114 |
|
| 115 |
{ Called after @link(Parent) became part of @link(World). |
| 116 |
Called only if ListenWorldChange is @true. } |
| 117 |
procedure WorldAfterAttach; virtual; |
| 118 |
|
| 119 |
{ Called before @link(Parent) stops being part of @link(World). |
| 120 |
Called only if ListenWorldChange is @true. |
| 121 |
|
| 122 |
This is also called at the destruction of behavior (more precisely from BeforeDestruction now). |
| 123 |
Even in this case, we guarantee that @link(Parent) is still valid during this call, |
| 124 |
and not in half-destroyed state. |
| 125 |
Before the actual destructor of TCastleBehavior we do BeforeParentDetach and BeforeWorldDetach |
| 126 |
and set Parent to @nil. } |
| 127 |
procedure WorldBeforeDetach; virtual; |
| 128 |
|
| 129 |
{ Check can this behavior be added to NewParent. |
| 130 |
When this returns @false, it has to set also ReasonWhyCannot. |
| 131 |
When overriding this, you can use e.g. this code to make sure we are the only |
| 132 |
behavior of given class: |
| 133 |
|
| 134 |
@longCode(# |
| 135 |
function TCastleBillboard.CanAttachToParent(const NewParent: TCastleTransform; |
| 136 |
out ReasonWhyCannot: String): Boolean; |
| 137 |
begin |
| 138 |
Result := inherited; |
| 139 |
if not Result then Exit; |
| 140 |
|
| 141 |
if NewParent.FindBehavior(TCastleBillboard) <> nil then |
| 142 |
begin |
| 143 |
ReasonWhyCannot := 'Only one TCastleBillboard behavior can be added to a given TCastleTransform'; |
| 144 |
Result := false; |
| 145 |
end; |
| 146 |
end; |
| 147 |
#) |
| 148 |
} |
| 149 |
function CanAttachToParent(const NewParent: TCastleTransform; |
| 150 |
out ReasonWhyCannot: String): Boolean; virtual; |
| 151 |
public |
| 152 |
procedure BeforeDestruction; override; |
| 153 |
|
| 154 |
(* |
| 155 |
// Not used in the end, for now the TransformDesigning and these methods are commented out. |
| 156 |
|
| 157 |
{ Create design-time things that are not displayed in the hierarchy |
| 158 |
so can be created after behavior selection in editor hierarchy } |
| 159 |
procedure DesigningBegin; virtual; |
| 160 |
|
| 161 |
{ Destroy design-time stuff. } |
| 162 |
procedure DesigningEnd; virtual; |
| 163 |
*) |
| 164 |
|
| 165 |
function World: TCastleAbstractRootTransform; |
| 166 |
|
| 167 |
{ Parent TCastleTransform of this behavior. |
| 168 |
Change it by doing @link(TCastleTransform.AddBehavior), |
| 169 |
@link(TCastleTransform.RemoveBehavior). |
| 170 |
|
| 171 |
@nil if this behavior is not yet added to a parent. } |
| 172 |
property Parent: TCastleTransform read FParent; |
| 173 |
|
| 174 |
{ Set this to @true to receive WorldBeforeDetach, WorldAfterAttach calls. } |
| 175 |
property ListenWorldChange: Boolean |
| 176 |
read FListenWorldChange write SetListenWorldChange; |
| 177 |
end; |
| 178 |
|
| 179 |
TCastleBehaviorList = {$ifdef FPC}specialize{$endif} TObjectList<TCastleBehavior>; |
| 180 |
|
| 181 |
{$endif read_interface} |
| 182 |
|
| 183 |
{$ifdef read_implementation} |
| 184 |
|
| 185 |
{ TCastleTransform.TCastleBehaviorEnumerator ------------------------------------------------- } |
| 186 |
|
| 187 |
{ TCastleBehaviorEnumerator is optimized to be a record, following |
| 188 |
https://hallvards.blogspot.com/2007/10/more-fun-with-enumerators.html } |
| 189 |
|
| 190 |
constructor TCastleTransform.TCastleBehaviorEnumerator.Create(const AParent: TCastleTransform); |
| 191 |
begin |
| 192 |
// inherited Create; |
| 193 |
FParent := AParent; |
| 194 |
FPosition := -1; |
| 195 |
end; |
| 196 |
|
| 197 |
function TCastleTransform.TCastleBehaviorEnumerator.GetCurrent: TCastleBehavior; |
| 198 |
begin |
| 199 |
Result := FParent.Behaviors[FPosition]; |
| 200 |
end; |
| 201 |
|
| 202 |
function TCastleTransform.TCastleBehaviorEnumerator.MoveNext: Boolean; |
| 203 |
begin |
| 204 |
Inc(FPosition); |
| 205 |
Result := FPosition < FParent.BehaviorsCount; |
| 206 |
end; |
| 207 |
|
| 208 |
function TCastleTransform.TCastleBehaviorEnumerator.GetEnumerator: TCastleBehaviorEnumerator; |
| 209 |
begin |
| 210 |
// Returns itself. See https://wiki.freepascal.org/for-in_loop |
| 211 |
Result := Self; |
| 212 |
end; |
| 213 |
|
| 214 |
{ TCastleBehavior ------------------------------------------------------------ } |
| 215 |
|
| 216 |
procedure TCastleBehavior.SetListenWorldChange(const Value: Boolean); |
| 217 |
begin |
| 218 |
if FListenWorldChange <> Value then |
| 219 |
begin |
| 220 |
FListenWorldChange := Value; |
| 221 |
if Parent <> nil then |
| 222 |
begin |
| 223 |
if Value then |
| 224 |
Parent.AddWorldChangeNotification(Self) |
| 225 |
else |
| 226 |
Parent.RemoveWorldChangeNotification(Self) |
| 227 |
end; |
| 228 |
end; |
| 229 |
end; |
| 230 |
|
| 231 |
procedure TCastleBehavior.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 232 |
begin |
| 233 |
end; |
| 234 |
|
| 235 |
procedure TCastleBehavior.BeforeRender( |
| 236 |
const CameraView: TViewVectors; |
| 237 |
const ParentParentWorldTransformation, ParentWorldTransformation: TTransformation); |
| 238 |
begin |
| 239 |
end; |
| 240 |
|
| 241 |
procedure TCastleBehavior.ParentChanged; |
| 242 |
begin |
| 243 |
|
| 244 |
end; |
| 245 |
|
| 246 |
procedure TCastleBehavior.ParentAfterAttach; |
| 247 |
begin |
| 248 |
|
| 249 |
end; |
| 250 |
|
| 251 |
procedure TCastleBehavior.ParentBeforeDetach; |
| 252 |
begin |
| 253 |
|
| 254 |
end; |
| 255 |
|
| 256 |
procedure TCastleBehavior.WorldAfterAttach; |
| 257 |
begin |
| 258 |
|
| 259 |
end; |
| 260 |
|
| 261 |
procedure TCastleBehavior.WorldBeforeDetach; |
| 262 |
begin |
| 263 |
|
| 264 |
end; |
| 265 |
|
| 266 |
function TCastleBehavior.CanAttachToParent(const NewParent: TCastleTransform; |
| 267 |
out ReasonWhyCannot: String): Boolean; |
| 268 |
begin |
| 269 |
ReasonWhyCannot := ''; |
| 270 |
Result := true; |
| 271 |
end; |
| 272 |
|
| 273 |
procedure TCastleBehavior.BeforeDestruction; |
| 274 |
begin |
| 275 |
(* |
| 276 |
// The TransformDesigning mechanism works, but it unused for now. |
| 277 |
DesigningEnd; |
| 278 |
*) |
| 279 |
|
| 280 |
{ Imagine this: |
| 281 |
|
| 282 |
TMyBehavior = class(TCastleBehavior) |
| 283 |
MyStuff: TObject; |
| 284 |
constructor Create; |
| 285 |
destructor Destroy; override; |
| 286 |
procedure BeforeParentDetach; override; |
| 287 |
end; |
| 288 |
|
| 289 |
constructor TMyBehavior.Create; |
| 290 |
begin |
| 291 |
inherited; |
| 292 |
MyStuff := TObject.Create; |
| 293 |
end; |
| 294 |
|
| 295 |
destructor TMyBehavior.Destroy; |
| 296 |
begin |
| 297 |
FreeAndNil(MyStuff); |
| 298 |
inherited; |
| 299 |
end; |
| 300 |
|
| 301 |
procedure TMyBehavior.BeforeParentDetach; |
| 302 |
begin |
| 303 |
MyStuff.Xxx := ...; // <- this is safe, because BeforeParentDetach is done from BeforeBehaviorDestroy |
| 304 |
inherited; |
| 305 |
end; |
| 306 |
|
| 307 |
var |
| 308 |
B: TMyBehavior; |
| 309 |
begin |
| 310 |
.. |
| 311 |
FreeAndNil(B); |
| 312 |
end; |
| 313 |
|
| 314 |
Thanks to calling Parent.BeforeBehaviorDestroy in TCastleBehavior.BeforeDestruction |
| 315 |
(and not TCastleBehavior.Destroy) accessing MyStuff in TMyBehavior.BeforeParentDetach |
| 316 |
is safe. |
| 317 |
} |
| 318 |
|
| 319 |
if Parent <> nil then |
| 320 |
begin |
| 321 |
Parent.BeforeBehaviorDestroy(Self); |
| 322 |
Assert(Parent = nil); |
| 323 |
end; |
| 324 |
|
| 325 |
inherited BeforeDestruction; |
| 326 |
end; |
| 327 |
|
| 328 |
function TCastleBehavior.World: TCastleAbstractRootTransform; |
| 329 |
begin |
| 330 |
if Parent <> nil then |
| 331 |
Result := Parent.World |
| 332 |
else |
| 333 |
Result := nil; |
| 334 |
end; |
| 335 |
|
| 336 |
{$endif read_implementation} |