Files
ichni_Official/Assets/Scripts/Game/Components/TransformSubmodule.cs

194 lines
7.1 KiB
C#
Raw Normal View History

2025-06-03 02:42:28 -04:00
using System;
using System.Collections;
using System.Collections.Generic;
2025-07-21 05:42:20 -04:00
using System.Linq;
2025-06-03 02:42:28 -04:00
using Ichni.RhythmGame.Beatmap;
using UniRx;
2025-07-21 05:42:20 -04:00
using UniRx.Triggers;
using Unity.VisualScripting;
2025-06-03 02:42:28 -04:00
using UnityEngine;
2025-07-21 05:42:20 -04:00
using Object = UnityEngine.Object;
2025-06-03 02:42:28 -04:00
namespace Ichni.RhythmGame
{
public class TransformSubmodule : SubmoduleBase
{
2026-03-14 03:13:10 -04:00
#region [] Transform Cached States
2025-06-03 02:42:28 -04:00
public Vector3 originalPosition;
public Vector3 originalEulerAngles;
public Vector3 originalScale;
2025-07-21 05:42:20 -04:00
public Vector3 positionOffset;
public Vector3 eulerAnglesOffset;
public Vector3 scaleOffset;
2025-06-03 02:42:28 -04:00
public Vector3 currentPosition;
public Vector3 currentEulerAngles;
public Vector3 currentScale;
2025-07-21 05:42:20 -04:00
2025-06-03 02:42:28 -04:00
public bool positionDirtyMark;
public bool eulerAnglesDirtyMark;
public bool scaleDirtyMark;
public bool eulerAnglesOffsetLock;
2025-07-21 05:42:20 -04:00
public IDisposable observer;
2026-03-14 03:13:10 -04:00
#endregion
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
#region [] Constructors & Initialization
2025-06-03 02:42:28 -04:00
public TransformSubmodule(GameElement attachedGameElement) : base(attachedGameElement)
{
this.originalPosition = Vector3.zero;
this.originalEulerAngles = Vector3.zero;
this.originalScale = Vector3.one;
2025-07-21 05:42:20 -04:00
positionOffset = Vector3.zero;
eulerAnglesOffset = Vector3.zero;
scaleOffset = Vector3.zero;
2025-06-03 02:42:28 -04:00
currentPosition = Vector3.zero;
currentEulerAngles = Vector3.zero;
currentScale = Vector3.one;
2025-07-21 05:42:20 -04:00
2025-06-03 02:42:28 -04:00
positionDirtyMark = true;
eulerAnglesDirtyMark = true;
scaleDirtyMark = true;
eulerAnglesOffsetLock = false;
2026-03-14 03:13:10 -04:00
if (!HaveSameSubmodule && attachedGameElement is IHaveTransformSubmodule host)
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
host.transformSubmodule = this;
host.SetTransformObserver();
2025-06-03 02:42:28 -04:00
}
}
public TransformSubmodule(GameElement attachedGameElement,
Vector3 originalPosition, Vector3 originalEulerAngles, Vector3 originalScale) : base(attachedGameElement)
{
this.originalPosition = originalPosition;
this.originalEulerAngles = originalEulerAngles;
this.originalScale = originalScale;
2025-07-21 05:42:20 -04:00
positionOffset = Vector3.zero;
eulerAnglesOffset = Vector3.zero;
scaleOffset = Vector3.zero;
2025-06-03 02:42:28 -04:00
currentPosition = originalPosition;
currentEulerAngles = originalEulerAngles;
currentScale = originalScale;
2025-07-21 05:42:20 -04:00
2025-06-03 02:42:28 -04:00
positionDirtyMark = true;
eulerAnglesDirtyMark = true;
scaleDirtyMark = true;
eulerAnglesOffsetLock = false;
2025-07-26 04:20:25 -04:00
2026-03-14 03:13:10 -04:00
if (!HaveSameSubmodule && attachedGameElement is IHaveTransformSubmodule host)
2025-06-03 02:42:28 -04:00
{
2026-03-14 03:13:10 -04:00
host.transformSubmodule = this;
host.SetTransformObserver();
2025-06-03 02:42:28 -04:00
}
}
2026-03-14 03:13:10 -04:00
#endregion
#region [] Lifecycle & State Refresh
2025-06-03 02:42:28 -04:00
public override void Refresh()
{
positionDirtyMark = true;
eulerAnglesDirtyMark = true;
scaleDirtyMark = true;
}
2025-07-21 05:42:20 -04:00
private bool HaveAnimation() => attachedGameElement.childElementList
.Any(element => element is Displacement or Swirl or Scale or LookAt);
public override void CheckAndRemoveObservers()
{
if (!HaveAnimation())
{
observer?.Dispose();
}
}
2026-03-14 03:13:10 -04:00
#endregion
2025-06-03 02:42:28 -04:00
}
2026-03-14 03:13:10 -04:00
#region [] Component Interface
2025-06-03 02:42:28 -04:00
public interface IHaveTransformSubmodule
{
TransformSubmodule transformSubmodule { get; set; }
/// <summary>
/// 设置物体Transform的监听顺序为Scale -> EulerAngles -> Position
/// 如果有一些特殊的物体例如CameraElementFolder需要自定义监听可以重写这个方法
/// </summary>
public void SetTransformObserver()
{
2026-03-19 14:14:28 -04:00
// 旧版的 UniRx 各自监听已淘汰,现由 GameManager 中枢在 LateUpdate 统一下发 UpdateTransform()
// 如果有一些特殊物体需要极其特殊时序,可覆盖此方法或手动管理
2025-07-21 05:42:20 -04:00
}
2025-07-26 04:20:25 -04:00
public void UpdateTransform(bool refreshAll = true)
2025-07-21 05:42:20 -04:00
{
2025-06-03 02:42:28 -04:00
GameElement attachedGameElement = transformSubmodule.attachedGameElement;
2025-07-21 05:42:20 -04:00
bool willRefresh = false;
2025-06-03 02:42:28 -04:00
2025-07-21 05:42:20 -04:00
if (transformSubmodule.scaleDirtyMark)
2025-06-03 02:42:28 -04:00
{
2025-07-21 05:42:20 -04:00
transformSubmodule.currentScale = transformSubmodule.originalScale + transformSubmodule.scaleOffset;
2025-08-11 14:04:06 -04:00
2025-07-21 05:42:20 -04:00
attachedGameElement.transform.localScale = transformSubmodule.currentScale;
transformSubmodule.scaleDirtyMark = false;
willRefresh = true;
transformSubmodule.scaleOffset = Vector3.zero;
}
if (!transformSubmodule.eulerAnglesOffsetLock && transformSubmodule.eulerAnglesDirtyMark)
{
transformSubmodule.currentEulerAngles = transformSubmodule.originalEulerAngles + transformSubmodule.eulerAnglesOffset;
attachedGameElement.transform.localEulerAngles = transformSubmodule.currentEulerAngles;
transformSubmodule.eulerAnglesDirtyMark = false;
willRefresh = true;
transformSubmodule.eulerAnglesOffset = Vector3.zero;
}
if (transformSubmodule.positionDirtyMark)
{
transformSubmodule.currentPosition = transformSubmodule.originalPosition + transformSubmodule.positionOffset;
attachedGameElement.transform.localPosition = transformSubmodule.currentPosition;
transformSubmodule.positionDirtyMark = false;
willRefresh = true;
transformSubmodule.positionOffset = Vector3.zero;
}
2025-06-03 02:42:28 -04:00
2025-07-26 04:20:25 -04:00
if(refreshAll && willRefresh)
2025-07-21 05:42:20 -04:00
{
attachedGameElement.Refresh();
}
}
public void UpdateLookAt(LookAt lookAt) // 处理LookAt
{
2025-08-11 14:04:06 -04:00
Transform target = lookAt.targetGameElement.transform;
2025-07-21 05:42:20 -04:00
Transform self = transformSubmodule.attachedGameElement.transform;
if (transformSubmodule.eulerAnglesOffsetLock && transformSubmodule.eulerAnglesDirtyMark)
{
Vector3 lookingDirection = (target.position - self.position).normalized;
2025-06-03 02:42:28 -04:00
2025-07-21 05:42:20 -04:00
Vector3 eulerAnglesOffset = Quaternion.LookRotation(lookingDirection).eulerAngles;
2025-06-03 02:42:28 -04:00
2025-07-21 05:42:20 -04:00
transformSubmodule.eulerAnglesOffset += eulerAnglesOffset;
transformSubmodule.currentEulerAngles = transformSubmodule.originalEulerAngles + transformSubmodule.eulerAnglesOffset;
self.localEulerAngles = transformSubmodule.currentEulerAngles;
transformSubmodule.eulerAnglesDirtyMark = false;
transformSubmodule.eulerAnglesOffsetLock = false;
transformSubmodule.eulerAnglesOffset = Vector3.zero;
}
2025-06-03 02:42:28 -04:00
}
}
2026-03-14 03:13:10 -04:00
#endregion
2025-06-03 02:42:28 -04:00
}