quantum-space-buddies/QSB/QuantumSync/WorldObjects/QSBSocketedQuantumObject.cs

75 lines
2.3 KiB
C#
Raw Normal View History

2021-01-29 15:29:50 +00:00
using OWML.Common;
using QSB.Player;
2021-12-23 17:07:29 -08:00
using QSB.QuantumSync.Messages;
2021-01-29 15:29:50 +00:00
using QSB.Utility;
2021-01-26 14:07:17 +00:00
using QSB.WorldSync;
using System.Reflection;
using UnityEngine;
2021-02-21 18:25:25 +00:00
using UnityEngine.UI;
2020-12-22 21:39:53 +00:00
namespace QSB.QuantumSync.WorldObjects
2020-12-22 21:39:53 +00:00
{
2021-01-26 14:07:17 +00:00
internal class QSBSocketedQuantumObject : QSBQuantumObject<SocketedQuantumObject>
2020-12-22 21:39:53 +00:00
{
2021-02-21 18:25:25 +00:00
public Text DebugBoxText;
public override void Init()
2020-12-22 21:39:53 +00:00
{
base.Init();
2021-11-25 22:44:15 +00:00
if (QSBCore.ShowQuantumDebugBoxes)
2021-02-21 18:25:25 +00:00
{
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, $"Socketed\r\nid:{ObjectId}").GetComponent<Text>();
2021-02-21 18:25:25 +00:00
}
2021-01-26 14:07:17 +00:00
}
2021-02-21 18:25:25 +00:00
public override void OnRemoval()
2021-01-26 14:07:17 +00:00
{
2021-02-21 18:25:25 +00:00
base.OnRemoval();
if (DebugBoxText != null)
{
2021-02-21 18:25:25 +00:00
Object.Destroy(DebugBoxText.gameObject);
}
2021-02-21 18:25:25 +00:00
}
public void MoveToSocket(SocketStateChangeMessage message)
{
2021-02-24 10:45:25 +00:00
var qsbSocket = QSBWorldSync.GetWorldFromId<QSBQuantumSocket>(message.SocketId);
2021-01-29 15:29:50 +00:00
if (qsbSocket == null)
{
2021-02-07 11:26:13 +00:00
DebugLog.ToConsole($"Couldn't find socket id {message.SocketId}", MessageType.Error);
2021-01-29 15:29:50 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2021-01-29 15:29:50 +00:00
var socket = qsbSocket.AttachedObject;
if (socket == null)
{
2021-02-07 11:26:13 +00:00
DebugLog.ToConsole($"QSBSocket id {message.SocketId} has no attached socket.", MessageType.Error);
2021-01-29 15:29:50 +00:00
return;
}
2021-01-31 19:50:01 +00:00
var wasEntangled = AttachedObject.IsPlayerEntangled();
var component = Locator.GetPlayerTransform().GetComponent<OWRigidbody>();
var location = new RelativeLocationData(Locator.GetPlayerTransform().GetComponent<OWRigidbody>(), AttachedObject.transform);
2021-01-26 14:07:17 +00:00
AttachedObject.GetType().GetMethod("MoveToSocket", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(AttachedObject, new object[] { socket });
2021-01-31 19:50:01 +00:00
if (wasEntangled)
{
component.MoveToRelativeLocation(location, AttachedObject.transform);
}
if (QuantumManager.Shrine != AttachedObject)
2021-01-26 14:07:17 +00:00
{
AttachedObject.transform.localRotation = message.LocalRotation;
}
else
{
var playerToShrine = QSBPlayerManager.GetPlayer(message.FromId).Body.transform.position - AttachedObject.transform.position;
var projectOnPlace = Vector3.ProjectOnPlane(playerToShrine, AttachedObject.transform.up);
var angle = OWMath.Angle(AttachedObject.transform.forward, projectOnPlace, AttachedObject.transform.up);
angle = OWMath.RoundToNearestMultiple(angle, 120f);
AttachedObject.transform.rotation = Quaternion.AngleAxis(angle, AttachedObject.transform.up) * AttachedObject.transform.rotation;
}
2020-12-22 21:39:53 +00:00
}
}
2020-12-23 10:43:13 +00:00
}