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

117 lines
3.7 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
{
2021-02-24 10:45:25 +00:00
internal class QSBOWItem<T> : WorldObject<T>, IQSBOWItem
where T : OWItem
2021-02-23 14:42:18 +00:00
{
2021-03-23 13:18:29 +00:00
public IQSBOWItemSocket InitialSocket { get; private set; }
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 uint HoldingPlayer { 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(() => WorldObjectManager.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 = QSBWorldSync.GetWorldFromUnity<QSBSector>(initialSector);
}
if (InitialParent == null)
{
DebugLog.ToConsole($"Warning - InitialParent of {AttachedObject.name} is null!", OWML.Common.MessageType.Warning);
}
if (InitialParent?.GetComponent<OWItemSocket>() != null)
{
var qsbObj = (IQSBOWItemSocket)QSBWorldSync.GetWorldFromUnity(InitialParent.GetComponent<OWItemSocket>());
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(uint player)
{
if (HoldingPlayer != player)
{
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
2021-02-25 14:53:34 +00:00
public ItemType GetItemType()
2021-02-25 21:24:10 +00:00
=> AttachedObject.GetItemType();
2021-02-25 22:46:31 +00:00
public void SetColliderActivation(bool active)
2021-02-25 21:24:10 +00:00
=> AttachedObject.SetColliderActivation(active);
2021-02-25 14:53:34 +00:00
public virtual void SocketItem(Transform socketTransform, Sector sector)
2021-03-23 13:18:29 +00:00
{
AttachedObject.SocketItem(socketTransform, sector);
HoldingPlayer = 0;
}
public virtual void PickUpItem(Transform holdTransform, uint playerId)
{
AttachedObject.PickUpItem(holdTransform);
HoldingPlayer = playerId;
}
2021-02-26 10:23:08 +00:00
2021-02-25 13:52:49 +00:00
public virtual void DropItem(Vector3 position, Vector3 normal, Sector sector)
{
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-03-23 13:18:29 +00:00
HoldingPlayer = 0;
2021-02-25 13:52:49 +00:00
}
2021-02-25 14:53:34 +00:00
public virtual void PlaySocketAnimation() { }
2021-02-25 21:24:10 +00:00
public virtual void PlayUnsocketAnimation() { }
public virtual void OnCompleteUnsocket() { }
2021-02-23 14:42:18 +00:00
}
}