| 1 |
{ |
| 2 |
Copyright 2023-2023 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 |
{ TSubAnimations behavior to play subanimations on parent TCastleScene. } |
| 17 |
unit GameSubAnimations; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, Generics.Collections, |
| 22 |
CastleScene, CastleTimeUtils, CastleTransform; |
| 23 |
|
| 24 |
type |
| 25 |
{ Subanimation definition used with TSubAnimations. } |
| 26 |
TSubAnimation = class |
| 27 |
Name: String; |
| 28 |
BaseAnimation: String; |
| 29 |
Start: TFloatTime; |
| 30 |
Duration: TFloatTime; |
| 31 |
end; |
| 32 |
|
| 33 |
TSubAnimationsDictionary = {$ifdef FPC}specialize{$endif} TObjectDictionary<String, TSubAnimation>; |
| 34 |
|
| 35 |
{ Behavior to play subanimations on parent TCastleScene. |
| 36 |
|
| 37 |
Define subanimations using @link(Add). |
| 38 |
Play them with @link(Play). |
| 39 |
List them with @link(List). } |
| 40 |
TSubAnimations = class(TCastleBehavior) |
| 41 |
private |
| 42 |
FList: TSubAnimationsDictionary; |
| 43 |
FCurrentSubAnimation: TSubAnimation; |
| 44 |
FCurrentTime: TFloatTime; |
| 45 |
FLoop: Boolean; |
| 46 |
ParentScene: TCastleScene; |
| 47 |
procedure UpdateAnimationPose; |
| 48 |
protected |
| 49 |
procedure ParentAfterAttach; override; |
| 50 |
public |
| 51 |
constructor Create(AOwner: TComponent); override; |
| 52 |
destructor Destroy; override; |
| 53 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 54 |
|
| 55 |
{ Define subanimation for future use. |
| 56 |
The TSubAnimation instance given here becomes owned by this class, |
| 57 |
do not manage it anymore. } |
| 58 |
procedure Add(const SubAnimation: TSubAnimation); overload; |
| 59 |
|
| 60 |
{ Define subanimation for future use. } |
| 61 |
procedure Add(const SubAnimationName, BaseAnimation: String; |
| 62 |
const Start, Duration: TFloatTime); overload; |
| 63 |
|
| 64 |
{ List of the subanimations. Read-only. } |
| 65 |
property List: TSubAnimationsDictionary read FList; |
| 66 |
|
| 67 |
{ Start playing given subanimation, by name. } |
| 68 |
procedure Play(const SubAnimationName: String; const ALoop: Boolean); overload; |
| 69 |
|
| 70 |
{ Start playing given subanimation. } |
| 71 |
procedure Play(const SubAnim: TSubAnimation; const ALoop: Boolean); overload; |
| 72 |
end; |
| 73 |
|
| 74 |
implementation |
| 75 |
|
| 76 |
uses SysUtils, |
| 77 |
CastleUriUtils, CastleDownload, CastleStringUtils, CastleLog, CastleUtils; |
| 78 |
|
| 79 |
constructor TSubAnimations.Create(AOwner: TComponent); |
| 80 |
begin |
| 81 |
inherited; |
| 82 |
FList := TSubAnimationsDictionary.Create([doOwnsValues]); |
| 83 |
end; |
| 84 |
|
| 85 |
destructor TSubAnimations.Destroy; |
| 86 |
begin |
| 87 |
FreeAndNil(FList); |
| 88 |
inherited; |
| 89 |
end; |
| 90 |
|
| 91 |
procedure TSubAnimations.ParentAfterAttach; |
| 92 |
begin |
| 93 |
inherited; |
| 94 |
ParentScene := Parent as TCastleScene; // TSubAnimations can only be added as behavior to TCastleScene |
| 95 |
end; |
| 96 |
|
| 97 |
procedure TSubAnimations.Add( |
| 98 |
const SubAnimationName, BaseAnimation: String; |
| 99 |
const Start, Duration: TFloatTime); |
| 100 |
var |
| 101 |
SubAnim: TSubAnimation; |
| 102 |
begin |
| 103 |
SubAnim := TSubAnimation.Create; |
| 104 |
SubAnim.Name := SubAnimationName; |
| 105 |
SubAnim.BaseAnimation := BaseAnimation; |
| 106 |
SubAnim.Start := Start; |
| 107 |
SubAnim.Duration := Duration; |
| 108 |
Add(SubAnim); |
| 109 |
end; |
| 110 |
|
| 111 |
procedure TSubAnimations.Add(const SubAnimation: TSubAnimation); |
| 112 |
begin |
| 113 |
FList.Add(SubAnimation.Name, SubAnimation); |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TSubAnimations.Play(const SubAnimationName: String; const ALoop: Boolean); |
| 117 |
var |
| 118 |
SubAnim: TSubAnimation; |
| 119 |
begin |
| 120 |
if not FList.TryGetValue(SubAnimationName, SubAnim) then |
| 121 |
begin |
| 122 |
WritelnWarning('Cannot play subanimation named "%s", not found', [SubAnimationName]); |
| 123 |
Exit; |
| 124 |
end; |
| 125 |
Play(SubAnim, ALoop); |
| 126 |
end; |
| 127 |
|
| 128 |
procedure TSubAnimations.Play(const SubAnim: TSubAnimation; const ALoop: Boolean); |
| 129 |
begin |
| 130 |
FCurrentTime := 0; |
| 131 |
FCurrentSubAnimation := SubAnim; |
| 132 |
FLoop := ALoop; |
| 133 |
UpdateAnimationPose; |
| 134 |
end; |
| 135 |
|
| 136 |
procedure TSubAnimations.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 137 |
begin |
| 138 |
inherited; |
| 139 |
if FCurrentSubAnimation <> nil then |
| 140 |
begin |
| 141 |
FCurrentTime := FCurrentTime + SecondsPassed; |
| 142 |
UpdateAnimationPose; |
| 143 |
end; |
| 144 |
end; |
| 145 |
|
| 146 |
procedure TSubAnimations.UpdateAnimationPose; |
| 147 |
var |
| 148 |
T: TFloatTime; |
| 149 |
begin |
| 150 |
Assert(FCurrentSubAnimation <> nil); |
| 151 |
if FLoop then |
| 152 |
T := FloatModulo(FCurrentTime, FCurrentSubAnimation.Duration) |
| 153 |
else |
| 154 |
T := Clamped(FCurrentTime, 0, FCurrentSubAnimation.Duration); |
| 155 |
ParentScene.ForceAnimationPose( |
| 156 |
FCurrentSubAnimation.BaseAnimation, |
| 157 |
FCurrentSubAnimation.Start + T, false); |
| 158 |
end; |
| 159 |
|
| 160 |
end. |