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

76 lines
2.4 KiB
C#
Raw Normal View History

2021-01-29 15:29:50 +00:00
using OWML.Common;
using QSB.Player;
2021-01-26 14:07:17 +00:00
using QSB.QuantumSync.Events;
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;
2020-12-23 22:43:05 +00:00
public override void Init(SocketedQuantumObject quantumObject, int id)
2020-12-22 21:39:53 +00:00
{
ObjectId = id;
AttachedObject = quantumObject;
2021-01-26 14:07:17 +00:00
base.Init(quantumObject, id);
2021-02-21 18:25:25 +00:00
if (QSBCore.DebugMode)
{
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, ObjectId.ToString()).GetComponent<Text>();
}
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;
}
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);
}
2021-03-07 09:27:23 +00:00
if (QuantumManager.Instance.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
}