105 lines
3.3 KiB
C#
Raw Normal View History

2022-01-28 20:49:07 -08:00
using Cysharp.Threading.Tasks;
using QSB.ItemSync.WorldObjects.Sockets;
2021-03-23 13:18:29 +00:00
using QSB.Player;
using QSB.SectorSync.WorldObjects;
2021-11-01 20:58:10 +00:00
using QSB.Utility;
2021-02-25 13:52:49 +00:00
using QSB.WorldSync;
2022-01-28 20:49:07 -08:00
using System.Threading;
2021-02-25 13:52:49 +00:00
using UnityEngine;
2021-02-23 14:42:18 +00:00
2021-11-01 15:49:00 +00:00
namespace QSB.ItemSync.WorldObjects.Items
2021-02-23 14:42:18 +00:00
{
2022-01-26 00:41:39 -08:00
internal class QSBItem<T> : WorldObject<T>, IQSBItem
where T : OWItem
2021-02-23 14:42:18 +00:00
{
2022-01-26 00:31:12 -08:00
private QSBItemSocket InitialSocket { get; set; }
private Transform InitialParent { get; set; }
private Vector3 InitialPosition { get; set; }
private Quaternion InitialRotation { get; set; }
private QSBSector InitialSector { get; set; }
2021-03-23 13:18:29 +00:00
2022-01-28 20:50:34 -08:00
public override async UniTask Init(CancellationToken ct)
2021-03-23 13:18:29 +00:00
{
if (AttachedObject == null)
2021-11-01 20:58:10 +00:00
{
DebugLog.ToConsole($"Error - AttachedObject is null! Type:{GetType().Name}", OWML.Common.MessageType.Error);
return;
}
2022-01-28 20:57:17 -08:00
await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct);
2021-11-01 20:58:10 +00:00
2022-01-28 20:57:17 -08:00
InitialParent = AttachedObject.transform.parent;
InitialPosition = AttachedObject.transform.localPosition;
InitialRotation = AttachedObject.transform.localRotation;
var initialSector = AttachedObject.GetSector();
if (initialSector != null)
{
InitialSector = initialSector.GetWorldObject<QSBSector>();
}
2022-01-28 20:57:17 -08:00
if (InitialParent == null)
{
DebugLog.ToConsole($"Warning - InitialParent of {AttachedObject.name} is null!", OWML.Common.MessageType.Warning);
}
2022-01-28 20:57:17 -08:00
if (InitialParent?.GetComponent<OWItemSocket>() != null)
{
var qsbObj = InitialParent.GetComponent<OWItemSocket>().GetWorldObject<QSBItemSocket>();
InitialSocket = qsbObj;
}
2021-06-18 22:38:32 +01:00
2021-03-23 13:18:29 +00:00
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
}
2021-03-25 22:01:10 +00:00
public override void OnRemoval() => QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
2021-03-23 13:18:29 +00:00
private void OnPlayerLeave(PlayerInfo player)
2021-03-23 13:18:29 +00:00
{
if (player.HeldItem != this)
2021-03-23 13:18:29 +00:00
{
return;
}
2021-06-18 22:38:32 +01:00
2021-03-23 13:18:29 +00:00
if (InitialSocket != null)
{
InitialSocket.PlaceIntoSocket(this);
return;
}
2021-06-18 22:38:32 +01:00
2021-03-23 13:18:29 +00:00
AttachedObject.transform.parent = InitialParent;
AttachedObject.transform.localPosition = InitialPosition;
AttachedObject.transform.localRotation = InitialRotation;
AttachedObject.transform.localScale = Vector3.one;
AttachedObject.SetSector(InitialSector?.AttachedObject);
2021-03-23 13:18:29 +00:00
AttachedObject.SetColliderActivation(true);
}
2021-02-25 13:52:49 +00:00
public override void SendInitialState(uint to)
{
2022-01-26 01:01:32 -08:00
// todo SendInitialState
}
2021-02-25 14:53:34 +00:00
public ItemType GetItemType()
2021-02-25 21:24:10 +00:00
=> AttachedObject.GetItemType();
public void PickUpItem(Transform holdTransform)
=> AttachedObject.PickUpItem(holdTransform);
2021-02-26 10:23:08 +00:00
2022-01-25 21:48:52 -08:00
public void DropItem(Vector3 position, Vector3 normal, Sector sector)
2021-02-25 13:52:49 +00:00
{
AttachedObject.transform.SetParent(sector.transform);
AttachedObject.transform.localScale = Vector3.one;
2021-11-01 15:49:00 +00:00
var localDropNormal = AttachedObject._localDropNormal;
2021-02-25 13:52:49 +00:00
var lhs = Quaternion.FromToRotation(AttachedObject.transform.TransformDirection(localDropNormal), normal);
AttachedObject.transform.rotation = lhs * AttachedObject.transform.rotation;
2021-11-01 15:49:00 +00:00
var localDropOffset = AttachedObject._localDropOffset;
2021-02-25 13:52:49 +00:00
AttachedObject.transform.position = sector.transform.TransformPoint(position) + AttachedObject.transform.TransformDirection(localDropOffset);
AttachedObject.SetSector(sector);
AttachedObject.SetColliderActivation(true);
}
2021-02-25 14:53:34 +00:00
2022-01-25 21:48:52 -08:00
public void OnCompleteUnsocket()
=> AttachedObject.OnCompleteUnsocket();
2021-02-23 14:42:18 +00:00
}
}