From a755cc13bd43c20b6fa15f3f5851e787a41bcd25 Mon Sep 17 00:00:00 2001 From: PopSlime Date: Mon, 27 Feb 2023 00:17:14 +0800 Subject: [PATCH] Implement animation. --- Assets/Cryville/Crtr/ChartPlayer.cs | 2 +- .../Cryville/Crtr/Components/SkinAnimation.cs | 45 ++++++++++++++++++- Assets/Cryville/Crtr/SkinContainer.cs | 9 ++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Assets/Cryville/Crtr/ChartPlayer.cs b/Assets/Cryville/Crtr/ChartPlayer.cs index c83b9d9..de4c8ac 100644 --- a/Assets/Cryville/Crtr/ChartPlayer.cs +++ b/Assets/Cryville/Crtr/ChartPlayer.cs @@ -24,7 +24,7 @@ namespace Cryville.Crtr { #region Fields Chart chart; Skin skin; - PdtSkin pskin; + public static PdtSkin pskin; Ruleset ruleset; PdtRuleset pruleset; Dictionary texs; diff --git a/Assets/Cryville/Crtr/Components/SkinAnimation.cs b/Assets/Cryville/Crtr/Components/SkinAnimation.cs index 7f4d429..29312cc 100644 --- a/Assets/Cryville/Crtr/Components/SkinAnimation.cs +++ b/Assets/Cryville/Crtr/Components/SkinAnimation.cs @@ -1,5 +1,48 @@ -namespace Cryville.Crtr.Components { +using Cryville.Common; + +namespace Cryville.Crtr.Components { public class SkinAnimation : SkinComponent { + public SkinAnimation() { + SubmitProperty("name", new PropOp.Identifier(v => Name = v)); + SubmitProperty("duration", new PropOp.Float(v => Duration = v)); + _rtimeSrc = new PropSrc.Float(() => _rtime); + } + + SkinContext _skinContext; + + AnimationSpan _anim; + public int Name { + set { + if (value == 0) return; + var id = new Identifier(value); + AnimationSpan anim; + if (!ChartPlayer.pskin.animations.TryGetValue(id, out anim)) { + Logger.Log("main", 4, "Skin", "Animation {0} not found", id.Name); + _anim = null; + return; + } + _anim = anim; + } + } + public float Duration { get; private set; } + + double _startTime; + float _rtime; + readonly PropSrc _rtimeSrc; + + public override void Init() { + _skinContext = new SkinContext(transform); + } + public override void Rewind(double time) { + _startTime = time; + } + public override void Tick(SkinContainer c, double time) { + _rtime = (float)(time - _startTime) / Duration; + _rtimeSrc.Invalidate(); + ChartPlayer.etor.ContextSelfValue = _rtimeSrc; + if (_anim != null) c.MatchAnimation(_anim, _rtime, new RuntimeSkinContext(_skinContext)); + ChartPlayer.etor.ContextSelfValue = null; + } protected override void OnDestroy() { } } } diff --git a/Assets/Cryville/Crtr/SkinContainer.cs b/Assets/Cryville/Crtr/SkinContainer.cs index 9946ac1..a667f96 100644 --- a/Assets/Cryville/Crtr/SkinContainer.cs +++ b/Assets/Cryville/Crtr/SkinContainer.cs @@ -140,6 +140,15 @@ namespace Cryville.Crtr { } if (rc.PropSrcs != null) ChartPlayer.etor.ContextCascadeDiscard(); } + public void MatchAnimation(AnimationSpan span, float rtime, RuntimeSkinContext ctx) { + foreach (var p in span.properties) { + p.Key.ExecuteDynamic(_group, ctx, p.Value, 0); + } + foreach (var s in span.spans) { + if (rtime < s.Key.Behind || rtime >= s.Key.Ahead) continue; + MatchAnimation(s.Value, rtime, ctx); + } + } } public class SkinContext { public Transform Transform { get; private set; }