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

82 lines
2.4 KiB
C#
Raw Normal View History

2022-01-28 20:49:07 -08:00
using Cysharp.Threading.Tasks;
using OWML.Common;
2021-01-29 15:29:50 +00:00
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;
2022-01-28 20:49:07 -08:00
using System.Threading;
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
{
2022-01-28 20:50:34 -08:00
public override async UniTask Init(CancellationToken ct)
2020-12-22 21:39:53 +00:00
{
2022-01-28 20:50:34 -08:00
base.Init(ct);
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>();
2022-01-23 00:31:15 -08:00
return $"{this}{Environment.NewLine}SocketId:{socketObj.ObjectId}";
2021-12-28 18:06:34 +00:00
}
else
{
2022-01-23 00:31:15 -08:00
return $"{this}{Environment.NewLine}SocketId:NULL";
}
2021-02-21 18:25:25 +00:00
}
public override void SendInitialState(uint to)
{
base.SendInitialState(to);
2022-01-26 01:01:32 -08:00
// todo SendInitialState
}
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
}