Debian Codegraph @

src/deprecated_units/castletransformextra.pas

raw
1 {
2 Copyright 2010-2022 Michalis Kamburelis.
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 { Additional classes derived from TCastleTransform (TCastleAlive, TCastleMoving...). }
17 unit CastleTransformExtra deprecated 'use CastleTransform unit for TCastleTransformDesign; implement TCastleMoving and friends in your own application';
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses SysUtils, Classes, Math, Generics.Collections,
24 CastleVectors, CastleFrustum, CastleBoxes, CastleClassUtils, CastleKeysMouse,
25 CastleRectangles, CastleUtils, CastleTimeUtils,
26 CastleSoundEngine, CastleSectors, CastleCameras, CastleTriangles,
27 CastleTransform, CastleBehaviors;
28
29 type
30 { Transformation moving and potentially pushing other objects.
31 Good for elevators, doors and such.
32
33 Other objects may be pushed, if @link(Pushes).
34 There are two methods of pushing available, see @link(PushesEverythingInside).
35 Only the objects with @link(TCastleTransform.CollidesWithMoving) are ever pushed by
36 this object (the rest of the world is treated as static, does not interact with
37 elevators / doors or such).
38
39 You can also stop/reverse the move to prevent some collisions
40 from occuring at all. This way you can e.g. prevent the door
41 from automatically closing, if someone/something blocks the way.
42 You do this by overriding BeforeTimeIncrease.
43 See TDoomLevelDoor.BeforeTimeIncrease in "The Castle" for example how to
44 do this. }
45 TCastleMoving = class(TCastleTransform)
46 private
47 FPushes: boolean;
48 FPushesEverythingInside: boolean;
49 FAnimationTime: TFloatTime;
50 protected
51 { Local object time, always increasing, used to track animations. }
52 property AnimationTime: TFloatTime read FAnimationTime;
53
54 function GetTranslationFromTime(const AnAnimationTime: TFloatTime):
55 TVector3; virtual; abstract;
56
57 { Do something right before animation progresses.
58 Called at the beginning of our @link(Update),
59 @italic(right before) AnimationTime changes to NewAnimationTime.
60
61 Useful for taking care of collision detection issues,
62 as our assumption always is that "nothing collides". Which means
63 that if you don't want your TCastleMoving to collide
64 with e.g. player or creatures or items, then you should
65 prevent the collision @italic(before it happens).
66 This is the place to do it. }
67 procedure BeforeTimeIncrease(const NewAnimationTime: TFloatTime); virtual;
68 public
69 constructor Create(AOwner: TComponent); override;
70 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
71 published
72 { Are other 3D objects pushed when this object moves.
73 Only the 3D objects with @link(TCastleTransform.CollidesWithMoving) are ever pushed by this object
74 (the rest of 3D world is treated as static, does not interact with
75 elevators / doors or such).
76
77 Only relevant if CheckCollides. Non-colliding objects never push others. }
78 property Pushes: boolean read FPushes write FPushes default true;
79
80 { If @link(Pushes) is @true, this determines how pushing actually works.
81 There two methods:
82
83 @orderedList(
84 @item(PushesEverythingInside = @true: We move every
85 3D object that is inside our bounding box and has CollidesWithMoving=@true.
86 This is sensible if we can reasonably assume that things
87 inside our box are standing. For example if this is
88 a (vertical or horizontal) elevator, then creatures/items
89 are usually standing/lying inside, and naturally move with
90 the same speed (and direction) as the elevator.)
91
92 @item(When PushesEverythingInside = @false: We check precise
93 collision between 3D objects with CollidesWithMoving=@true
94 and our triangle mesh.
95 We use TCastleTransform.BoxCollision / TCastleTransform.SphereCollsion
96 that can check collisions with TCastleScene precisely.)
97 )
98
99 Neither method is really perfect.
100
101 PushesEverythingInside = @false seems like a more precise check,
102 as it actually compares the triangle mesh, taking into account
103 the interior of (this) moving 3D object. PushesEverythingInside = @true
104 just approximates the moving 3D object by it's bounding box.
105
106 On the other hand, PushesEverythingInside = @true makes the elevator
107 more "sticky". With PushesEverythingInside = @false,
108 when player hits the floor, it takes them some time to raise up.
109 This creates a "bouncing camera" effect when the elevator goes up
110 quickly: player constantly falls to the ground, tries to get up,
111 but elevator moves up and player falls to it's ground again.
112 When the elevator goes down, the player/creature constantly falls
113 down on it because of gravity, which again causes artifacts
114 as gravity may work significantly slower/faster than elavator moving speed.
115 When the elevator is a horizontal moving platform, it will "slip"
116 from under the player/creature, leaving the poor fella suddenly hanging
117 in the air, and falling down because of gravity in the next second.
118
119 In practice: PushesEverythingInside should be @true for small
120 containers, when you can reasonably assume that things (creatures,
121 player, items) stand inside, and when you intend to use it for transport
122 of 3D stuff. For very large moving stuff, that possibly
123 interacts with flying players/creatures in some creative way,
124 PushesEverythingInside may be @false. }
125 property PushesEverythingInside: boolean
126 read FPushesEverythingInside write FPushesEverythingInside default true;
127 end deprecated 'the usefullness of this class is low, as in a typical application you can implement something similar yourself, adjusted to your particular case';
128
129 { Transform moving with constant speed between 2 points.
130 Moves with a constant speed from (0, 0, 0) to TranslationEnd.
131 They are called @italic(begin position) and @italic(end position).
132
133 This is a simplified, more comfortable descendant of TCastleMoving.
134 You get easy to use GoBeginPosition, GoEndPosition
135 properties, you can easily set sounds by SoundGoBeginPosition and
136 SoundGoEndPosition and such. }
137 TCastleLinearMoving = class(TCastleMoving)
138 private
139 FEndPosition: boolean;
140 FEndPositionStateChangeTime: Single;
141
142 FSoundGoBeginPosition: TCastleSound;
143 FSoundGoEndPosition: TCastleSound;
144 FSoundGoBeginPositionLooping: boolean;
145 FSoundGoEndPositionLooping: boolean;
146 FSoundTracksCurrentPosition: boolean;
147
148 SoundSourceTransform: TCastleTransform;
149 SoundSource: TCastleSoundSource;
150 procedure PlaySound(const SoundType: TCastleSound; const Looping: boolean);
151 public
152 constructor Create(AOwner: TComponent); override;
153 destructor Destroy; override;
154
155 { Is this object in @italic(end position), or going to it.
156 If @false, then this object is in @italic(begin position)
157 or going to it. See also CompletelyEndPosion and CompletelyBeginPosition.
158
159 Initially this is @false, and EndPositionStateChangeTime is set such that
160 we're sure that we're in CompletelyBeginPosion, }
161 property EndPosition: boolean read FEndPosition;
162
163 { Last time EndPosition changed. }
164 property EndPositionStateChangeTime: Single read FEndPositionStateChangeTime;
165
166 function CompletelyEndPosition: boolean;
167 function CompletelyBeginPosition: boolean;
168
169 { Start going to @italic(begin position), assuming that
170 currently we're in @italic(end position) (i.e. CompletelyEndPosion). }
171 procedure GoBeginPosition;
172
173 { Start going to @italic(end position), assuming that
174 currently we're in @italic(begin position) (i.e. CompletelyBeginPosion). }
175 procedure GoEndPosition;
176
177 { Stop going from @italic(end position) to @italic(begin position)
178 and go back to @italic(end position). Call this only when currently
179 EndPosition is @false and we were in the middle of going to
180 @italic(begin position).
181
182 As an example, this is what happens when door on DOOM level gets blocked.
183 In the middle of closing (which is going to @italic(begin position))
184 it will realize that something blocks it, and open back
185 (go back to @italic(end position)). }
186 procedure RevertGoEndPosition;
187
188 { Just like RevertGoEndPosition, but this should be used in the middle
189 of the move from @italic(begin position) to @italic(end position),
190 to go back to @italic(begin position). }
191 procedure RevertGoBeginPosition;
192
193 { This goes to the @italic(other) position.
194 Which means that if we're completely in @italic(end position)
195 or in the middle of move to @italic(end position), this goes
196 back to @italic(begin position). And if we're in @italic(begin position),
197 this goes back to @italic(end position). }
198 procedure GoOtherPosition;
199
200 property SoundGoBeginPosition: TCastleSound
201 read FSoundGoBeginPosition write FSoundGoBeginPosition;
202 property SoundGoEndPosition: TCastleSound
203 read FSoundGoEndPosition write FSoundGoEndPosition;
204
205 property SoundGoBeginPositionLooping: boolean
206 read FSoundGoBeginPositionLooping write FSoundGoBeginPositionLooping
207 default false;
208 property SoundGoEndPositionLooping: boolean
209 read FSoundGoEndPositionLooping write FSoundGoEndPositionLooping
210 default false;
211
212 { If @true then the sound (set by SoundGoBeginPosition or
213 SoundGoEndPosition) 3D position changes as the 3D position of the object
214 changes.
215
216 Otherwise (default) sound is initially made at initial
217 3D position of this object, and then the sound position doesn't change
218 (even if the position of the object changes). }
219 property SoundTracksCurrentPosition: boolean
220 read FSoundTracksCurrentPosition write FSoundTracksCurrentPosition
221 default false;
222 public
223 MoveTime: Single;
224 TranslationEnd: TVector3;
225
226 function GetTranslationFromTime(const AnAnimationTime: TFloatTime):
227 TVector3; override;
228
229 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
230 end deprecated 'the usefullness of this class is low, as in a typical application you can implement something similar yourself, adjusted to your particular case';
231
232 { Transform representing an alive thing. Basis for players, creatures and everything
233 else that has some position, direction and that can be killed.
234
235 Note that the TCastleAlive doesn't remove dead objects, doesn't make any
236 dead animations or such. TCastleAlive class merely keeps track of
237 @link(Life), @link(Dead) and such properties,
238 and allows you to call @link(Hurt) doing eventual knockback.
239 If your own code doesn't call @link(Hurt),
240 or even doesn't look at @link(Life) value, then they have no implication
241 for given 3D object, so it may be indestructible just like other 3D objects. }
242 TCastleAlive = class(TCastleTransform)
243 private
244 FLifeTime: Single;
245 FDieTime: Single;
246 FLife: Single;
247 FMaxLife: Single;
248 { FKnockbackDistance <= 0 means "no knockback currently" }
249 FKnockbackDistance: Single;
250 FLastHurtDirection: TVector3;
251 { Same as LastHurtDirection but (for things with Gravity) flattened
252 to be orthogonal to World.Gravity. This prevents from "pushing" creatures
253 into the floor by hitting them in downward direction, which is often
254 too easy for non-flying creatures that have Sphere with Middle point high. }
255 FLastHurtDirectionGround: TVector3;
256 FKnockBackSpeed: Single;
257 protected
258 procedure SetLife(const Value: Single); virtual;
259 procedure CancelKnockback;
260 public
261 const
262 DefaultKnockBackSpeed = 1.0;
263
264 constructor Create(AOwner: TComponent); override;
265
266 { Shortcut for checking Life <= 0. }
267 function Dead: boolean;
268
269 { Hurt given creature, decreasing it's life by LifeLoss,
270 setting last attack direction (used by knockback and some other effects),
271 optionally doing a knockback.
272 If all you want to do is to decrease Life, you can also just set @link(Life)
273 property. Unless your code depends on LastHurtDirection being always updated
274 (only TCreature in CastleCreatures unit depends on it now).
275
276 HurtDirection should be a normalized vector indicating direction
277 in which the attack came.
278
279 AKnockbackDistance, if non-zero, indicates to push creature by given
280 length in the direction given by HurtDirection.
281 Ignored if HurtDirection is zero.
282
283 Attacker is the other alive creature that caused this damage. It may be @nil
284 if no other TCastleAlive is directly responsible for this damage. This may
285 be useful for various purposes, for example the victim may become aware
286 of attacker presence when it's attacked. }
287 procedure Hurt(const LifeLoss: Single;
288 const HurtDirection: TVector3;
289 const AKnockbackDistance: Single; const Attacker: TCastleAlive); virtual;
290
291 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
292
293 { Direction from where the attack came.
294 Zero if there was no specific direction of last attack,
295 otherwise a normalized (length 1) vector. }
296 property LastHurtDirection: TVector3 read FLastHurtDirection;
297
298 property LifeTime: Single read FLifeTime;
299
300 { Time of death, only valid if @link(Dead), taken from LifeTime. }
301 property DieTime: Single read FDieTime;
302 published
303 { Current Life. We're dead when this is <= 0. }
304 property Life: Single read FLife write SetLife;
305
306 { Maximum amount of life. Used as default value for Life when sensible.
307 Can be also used for information (to display on player HUDs and such).
308
309 It's not really a limit, that is you can set Life
310 to something larger than MaxLife if you want. It's normal in some games,
311 where you can get some "magic life boost" that makes your health temporarily
312 larger than normal. Whether it's sensible in your game (and whether your
313 HUD will display it sensibly) is up to you. }
314 property MaxLife: Single read FMaxLife write FMaxLife;
315
316 { Scales how far the knockback effect pushes this creature/player. }
317 property KnockBackSpeed: Single read FKnockBackSpeed write FKnockBackSpeed
318 {$ifdef FPC} default DefaultKnockBackSpeed{$endif};
319 end deprecated 'use TCastleAliveBehavior, or implement "being alive" logic in your own game';
320
321 TCastleTransformDesign = CastleTransform.TCastleTransformDesign;
322
323 const
324 { Default values common to TPlayer and TCreature classes.
325 @groupBegin }
326 DefaultFallMinHeightToDamage = 5.0;
327 DefaultFallDamageScaleMin = 0.8;
328 DefaultFallDamageScaleMax = 1.2;
329 { @groupEnd }
330
331 implementation
332
333 uses CastleComponentSerialize, CastleLog, CastleUriUtils;
334
335 { TCastleMoving --------------------------------------------------------- }
336
337 { TODO: this browses World list, doesn't take into acount CollidesWithMoving items
338 that may be inside a sublist. }
339
340 constructor TCastleMoving.Create(AOwner: TComponent);
341 begin
342 inherited;
343 FPushes := true;
344 FPushesEverythingInside := true;
345 FAnimationTime := 0;
346 end;
347
348 { Note: When pushing the creature/player/item, right now
349 we don't check whether the creature/player/item will not be
350 pushed into collision with something else.
351
352 For now, design your level to minimize the chance that it will ever happen.
353 Although in theory you cannot design your level to guarantee
354 that it will never happen (because e.g. a creature may be pushed
355 into collision with other creature, and since creatures move
356 on their own they can arrange themselves (in theory) in all manners of
357 funny configurations...). But in practice it's not so difficult,
358 just make sure that there is enough space on the way of move.
359 }
360
361 procedure TCastleMoving.BeforeTimeIncrease(
362 const NewAnimationTime: TFloatTime);
363
364 function BoundingBoxAssumeTranslation(
365 const AssumeTranslation: TVector3): TBox3D;
366 begin
367 if CheckCollides then
368 Result := (inherited BoundingBox).Translate(AssumeTranslation) else
369 Result := TBox3D.Empty;
370 end;
371
372 function SphereCollisionAssumeTranslation(
373 const AssumeTranslation: TVector3;
374 const Pos: TVector3; const Radius: Single;
375 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
376 begin
377 Result := CheckCollides;
378 if Result then
379 begin
380 { We use the same trick as in TCastleTransform.MoveCollision to
381 use "inherited SphereCollsion" with Translation. }
382
383 Result := inherited SphereCollision(
384 Pos - AssumeTranslation, Radius, TrianglesToIgnoreFunc);
385 end;
386 end;
387
388 function BoxCollisionAssumeTranslation(
389 const AssumeTranslation: TVector3;
390 const Box: TBox3D;
391 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
392 begin
393 Result := CheckCollides;
394 if Result then
395 begin
396 { We use the same trick as in TCastleTransform.MoveCollision to
397 use "inherited BoxCollision" with Translation. }
398
399 Result := inherited BoxCollision(
400 Box.AntiTranslate(AssumeTranslation), TrianglesToIgnoreFunc);
401 end;
402 end;
403
404 var
405 CurrentBox, NewBox, Box: TBox3D;
406 I: Integer;
407 MoveTranslation: TVector3;
408 CurrentTranslation, NewTranslation: TVector3;
409 SphereRadius: Single;
410 Item: TCastleTransform;
411 begin
412 if CheckCollides and Pushes then
413 begin
414 CurrentTranslation := GetTranslationFromTime(AnimationTime);
415 NewTranslation := GetTranslationFromTime(NewAnimationTime);
416
417 { It often happens that TCastleMoving doesn't move at all,
418 and then MoveTranslation doesn't change at all
419 (even when compared precisely, without usual epsilon used to compare
420 floats). So the check below may be worth the time, we expect
421 it will avoid doing actual work. }
422
423 if not TVector3.PerfectlyEquals(CurrentTranslation, NewTranslation) then
424 begin
425 MoveTranslation := NewTranslation - CurrentTranslation;
426
427 { TODO: it may be sensible to add a pushing method when we compare
428 other object's bounding box (never a sphere, and be sure to use
429 the "tall" box for player, including it's legs) with octree
430 (that is, using inherited BoxCollision).
431 This can have the advantages of both PushesEverythingInside=true
432 (reacts more sticky, more eager to move colliding stuff with
433 the same speed as elevator)
434 and PushesEverythingInside=false (takes into account triangle mesh,
435 not just our bounding volume). }
436
437 if PushesEverythingInside then
438 begin
439 CurrentBox := BoundingBox;
440 NewBox := BoundingBoxAssumeTranslation(NewTranslation);
441 for I := 0 to World.Count - 1 do
442 begin
443 Item := World[I];
444 if (Item is TCastleTransform) and Item.CollidesWithMoving then
445 begin
446 { This case doesn't really use Item.Sphere. But it's not really
447 terribly important design decision, we may use Item.Sphere
448 one day here. It's most comfortable to just use
449 here Item.BoundingBox, as we perform collisions with our box. }
450 Box := Item.BoundingBox;
451 if Box.Collision(NewBox) or
452 Box.Collision(CurrentBox) then
453 TCastleTransform(Item).Translate(MoveTranslation);
454 end;
455 end;
456 end else
457 begin
458 for I := 0 to World.Count - 1 do
459 begin
460 Item := World[I];
461 if (Item is TCastleTransform) and Item.CollidesWithMoving then
462 if Item.Sphere(SphereRadius) then
463 begin
464 if SphereCollisionAssumeTranslation(NewTranslation,
465 Item.Middle, SphereRadius, nil) then
466 TCastleTransform(Item).Translate(MoveTranslation);
467 end else
468 begin
469 if BoxCollisionAssumeTranslation(NewTranslation,
470 Item.BoundingBox, nil) then
471 TCastleTransform(Item).Translate(MoveTranslation);
472 end;
473 end;
474 end;
475 end;
476 end;
477 end;
478
479 procedure TCastleMoving.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType);
480 var
481 NewAnimationTime: TFloatTime;
482 begin
483 inherited;
484
485 NewAnimationTime := AnimationTime + SecondsPassed;
486 BeforeTimeIncrease(NewAnimationTime);
487 FAnimationTime := NewAnimationTime;
488
489 Translation := GetTranslationFromTime(AnimationTime);
490 end;
491
492 { TCastleLinearMoving --------------------------------------------------- }
493
494 constructor TCastleLinearMoving.Create(AOwner: TComponent);
495 begin
496 inherited;
497
498 SoundSourceTransform := TCastleTransform.Create(Self);
499 Add(SoundSourceTransform);
500
501 SoundSource := TCastleSoundSource.Create(Self);
502 SoundSourceTransform.AddBehavior(SoundSource);
503
504 FEndPosition := false;
505
506 { We set FEndPositionStateChangeTime to a past time, to be sure
507 that we don't treat the door as "closing right now". }
508 FEndPositionStateChangeTime := -1000.0; { TODO: should be implemented better... }
509 end;
510
511 destructor TCastleLinearMoving.Destroy;
512 begin
513 inherited;
514 end;
515
516 procedure TCastleLinearMoving.PlaySound(const SoundType: TCastleSound;
517 const Looping: boolean);
518 var
519 PlayingSound: TCastlePlayingSoundSource;
520 begin
521 if Looping then
522 begin
523 SoundSource.Sound := SoundType;
524 end else
525 begin
526 SoundSource.Sound := nil;
527 if SoundType <> nil then
528 begin
529 PlayingSound := TCastlePlayingSoundSource.Create(nil);
530 PlayingSound.Sound := SoundType;
531 PlayingSound.FreeOnStop := true;
532 PlayingSound.Follow := SoundTracksCurrentPosition;
533 SoundSource.Play(PlayingSound);
534 end;
535 end;
536 end;
537
538 procedure TCastleLinearMoving.GoEndPosition;
539 begin
540 FEndPosition := true;
541 FEndPositionStateChangeTime := AnimationTime;
542 PlaySound(SoundGoEndPosition, SoundGoEndPositionLooping);
543 end;
544
545 procedure TCastleLinearMoving.GoBeginPosition;
546 begin
547 FEndPosition := false;
548 FEndPositionStateChangeTime := AnimationTime;
549 PlaySound(SoundGoBeginPosition, SoundGoBeginPositionLooping);
550 end;
551
552 procedure TCastleLinearMoving.RevertGoEndPosition;
553 begin
554 FEndPosition := true;
555 FEndPositionStateChangeTime := { AnimationTime -
556 (MoveTime - (AnimationTime - EndPositionStateChangeTime)) }
557 { simplified : }
558 2 * AnimationTime - MoveTime - EndPositionStateChangeTime;
559 PlaySound(SoundGoEndPosition, SoundGoEndPositionLooping);
560 end;
561
562 procedure TCastleLinearMoving.RevertGoBeginPosition;
563 begin
564 FEndPosition := false;
565 FEndPositionStateChangeTime := { AnimationTime -
566 (MoveTime - (AnimationTime - EndPositionStateChangeTime)) }
567 { simplified : }
568 2 * AnimationTime - MoveTime - EndPositionStateChangeTime;
569 PlaySound(SoundGoEndPosition, SoundGoBeginPositionLooping);
570 end;
571
572 procedure TCastleLinearMoving.GoOtherPosition;
573 begin
574 if CompletelyEndPosition then
575 GoBeginPosition else
576 if CompletelyBeginPosition then
577 GoEndPosition else
578 begin
579 if EndPosition then
580 RevertGoBeginPosition else
581 RevertGoEndPosition;
582 end;
583 end;
584
585 function TCastleLinearMoving.GetTranslationFromTime(
586 const AnAnimationTime: TFloatTime): TVector3;
587 begin
588 if not EndPosition then
589 begin
590 if AnAnimationTime - EndPositionStateChangeTime > MoveTime then
591 { Completely closed. }
592 Result := TVector3.Zero else
593 { During closing. }
594 Result := TranslationEnd *
595 (1 - (AnAnimationTime - EndPositionStateChangeTime) / MoveTime);
596 end else
597 begin
598 if AnAnimationTime - EndPositionStateChangeTime > MoveTime then
599 { Completely open. }
600 Result := TranslationEnd else
601 { During opening. }
602 Result := TranslationEnd *
603 ((AnAnimationTime - EndPositionStateChangeTime) / MoveTime);
604 end;
605 end;
606
607 function TCastleLinearMoving.CompletelyEndPosition: boolean;
608 begin
609 Result := EndPosition and
610 (AnimationTime - EndPositionStateChangeTime > MoveTime);
611 end;
612
613 function TCastleLinearMoving.CompletelyBeginPosition: boolean;
614 begin
615 Result := (not EndPosition) and
616 (AnimationTime - EndPositionStateChangeTime > MoveTime);
617 end;
618
619 procedure TCastleLinearMoving.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType);
620 var
621 B: TBox3D;
622 begin
623 inherited;
624
625 B := LocalBoundingBox;
626 if B.IsEmpty then
627 SoundSourceTransform.Translation := TVector3.Zero
628 else
629 SoundSourceTransform.Translation := B.Center;
630
631 { If the SoundGoBegin/EndPosition is longer than the MoveTime
632 (or it's looping),
633 stop this sound once we're completely in Begin/EndPosition. }
634 if AnimationTime - EndPositionStateChangeTime > MoveTime then
635 SoundSource.Sound := nil;
636 end;
637
638 { TCastleAlive ------------------------------------------------------------------- }
639
640 constructor TCastleAlive.Create(AOwner: TComponent);
641 begin
642 inherited;
643 KnockBackSpeed := 1.0;
644 { at the beginning we are Dead (Life = 0) and DieTime = LifeTime,
645 so everything is already in the correct state. }
646 end;
647
648 procedure TCastleAlive.SetLife(const Value: Single);
649 begin
650 if (FLife > 0) and (Value <= 0) then
651 FDieTime := LifeTime;
652 FLife := Value;
653 end;
654
655 function TCastleAlive.Dead: boolean;
656 begin
657 Result := Life <= 0;
658 end;
659
660 procedure TCastleAlive.Hurt(const LifeLoss: Single;
661 const HurtDirection: TVector3;
662 const AKnockbackDistance: Single; const Attacker: TCastleAlive);
663 begin
664 Life := Life - LifeLoss;
665 FKnockbackDistance := AKnockbackDistance;
666 FLastHurtDirection := HurtDirection;
667
668 { calculate FLastHurtDirectionGround }
669 FLastHurtDirectionGround := FLastHurtDirection;
670 if Gravity then
671 MakeVectorsOrthoOnTheirPlane(FLastHurtDirectionGround, World.GravityUp);
672 end;
673
674 procedure TCastleAlive.CancelKnockback;
675 begin
676 FKnockbackDistance := 0;
677 end;
678
679 procedure TCastleAlive.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType);
680 { Do the knockback effect, if it's currently active, by pushing
681 creature along last attack direction. }
682 var
683 CurrentKnockBackDistance: Single;
684 begin
685 inherited;
686 if not Exists then Exit;
687
688 FLifeTime := FLifeTime + SecondsPassed;
689
690 if FKnockbackDistance > 0 then
691 begin
692 { Calculate CurrentKnockBackDistance, update FKnockbackDistance }
693 CurrentKnockBackDistance := KnockBackSpeed * SecondsPassed;
694 if FKnockbackDistance < CurrentKnockBackDistance then
695 begin
696 CurrentKnockBackDistance := FKnockbackDistance;
697 FKnockbackDistance := 0;
698 end else
699 FKnockbackDistance := FKnockbackDistance - CurrentKnockBackDistance;
700
701 Move(FLastHurtDirectionGround * CurrentKnockBackDistance, false);
702 end;
703 end;
704
705 end.