Skip to content

Commit 2d5e6a6

Browse files
authored
refactor: Modernize script templates (#2662)
1 parent ccf4ff6 commit 2d5e6a6

20 files changed

Lines changed: 1629 additions & 1656 deletions

sources/editor/Stride.Assets.Presentation/Templates/Assets/Scripts/Animation/AnimationBlend.cs

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,76 @@
66
using Stride.Animations;
77
using Stride.Engine;
88

9-
namespace ##Namespace##
9+
namespace ##Namespace##;
10+
11+
public class ##Scriptname## : SyncScript, IBlendTreeBuilder
1012
{
11-
public class ##Scriptname## : SyncScript, IBlendTreeBuilder
12-
{
13-
[Display("Animation Component")]
14-
public AnimationComponent AnimationComponent;
13+
[Display("Animation Component")]
14+
public AnimationComponent AnimationComponent;
1515

16-
[Display("Animation 1")]
17-
public AnimationClip Animation1;
16+
[Display("Animation 1")]
17+
public AnimationClip Animation1;
1818

19-
[Display("Animation 2")]
20-
public AnimationClip Animation2;
19+
[Display("Animation 2")]
20+
public AnimationClip Animation2;
2121

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;
2525

26-
[Display("Time Scale")]
27-
public double TimeFactor = 1;
26+
[Display("Time Scale")]
27+
public double TimeFactor = 1;
2828

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;
3232

33-
public override void Start()
34-
{
35-
base.Start();
33+
public override void Start()
34+
{
35+
base.Start();
3636

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");
3939

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");
4242

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");
4545

46-
AnimationComponent.BlendTreeBuilder = this;
46+
AnimationComponent.BlendTreeBuilder = this;
4747

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+
}
5151

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+
}
5757

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;
6262

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);
6666

67-
var currentTicks = TimeSpan.FromTicks((long)(currentTime * blendedMaxDuration));
67+
var currentTicks = TimeSpan.FromTicks((long)(currentTime * blendedMaxDuration));
6868

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);
7070

71-
currentTime = ((double) currentTicks.Ticks/(double) blendedMaxDuration);
72-
}
71+
currentTime = (currentTicks.Ticks/(double) blendedMaxDuration);
72+
}
7373

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));
8180
}
8281
}

sources/editor/Stride.Assets.Presentation/Templates/Assets/Scripts/Animation/AnimationStart.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,45 @@
99
using Stride.Input;
1010
using Stride.Engine;
1111

12-
namespace ##Namespace##
12+
namespace ##Namespace##;
13+
14+
[DataContract("PlayAnimation")]
15+
public class PlayAnimation
1316
{
14-
[DataContract("PlayAnimation")]
15-
public class PlayAnimation
16-
{
17-
public AnimationClip Clip;
18-
public AnimationBlendOperation BlendOperation = AnimationBlendOperation.LinearBlend;
19-
public double StartTime = 0;
20-
}
17+
public AnimationClip Clip;
18+
public AnimationBlendOperation BlendOperation = AnimationBlendOperation.LinearBlend;
19+
public double StartTime = 0;
20+
}
2121

22+
/// <summary>
23+
/// Script which starts a few animations on its entity
24+
/// </summary>
25+
public class ##Scriptname## : StartupScript
26+
{
2227
/// <summary>
23-
/// Script which starts a few animations on its entity
28+
/// A list of animations to be loaded when the script starts
2429
/// </summary>
25-
public class ##Scriptname## : StartupScript
30+
public readonly List<PlayAnimation> Animations = [];
31+
32+
public override void Start()
2633
{
27-
/// <summary>
28-
/// A list of animations to be loaded when the script starts
29-
/// </summary>
30-
public readonly List<PlayAnimation> Animations = new List<PlayAnimation>();
31-
32-
public override void Start()
33-
{
34-
var animComponent = Entity.GetOrCreate<AnimationComponent>();
34+
var animComponent = Entity.GetOrCreate<AnimationComponent>();
3535

36-
if (animComponent != null)
37-
PlayAnimations(animComponent);
36+
if (animComponent != null)
37+
PlayAnimations(animComponent);
3838

39-
// Destroy this script since it's no longer needed
40-
Entity.Remove(this);
41-
}
39+
// Destroy this script since it's no longer needed
40+
Entity.Remove(this);
41+
}
4242

43-
private void PlayAnimations(AnimationComponent animComponent)
43+
private void PlayAnimations(AnimationComponent animComponent)
44+
{
45+
foreach (var anim in Animations)
4446
{
45-
foreach (var anim in Animations)
46-
{
47-
if (anim.Clip != null)
48-
animComponent.Add(anim.Clip, anim.StartTime, anim.BlendOperation);
49-
}
50-
51-
Animations.Clear();
47+
if (anim.Clip != null)
48+
animComponent.Add(anim.Clip, anim.StartTime, anim.BlendOperation);
5249
}
50+
51+
Animations.Clear();
5352
}
5453
}

0 commit comments

Comments
 (0)