BITKit/Src/Unity/Scripts/Location/LocationAdditive.cs

130 lines
3.8 KiB
C#
Raw Normal View History

2024-03-31 23:31:00 +08:00
using System;
2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
2024-03-31 23:31:00 +08:00
using System.Text;
using Unity.Mathematics;
2023-06-05 19:57:17 +08:00
using UnityEngine;
2023-10-06 23:43:19 +08:00
using UnityEngine.Rendering;
2023-09-01 14:35:05 +08:00
// ReSharper disable InvertIf
2023-06-05 19:57:17 +08:00
namespace BITKit
{
2023-09-01 14:35:05 +08:00
public class LocationAdditive : MonoBehaviour
2023-06-05 19:57:17 +08:00
{
2024-03-31 23:31:00 +08:00
[SerializeReference,SubclassSelector] internal IReference autoReference;
2023-11-30 00:25:43 +08:00
[SerializeField] private bool disabledRotation;
[SerializeField] private bool disabledPosition;
[SerializeField] private Vector3 positionWeight=Vector3.one;
2024-03-31 23:31:00 +08:00
[SerializeField] private bool debug;
[SerializeField,ReadOnly(HideLabel = true)] private string debugInfo;
2023-09-01 14:35:05 +08:00
private Vector3 currentPosition;
2024-03-31 23:31:00 +08:00
private Quaternion currentRotation=Quaternion.identity;
2023-11-30 00:25:43 +08:00
2023-10-06 23:43:19 +08:00
private Vector3 globalPosition;
2024-03-31 23:31:00 +08:00
private readonly Optional<Quaternion> globalRotation = new(Quaternion.identity);
2023-11-30 00:25:43 +08:00
2024-03-31 23:31:00 +08:00
private Transform _transform;
private readonly StringBuilder reportBuilder = new();
public Vector3 Position
{
get => _transform.localPosition;
set => _transform.localPosition = value;
}
public Quaternion Rotation
{
get => _transform.localRotation;
set => _transform.localRotation = value;
}
public void AddPosition(Vector3 value,string info=null)
2023-06-05 19:57:17 +08:00
{
currentPosition += value;
2024-03-31 23:31:00 +08:00
if (debug)
{
reportBuilder.AppendLine($"{info}:{value}");
}
2023-06-05 19:57:17 +08:00
}
2023-11-30 00:25:43 +08:00
2024-03-31 23:31:00 +08:00
// public void AddEuler(Vector3 value)
// {
// //currentEuler += value;
// currentRotation *= Quaternion.Euler(value);
// }
public void AddRotation(Quaternion value, string info = null)
2023-06-05 19:57:17 +08:00
{
2024-03-31 23:31:00 +08:00
currentRotation *= value;
if (debug)
{
reportBuilder.AppendLine($"{info}:{value}");
}
2023-06-05 19:57:17 +08:00
}
2023-11-30 00:25:43 +08:00
2024-03-31 23:31:00 +08:00
2023-10-06 23:43:19 +08:00
public void SetGlobalPosition(Vector3 value)
{
globalPosition = value;
}
2023-11-30 00:25:43 +08:00
2023-10-06 23:43:19 +08:00
public void SetGlobalRotation(Quaternion value)
{
2024-03-31 23:31:00 +08:00
globalRotation.SetValueThenAllow(value);
}
private void Start()
{
_transform = transform;
2023-10-06 23:43:19 +08:00
}
2023-11-30 00:25:43 +08:00
2024-03-31 23:31:00 +08:00
private void LateUpdate()
2023-06-05 19:57:17 +08:00
{
2023-11-30 00:25:43 +08:00
currentPosition = Vector3.Scale(currentPosition, positionWeight);
2024-03-31 23:31:00 +08:00
currentPosition = Vector3.ClampMagnitude(currentPosition, 4096);
2023-11-30 00:25:43 +08:00
if (disabledRotation is false)
2024-03-31 23:31:00 +08:00
{
if (globalRotation.Allow)
2023-11-30 00:25:43 +08:00
{
2024-03-31 23:31:00 +08:00
transform.rotation =globalRotation * currentRotation;
}
else
{
transform.localRotation = currentRotation;
2023-11-30 00:25:43 +08:00
}
2024-03-31 23:31:00 +08:00
}
2023-11-30 00:25:43 +08:00
if (disabledPosition is false)
2024-03-31 23:31:00 +08:00
{
2023-11-30 00:25:43 +08:00
switch (currentPosition, globalPosition)
{
case var (position, global) when position.IsDefault() is false && global.IsDefault():
transform.localPosition = position;
break;
case var (position, global) when position.IsDefault() && global.IsDefault() is false:
transform.position = globalPosition = global;
break;
case var (position, global) when position.IsDefault() is false && global.IsDefault() is false:
transform.position = globalPosition = global + position;
break;
}
2024-03-31 23:31:00 +08:00
}
2023-11-30 00:25:43 +08:00
2024-03-31 23:31:00 +08:00
currentRotation = Quaternion.identity;
2023-10-06 23:43:19 +08:00
currentPosition = Vector3.zero;
2024-03-31 23:31:00 +08:00
if (debug)
{
debugInfo = reportBuilder.ToString();
reportBuilder.Clear();
}
2023-06-05 19:57:17 +08:00
}
}
}