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

73 lines
2.1 KiB
C#
Raw Normal View History

2021-01-29 15:29:50 +00:00
using OWML.Common;
using QSB.Player;
using QSB.Utility;
2021-01-26 14:07:17 +00:00
using QSB.WorldSync;
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
}
2021-12-26 06:04:46 +00:00
public void MoveToSocket(uint playerId, int socketId, Quaternion localRotation)
2021-02-21 18:25:25 +00:00
{
var qsbSocket = socketId.GetWorldObject<QSBQuantumSocket>();
2021-01-29 15:29:50 +00:00
if (qsbSocket == null)
{
2021-12-26 06:04:46 +00:00
DebugLog.ToConsole($"Couldn't find socket id {socketId}", MessageType.Error);
2021-01-29 15:29:50 +00:00
return;
}
2021-06-18 21:38:32 +00:00
2021-01-29 15:29:50 +00:00
var socket = qsbSocket.AttachedObject;
if (socket == null)
{
2021-12-26 06:04:46 +00:00
DebugLog.ToConsole($"QSBSocket id {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-12-26 06:04:46 +00:00
AttachedObject.MoveToSocket(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
{
2021-12-26 06:04:46 +00:00
AttachedObject.transform.localRotation = localRotation;
2021-01-26 14:07:17 +00:00
}
else
{
2021-12-26 06:04:46 +00:00
var playerToShrine = QSBPlayerManager.GetPlayer(playerId).Body.transform.position - AttachedObject.transform.position;
2021-01-26 14:07:17 +00:00
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
}