HeavenStudio/Assets/Scripts/Games/LumBEARjack/LBJCatMove.cs
2024-02-14 18:34:28 +01:00

63 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HeavenStudio.Util;
namespace HeavenStudio.Games.Scripts_LumBEARjack
{
public class LBJCatMove : MonoBehaviour
{
[Header("Components")]
[SerializeField] private Transform _otherPoint;
[Header("Properties")]
[SerializeField] private bool _startAtOther;
[SerializeField] private bool _usePoint = true;
[SerializeField] private float _slideOffset = 0f;
private Vector3 _thisPosition;
private Vector3 _otherPosition;
private Coroutine _currentMove;
private void Awake()
{
_thisPosition = transform.position;
if (_usePoint)
{
_otherPosition = _otherPoint.position;
}
else
{
_otherPosition = _thisPosition + new Vector3(_slideOffset / 2, 0);
}
if (_startAtOther) transform.position = _otherPosition;
}
public void Move(double beat, double length, bool inToScene)
{
if (_currentMove != null) StopCoroutine(_currentMove);
_currentMove = StartCoroutine(MoveCo(beat, length, inToScene));
}
private IEnumerator MoveCo(double beat, double length, bool inToScene)
{
if (length <= 0)
{
transform.position = inToScene ? _thisPosition : _otherPosition;
yield break;
}
float normalized = Conductor.instance.GetPositionFromBeat(beat, length, false);
while (normalized <= 1f)
{
normalized = Conductor.instance.GetPositionFromBeat(beat, length);
Vector3 newPos = Vector3.Lerp(inToScene ? _otherPosition : _thisPosition, inToScene ? _thisPosition : _otherPosition, normalized);
transform.position = newPos;
yield return null;
}
}
}
}