148 lines
4.7 KiB
C#
148 lines
4.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Ichni.RhythmGame.Beatmap;
|
|
using UniRx;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace Ichni.RhythmGame
|
|
{
|
|
public partial class TimeDurationSubmodule : SubmoduleBase
|
|
{
|
|
#region [时间标记与代理] Time Flags & Actions
|
|
public bool isOverridingDuration; //是否手动设置了时间区间,开启时,子物体的时间区间将被忽略,且在自动计算区间时跳过此模块
|
|
public float startTime, endTime; //起止时间
|
|
private Action _enableAction, _disableAction; //启用和禁用时的额外操作
|
|
#endregion
|
|
|
|
#region [构造函数与初始化] Constructors & Initialization
|
|
public TimeDurationSubmodule(GameElement attachedGameElement) : base(attachedGameElement)
|
|
{
|
|
isOverridingDuration = false;
|
|
startTime = -32767; //TODO: 换为-delay
|
|
endTime = 32767; //TODO: 换为songLength
|
|
|
|
if (!HaveSameSubmodule)
|
|
{
|
|
(attachedGameElement as IHaveTimeDurationSubmodule).timeDurationSubmodule = this;
|
|
}
|
|
}
|
|
|
|
public TimeDurationSubmodule(GameElement attachedGameElement, bool isOverridingDuration, float startTime, float endTime) :
|
|
base(attachedGameElement)
|
|
{
|
|
this.isOverridingDuration = isOverridingDuration;
|
|
this.startTime = startTime;
|
|
this.endTime = endTime;
|
|
|
|
if (!HaveSameSubmodule)
|
|
{
|
|
(attachedGameElement as IHaveTimeDurationSubmodule).timeDurationSubmodule = this;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region [时间判断工具] Time Checking Utils
|
|
public bool CheckTimeInDuration(float time, float offset = 0.2f)
|
|
{
|
|
return time >= startTime - offset && time <= endTime + offset;
|
|
}
|
|
|
|
public bool CheckAfterEndTime(float time, float offset = 0.2f)
|
|
{
|
|
return time > endTime + offset;
|
|
}
|
|
#endregion
|
|
|
|
#region [设置与时长运算] Get & Set Durations
|
|
public void SetDuration(float startTime, float endTime)
|
|
{
|
|
this.startTime = startTime;
|
|
this.endTime = endTime;
|
|
this.isOverridingDuration = true;
|
|
}
|
|
|
|
public void SetDuration(params FlexibleFloat[] flexibleFloats)
|
|
{
|
|
List<float> startTimes = new List<float>();
|
|
List<float> endTimes = new List<float>();
|
|
|
|
foreach (FlexibleFloat flexibleFloat in flexibleFloats)
|
|
{
|
|
flexibleFloat.Sort();
|
|
|
|
if (flexibleFloat.animations.Count > 0)
|
|
{
|
|
startTimes.Add(flexibleFloat.animations[0].startTime);
|
|
endTimes.Add(flexibleFloat.animations[^1].endTime);
|
|
}
|
|
else continue;
|
|
}
|
|
if (startTimes.Count == 0 || endTimes.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
startTime = startTimes.Min();
|
|
endTime = endTimes.Max();
|
|
}
|
|
|
|
public void SetDurationFromChildren(List<TimeDurationSubmodule> children)
|
|
{
|
|
List<float2> durations = new List<float2>();
|
|
|
|
if (children.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var child in children)
|
|
{
|
|
durations.Add(new float2(child.startTime, child.endTime));
|
|
}
|
|
|
|
startTime = durations.Min(duration => duration.x);
|
|
endTime = durations.Max(duration => duration.y);
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
|
|
#region [动态控制状态] Dynamic State Control
|
|
public partial class TimeDurationSubmodule
|
|
{
|
|
public void SetUpActions(Action enableAction, Action disableAction = null)
|
|
{
|
|
_enableAction = enableAction;
|
|
_disableAction = disableAction;
|
|
}
|
|
|
|
public void DetectAndSwitchActiveState(float currentSongTime)
|
|
{
|
|
bool inDuration = CheckTimeInDuration(currentSongTime);
|
|
bool isActive = attachedGameElement.gameObject.activeSelf;
|
|
|
|
if (inDuration && !isActive)
|
|
{
|
|
attachedGameElement.gameObject.SetActive(true);
|
|
_enableAction?.Invoke();
|
|
}
|
|
else if (!inDuration && isActive)
|
|
{
|
|
attachedGameElement.gameObject.SetActive(false);
|
|
_disableAction?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region [组件接口] Component Interface
|
|
public interface IHaveTimeDurationSubmodule
|
|
{
|
|
public TimeDurationSubmodule timeDurationSubmodule { get; set; }
|
|
}
|
|
#endregion
|
|
|
|
} |