187 lines
5.2 KiB
C#
Raw Normal View History

2021-11-13 02:30:48 -08:00
using UnityEngine;
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
namespace QSB.Tools.TranslatorTool;
2022-07-01 10:48:30 +01:00
public class QSBNomaiTranslator : QSBTool
2021-11-10 09:12:05 +00:00
{
2022-03-02 19:46:33 -08:00
public static float distToClosestTextCenter = 1f;
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
public const float MAX_INTERACT_RANGE = 25f;
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
public Transform RaycastTransform;
private Collider _lastHitCollider;
private QSBNomaiTranslatorProp _translatorProp;
private NomaiText _currentNomaiText;
private NomaiTextLine _lastHighlightedTextLine;
private bool _lastLineWasTranslated;
private bool _lastLineLocked;
private float _lastLineDist;
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
private void Awake()
{
_lastHitCollider = null;
_translatorProp = this.GetRequiredComponentInChildren<QSBNomaiTranslatorProp>();
_currentNomaiText = null;
}
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
public override void OnDisable()
{
if (!_isDitheringOut)
{
2022-03-02 19:46:33 -08:00
_translatorProp.OnFinishUnequipAnimation();
}
2022-03-02 19:46:33 -08:00
}
2022-03-02 19:46:33 -08:00
public override void EquipTool()
{
base.EquipTool();
_translatorProp.OnEquipTool();
}
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
public override void UnequipTool()
{
base.UnequipTool();
_translatorProp.OnUnequipTool();
}
public override void FinishDitherOut()
{
base.FinishDitherOut();
_translatorProp.OnFinishUnequipAnimation();
}
2022-01-01 00:38:17 +00:00
2022-07-01 10:48:30 +01:00
public void SetScroll(float scrollPosition)
=> _translatorProp.SetScroll(scrollPosition);
public void UpdateTranslating(bool translating)
=> _translatorProp.UpdateTranslating(translating);
2022-03-02 19:46:33 -08:00
public override void Update()
{
base.Update();
if (!_isEquipped)
2021-11-10 09:12:05 +00:00
{
2022-03-02 19:46:33 -08:00
return;
}
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
distToClosestTextCenter = 1f;
var tooCloseToTarget = false;
var num = float.MaxValue;
if (Physics.Raycast(RaycastTransform.position, RaycastTransform.forward, out var raycastHit, 25f, OWLayerMask.blockableInteractMask))
{
2022-03-02 19:46:33 -08:00
_lastHitCollider = raycastHit.collider;
_currentNomaiText = _lastHitCollider.GetComponent<NomaiText>();
2021-11-10 09:12:05 +00:00
2022-03-02 19:46:33 -08:00
if (_currentNomaiText != null && !_currentNomaiText.CheckAllowFocus(raycastHit.distance, RaycastTransform.forward))
{
_currentNomaiText = null;
}
2022-03-02 19:46:33 -08:00
num = raycastHit.distance;
}
else
{
_lastHitCollider = null;
_currentNomaiText = null;
}
if (_currentNomaiText != null)
{
tooCloseToTarget = num < _currentNomaiText.GetMinimumReadableDistance();
2022-03-02 19:46:33 -08:00
if (_currentNomaiText is NomaiWallText nomaiWallText)
{
var nomaiTextLine = nomaiWallText.GetClosestTextLineByCenter(raycastHit.point);
if (_lastLineLocked)
2021-11-10 09:12:05 +00:00
{
2022-03-02 19:46:33 -08:00
var distToCenter = _lastHighlightedTextLine.GetDistToCenter(raycastHit.point);
if (distToCenter > _lastLineDist + 0.1f)
2021-11-10 09:12:05 +00:00
{
_lastHighlightedTextLine = nomaiTextLine;
_lastLineWasTranslated = nomaiTextLine != null && nomaiTextLine.IsTranslated();
2022-03-02 19:46:33 -08:00
_lastLineLocked = false;
2021-11-10 09:12:05 +00:00
}
2022-03-02 19:46:33 -08:00
else
2021-11-10 09:12:05 +00:00
{
2022-03-02 19:46:33 -08:00
nomaiTextLine = _lastHighlightedTextLine;
}
2022-03-02 19:46:33 -08:00
if (distToCenter < _lastLineDist)
{
2022-03-02 19:46:33 -08:00
_lastLineDist = distToCenter;
2021-11-10 09:12:05 +00:00
}
}
2022-03-02 19:46:33 -08:00
else if (_lastHighlightedTextLine != null && _lastHighlightedTextLine.IsTranslated() && !_lastLineWasTranslated)
{
2022-03-02 19:46:33 -08:00
_lastLineWasTranslated = true;
_lastLineDist = _lastHighlightedTextLine.GetDistToCenter(raycastHit.point);
_lastLineLocked = true;
2021-11-10 09:12:05 +00:00
}
2022-03-02 19:46:33 -08:00
else
{
2022-03-02 19:46:33 -08:00
_lastHighlightedTextLine = nomaiTextLine;
_lastLineWasTranslated = nomaiTextLine != null && nomaiTextLine.IsTranslated();
}
2022-03-02 19:46:33 -08:00
if (nomaiTextLine && !nomaiTextLine.IsHidden() && nomaiTextLine.IsActive())
{
2022-03-02 19:46:33 -08:00
distToClosestTextCenter = Vector3.Distance(raycastHit.point, nomaiTextLine.GetWorldCenter());
2022-06-30 10:18:55 +01:00
_translatorProp.SetNomaiText(_currentNomaiText, nomaiTextLine.GetEntryID());
2022-03-02 19:46:33 -08:00
_translatorProp.SetNomaiTextLine(nomaiTextLine);
}
else
{
2022-06-30 10:18:55 +01:00
_translatorProp.ClearNomaiText();
2022-03-02 19:46:33 -08:00
_translatorProp.ClearNomaiTextLine();
_lastHighlightedTextLine = null;
_lastLineWasTranslated = false;
_lastLineLocked = false;
}
}
2022-03-02 19:46:33 -08:00
else if (_currentNomaiText is NomaiComputer nomaiComputer)
{
2022-03-02 19:46:33 -08:00
var closestRing = nomaiComputer.GetClosestRing(raycastHit.point, out var num2);
if (closestRing)
{
distToClosestTextCenter = Mathf.Min(num2 * 2f, 1f);
2022-06-30 10:18:55 +01:00
_translatorProp.SetNomaiText(_currentNomaiText, closestRing.GetEntryID());
2022-03-02 19:46:33 -08:00
_translatorProp.SetNomaiComputerRing(closestRing);
}
}
2022-03-02 19:46:33 -08:00
else if (_currentNomaiText is NomaiVesselComputer nomaiVesselComputer)
{
var closestRing2 = nomaiVesselComputer.GetClosestRing(raycastHit.point, out var num3);
if (closestRing2)
{
distToClosestTextCenter = Mathf.Min(num3 * 2f, 1f);
2022-06-30 10:18:55 +01:00
_translatorProp.SetNomaiText(_currentNomaiText, closestRing2.GetEntryID());
2022-03-02 19:46:33 -08:00
_translatorProp.SetNomaiVesselComputerRing(closestRing2);
}
}
else if (_currentNomaiText is GhostWallText ghostWallText)
{
2022-06-30 10:18:55 +01:00
_translatorProp.SetTargetingGhostText(true);
2022-03-02 19:46:33 -08:00
_translatorProp.SetNomaiTextLine(ghostWallText.GetTextLine());
}
2022-06-30 10:18:55 +01:00
else if (raycastHit.textureCoord2 == Vector2.zero)
{
_translatorProp.SetNomaiText(this._currentNomaiText);
}
else
{
var textID = Mathf.RoundToInt(raycastHit.textureCoord2.x * 10f);
_translatorProp.SetNomaiText(this._currentNomaiText, textID);
}
2022-03-02 19:46:33 -08:00
}
else
{
2022-06-30 10:18:55 +01:00
_translatorProp.ClearNomaiText();
2022-03-02 19:46:33 -08:00
_translatorProp.ClearNomaiTextLine();
_translatorProp.ClearNomaiComputerRing();
_translatorProp.ClearNomaiVesselComputerRing();
2022-06-30 10:18:55 +01:00
_translatorProp.SetTargetingGhostText(false);
}
2022-03-02 19:46:33 -08:00
_translatorProp.SetTooCloseToTarget(tooCloseToTarget);
2021-11-10 09:12:05 +00:00
}
}