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

73 lines
2.2 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;
2021-12-28 18:06:34 +00:00
using System;
2021-01-26 14:07:17 +00:00
using UnityEngine;
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
{
public override void Init()
2020-12-22 21:39:53 +00:00
{
base.Init();
2021-12-26 20:57:34 -08:00
AttachedObject._randomYRotation = false;
2021-01-26 14:07:17 +00:00
}
2021-12-28 18:06:34 +00:00
public override string ReturnLabel()
2021-01-26 14:07:17 +00:00
{
2021-12-28 18:06:34 +00:00
var socket = AttachedObject.GetCurrentSocket();
if (socket != null)
{
2021-12-28 16:20:28 -08:00
var socketObj = socket.GetWorldObject<QSBQuantumSocket>();
2021-12-28 18:06:34 +00:00
return $"{LogName}{Environment.NewLine}SocketId:{socketObj.ObjectId}";
}
else
{
2021-12-28 18:06:34 +00:00
return $"{LogName}{Environment.NewLine}SocketId:NULL";
}
2021-02-21 18:25:25 +00:00
}
2021-12-25 22:04:46 -08: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-25 22:04:46 -08:00
DebugLog.ToConsole($"Couldn't find socket id {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-12-25 22:04:46 -08: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-25 22:04:46 -08: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-25 22:04:46 -08:00
AttachedObject.transform.localRotation = localRotation;
2021-01-26 14:07:17 +00:00
}
else
{
2021-12-25 22:04:46 -08: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
}