|
6 | 6 | using Stride.Animations; |
7 | 7 | using Stride.Engine; |
8 | 8 |
|
9 | | -namespace ##Namespace## |
| 9 | +namespace ##Namespace##; |
| 10 | + |
| 11 | +public class ##Scriptname## : SyncScript, IBlendTreeBuilder |
10 | 12 | { |
11 | | - public class ##Scriptname## : SyncScript, IBlendTreeBuilder |
12 | | - { |
13 | | - [Display("Animation Component")] |
14 | | - public AnimationComponent AnimationComponent; |
| 13 | + [Display("Animation Component")] |
| 14 | + public AnimationComponent AnimationComponent; |
15 | 15 |
|
16 | | - [Display("Animation 1")] |
17 | | - public AnimationClip Animation1; |
| 16 | + [Display("Animation 1")] |
| 17 | + public AnimationClip Animation1; |
18 | 18 |
|
19 | | - [Display("Animation 2")] |
20 | | - public AnimationClip Animation2; |
| 19 | + [Display("Animation 2")] |
| 20 | + public AnimationClip Animation2; |
21 | 21 |
|
22 | | - [DataMemberRange(0, 1, 0.01, 0.1, 3)] |
23 | | - [Display("Blend Lerp")] |
24 | | - public float BlendLerp = 0.5f; |
| 22 | + [DataMemberRange(0, 1, 0.01, 0.1, 3)] |
| 23 | + [Display("Blend Lerp")] |
| 24 | + public float BlendLerp = 0.5f; |
25 | 25 |
|
26 | | - [Display("Time Scale")] |
27 | | - public double TimeFactor = 1; |
| 26 | + [Display("Time Scale")] |
| 27 | + public double TimeFactor = 1; |
28 | 28 |
|
29 | | - private AnimationClipEvaluator anim1Evaluator; |
30 | | - private AnimationClipEvaluator anim2Evaluator; |
31 | | - private double currentTime = 0; |
| 29 | + private AnimationClipEvaluator anim1Evaluator; |
| 30 | + private AnimationClipEvaluator anim2Evaluator; |
| 31 | + private double currentTime = 0; |
32 | 32 |
|
33 | | - public override void Start() |
34 | | - { |
35 | | - base.Start(); |
| 33 | + public override void Start() |
| 34 | + { |
| 35 | + base.Start(); |
36 | 36 |
|
37 | | - if (AnimationComponent == null) |
38 | | - throw new InvalidOperationException("The animation component is not set"); |
| 37 | + if (AnimationComponent == null) |
| 38 | + throw new InvalidOperationException("The animation component is not set"); |
39 | 39 |
|
40 | | - if (Animation1 == null) |
41 | | - throw new InvalidOperationException("Animation 1 is not set"); |
| 40 | + if (Animation1 == null) |
| 41 | + throw new InvalidOperationException("Animation 1 is not set"); |
42 | 42 |
|
43 | | - if (Animation2 == null) |
44 | | - throw new InvalidOperationException("Animation 2 is not set"); |
| 43 | + if (Animation2 == null) |
| 44 | + throw new InvalidOperationException("Animation 2 is not set"); |
45 | 45 |
|
46 | | - AnimationComponent.BlendTreeBuilder = this; |
| 46 | + AnimationComponent.BlendTreeBuilder = this; |
47 | 47 |
|
48 | | - anim1Evaluator = AnimationComponent.Blender.CreateEvaluator(Animation1); |
49 | | - anim2Evaluator = AnimationComponent.Blender.CreateEvaluator(Animation2); |
50 | | - } |
| 48 | + anim1Evaluator = AnimationComponent.Blender.CreateEvaluator(Animation1); |
| 49 | + anim2Evaluator = AnimationComponent.Blender.CreateEvaluator(Animation2); |
| 50 | + } |
51 | 51 |
|
52 | | - public override void Cancel() |
53 | | - { |
54 | | - AnimationComponent.Blender.ReleaseEvaluator(anim1Evaluator); |
55 | | - AnimationComponent.Blender.ReleaseEvaluator(anim2Evaluator); |
56 | | - } |
| 52 | + public override void Cancel() |
| 53 | + { |
| 54 | + AnimationComponent.Blender.ReleaseEvaluator(anim1Evaluator); |
| 55 | + AnimationComponent.Blender.ReleaseEvaluator(anim2Evaluator); |
| 56 | + } |
57 | 57 |
|
58 | | - public override void Update() |
59 | | - { |
60 | | - // Use DrawTime rather than UpdateTime |
61 | | - var time = Game.DrawTime; |
| 58 | + public override void Update() |
| 59 | + { |
| 60 | + // Use DrawTime rather than UpdateTime |
| 61 | + var time = Game.DrawTime; |
62 | 62 |
|
63 | | - // This update function will account for animation with different durations, keeping a current time relative to the blended maximum duration |
64 | | - long blendedMaxDuration = 0; |
65 | | - blendedMaxDuration = (long)MathUtil.Lerp(Animation1.Duration.Ticks, Animation1.Duration.Ticks, BlendLerp); |
| 63 | + // This update function will account for animation with different durations, keeping a current time relative to the blended maximum duration |
| 64 | + long blendedMaxDuration = 0; |
| 65 | + blendedMaxDuration = (long)MathUtil.Lerp(Animation1.Duration.Ticks, Animation1.Duration.Ticks, BlendLerp); |
66 | 66 |
|
67 | | - var currentTicks = TimeSpan.FromTicks((long)(currentTime * blendedMaxDuration)); |
| 67 | + var currentTicks = TimeSpan.FromTicks((long)(currentTime * blendedMaxDuration)); |
68 | 68 |
|
69 | | - currentTicks = blendedMaxDuration == 0 ? TimeSpan.Zero : TimeSpan.FromTicks((currentTicks.Ticks + (long)(time.Elapsed.Ticks * TimeFactor)) % blendedMaxDuration); |
| 69 | + currentTicks = blendedMaxDuration == 0 ? TimeSpan.Zero : TimeSpan.FromTicks((currentTicks.Ticks + (long)(time.Elapsed.Ticks * TimeFactor)) % blendedMaxDuration); |
70 | 70 |
|
71 | | - currentTime = ((double) currentTicks.Ticks/(double) blendedMaxDuration); |
72 | | - } |
| 71 | + currentTime = (currentTicks.Ticks/(double) blendedMaxDuration); |
| 72 | + } |
73 | 73 |
|
74 | | - public void BuildBlendTree(FastList<AnimationOperation> blendStack) |
75 | | - { |
76 | | - // Note! The tree has to be flattened and given as a stack! |
77 | | - blendStack.Add(AnimationOperation.NewPush(anim1Evaluator, TimeSpan.FromTicks((long)(currentTime * Animation1.Duration.Ticks)))); |
78 | | - blendStack.Add(AnimationOperation.NewPush(anim2Evaluator, TimeSpan.FromTicks((long)(currentTime * Animation2.Duration.Ticks)))); |
79 | | - blendStack.Add(AnimationOperation.NewBlend(CoreAnimationOperation.Blend, BlendLerp)); |
80 | | - } |
| 74 | + public void BuildBlendTree(FastList<AnimationOperation> blendStack) |
| 75 | + { |
| 76 | + // Note! The tree has to be flattened and given as a stack! |
| 77 | + blendStack.Add(AnimationOperation.NewPush(anim1Evaluator, TimeSpan.FromTicks((long)(currentTime * Animation1.Duration.Ticks)))); |
| 78 | + blendStack.Add(AnimationOperation.NewPush(anim2Evaluator, TimeSpan.FromTicks((long)(currentTime * Animation2.Duration.Ticks)))); |
| 79 | + blendStack.Add(AnimationOperation.NewBlend(CoreAnimationOperation.Blend, BlendLerp)); |
81 | 80 | } |
82 | 81 | } |
0 commit comments