130 lines
3.8 KiB
C#
130 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
// ReSharper disable InvertIf
|
|
namespace BITKit
|
|
{
|
|
public class LocationAdditive : MonoBehaviour
|
|
{
|
|
[SerializeReference,SubclassSelector] internal IReference autoReference;
|
|
|
|
[SerializeField] private bool disabledRotation;
|
|
[SerializeField] private bool disabledPosition;
|
|
|
|
[SerializeField] private Vector3 positionWeight=Vector3.one;
|
|
|
|
[SerializeField] private bool debug;
|
|
|
|
[SerializeField,ReadOnly(HideLabel = true)] private string debugInfo;
|
|
|
|
private Vector3 currentPosition;
|
|
private Quaternion currentRotation=Quaternion.identity;
|
|
|
|
private Vector3 globalPosition;
|
|
private readonly Optional<Quaternion> globalRotation = new(Quaternion.identity);
|
|
|
|
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)
|
|
{
|
|
currentPosition += value;
|
|
if (debug)
|
|
{
|
|
reportBuilder.AppendLine($"{info}:{value}");
|
|
}
|
|
}
|
|
|
|
// public void AddEuler(Vector3 value)
|
|
// {
|
|
// //currentEuler += value;
|
|
// currentRotation *= Quaternion.Euler(value);
|
|
// }
|
|
public void AddRotation(Quaternion value, string info = null)
|
|
{
|
|
currentRotation *= value;
|
|
if (debug)
|
|
{
|
|
reportBuilder.AppendLine($"{info}:{value}");
|
|
}
|
|
}
|
|
|
|
|
|
public void SetGlobalPosition(Vector3 value)
|
|
{
|
|
globalPosition = value;
|
|
}
|
|
|
|
public void SetGlobalRotation(Quaternion value)
|
|
{
|
|
globalRotation.SetValueThenAllow(value);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_transform = transform;
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
currentPosition = Vector3.Scale(currentPosition, positionWeight);
|
|
|
|
currentPosition = Vector3.ClampMagnitude(currentPosition, 4096);
|
|
|
|
if (disabledRotation is false)
|
|
{
|
|
if (globalRotation.Allow)
|
|
{
|
|
transform.rotation =globalRotation * currentRotation;
|
|
}
|
|
else
|
|
{
|
|
transform.localRotation = currentRotation;
|
|
}
|
|
}
|
|
|
|
if (disabledPosition is false)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
currentRotation = Quaternion.identity;
|
|
|
|
currentPosition = Vector3.zero;
|
|
|
|
if (debug)
|
|
{
|
|
debugInfo = reportBuilder.ToString();
|
|
reportBuilder.Clear();
|
|
}
|
|
}
|
|
}
|
|
} |