quantum-space-buddies/QSB/ItemSync/WorldObjects/Items/QSBItem.cs

106 lines
3.2 KiB
C#
Raw Normal View History

2021-11-09 19:39:56 +00:00
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;
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 05:48:52 +00:00
public class QSBItem : WorldObject<OWItem>
2021-02-23 14:42:18 +00:00
{
2022-01-26 05:38:38 +00:00
public QSBItemSocket InitialSocket { get; private set; }
2021-03-23 13:18:29 +00:00
public Transform InitialParent { get; private set; }
public Vector3 InitialPosition { get; private set; }
public Quaternion InitialRotation { get; private set; }
public QSBSector InitialSector { get; private set; }
public override void Init()
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;
}
StartDelayedReady();
QSBCore.UnityEvents.RunWhen(() => QSBWorldSync.AllObjectsAdded, () =>
2021-11-01 20:58:10 +00:00
{
FinishDelayedReady();
2021-11-01 20:58:10 +00:00
InitialParent = AttachedObject.transform.parent;
InitialPosition = AttachedObject.transform.localPosition;
InitialRotation = AttachedObject.transform.localRotation;
var initialSector = AttachedObject.GetSector();
if (initialSector != null)
{
InitialSector = initialSector.GetWorldObject<QSBSector>();
}
if (InitialParent == null)
{
DebugLog.ToConsole($"Warning - InitialParent of {AttachedObject.name} is null!", OWML.Common.MessageType.Warning);
}
if (InitialParent?.GetComponent<OWItemSocket>() != null)
{
2022-01-26 05:38:38 +00:00
var qsbObj = InitialParent.GetComponent<OWItemSocket>().GetWorldObject<QSBItemSocket>();
InitialSocket = qsbObj;
}
});
2021-06-18 21:38:32 +00: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 21:38:32 +00:00
2021-03-23 13:18:29 +00:00
if (InitialSocket != null)
{
InitialSocket.PlaceIntoSocket(this);
return;
}
2021-06-18 21:38:32 +00: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 05:32:43 +00:00
// todo SendResyncInfo
}
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-26 05:48:52 +00: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-26 05:48:52 +00:00
public void OnCompleteUnsocket()
=> AttachedObject.OnCompleteUnsocket();
2021-02-23 14:42:18 +00:00
}
}