qsb item: last location instead of just initial location

This commit is contained in:
JohnCorby 2022-03-23 14:19:54 -07:00
parent 3359b85ef0
commit c3168aa694
3 changed files with 47 additions and 45 deletions

View File

@ -43,7 +43,7 @@ public class QSBElevator : WorldObject<Elevator>
} }
} }
QSBPatch.RemoteCall(() => AttachedObject.StartLift(), this); QSBPatch.RemoteCall(AttachedObject.StartLift);
} }
private void SetDirection(bool isGoingUp) private void SetDirection(bool isGoingUp)

View File

@ -1,5 +1,6 @@
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using QSB.ItemSync.WorldObjects.Sockets; using QSB.ItemSync.WorldObjects.Sockets;
using QSB.Patches;
using QSB.Player; using QSB.Player;
using QSB.SectorSync.WorldObjects; using QSB.SectorSync.WorldObjects;
using QSB.Utility; using QSB.Utility;
@ -12,47 +13,42 @@ namespace QSB.ItemSync.WorldObjects.Items;
internal class QSBItem<T> : WorldObject<T>, IQSBItem internal class QSBItem<T> : WorldObject<T>, IQSBItem
where T : OWItem where T : OWItem
{ {
private QSBItemSocket InitialSocket { get; set; } private Transform _lastParent;
private Transform InitialParent { get; set; } private Vector3 _lastPosition;
private Vector3 InitialPosition { get; set; } private Quaternion _lastRotation;
private Quaternion InitialRotation { get; set; } private QSBSector _lastSector;
private QSBSector InitialSector { get; set; } private QSBItemSocket _lastSocket;
public override async UniTask Init(CancellationToken ct) public override async UniTask Init(CancellationToken ct)
{ {
if (AttachedObject == null)
{
DebugLog.ToConsole($"Error - AttachedObject is null! Type:{GetType().Name}", OWML.Common.MessageType.Error);
return;
}
await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct); await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct);
InitialParent = AttachedObject.transform.parent; SetLastLocation();
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)
{
var qsbObj = InitialParent.GetComponent<OWItemSocket>().GetWorldObject<QSBItemSocket>();
InitialSocket = qsbObj;
}
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave; QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
} }
public override void OnRemoval() => QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave; public override void OnRemoval() => QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
private void SetLastLocation()
{
_lastParent = AttachedObject.transform.parent;
_lastPosition = AttachedObject.transform.localPosition;
_lastRotation = AttachedObject.transform.localRotation;
var sector = AttachedObject.GetSector();
if (sector != null)
{
_lastSector = sector.GetWorldObject<QSBSector>();
}
var socket = _lastParent.GetComponent<OWItemSocket>();
if (socket != null)
{
_lastSocket = socket.GetWorldObject<QSBItemSocket>();
}
}
private void OnPlayerLeave(PlayerInfo player) private void OnPlayerLeave(PlayerInfo player)
{ {
if (player.HeldItem != this) if (player.HeldItem != this)
@ -60,17 +56,17 @@ internal class QSBItem<T> : WorldObject<T>, IQSBItem
return; return;
} }
if (InitialSocket != null) if (_lastSocket != null)
{ {
InitialSocket.PlaceIntoSocket(this); QSBPatch.RemoteCall(() => _lastSocket.PlaceIntoSocket(this));
return; return;
} }
AttachedObject.transform.parent = InitialParent; AttachedObject.transform.parent = _lastParent;
AttachedObject.transform.localPosition = InitialPosition; AttachedObject.transform.localPosition = _lastPosition;
AttachedObject.transform.localRotation = InitialRotation; AttachedObject.transform.localRotation = _lastRotation;
AttachedObject.transform.localScale = Vector3.one; AttachedObject.transform.localScale = Vector3.one;
AttachedObject.SetSector(InitialSector?.AttachedObject); AttachedObject.SetSector(_lastSector?.AttachedObject);
AttachedObject.SetColliderActivation(true); AttachedObject.SetColliderActivation(true);
} }
@ -80,14 +76,20 @@ internal class QSBItem<T> : WorldObject<T>, IQSBItem
} }
public ItemType GetItemType() public ItemType GetItemType()
=> AttachedObject.GetItemType(); => QSBPatch.RemoteCall(AttachedObject.GetItemType);
public void PickUpItem(Transform holdTransform) public void PickUpItem(Transform holdTransform)
=> AttachedObject.PickUpItem(holdTransform); => QSBPatch.RemoteCall(() => AttachedObject.PickUpItem(holdTransform));
public void DropItem(Vector3 position, Vector3 normal, Sector sector) => public void DropItem(Vector3 position, Vector3 normal, Sector sector)
AttachedObject.DropItem(position, normal, sector.transform, sector, null); {
QSBPatch.RemoteCall(() => AttachedObject.DropItem(position, normal, sector.transform, sector, null));
SetLastLocation();
}
public void OnCompleteUnsocket() public void OnCompleteUnsocket()
=> AttachedObject.OnCompleteUnsocket(); {
QSBPatch.RemoteCall(AttachedObject.OnCompleteUnsocket);
SetLastLocation();
}
} }

View File

@ -17,8 +17,8 @@ public abstract class QSBPatch
public static void RemoteCall(Action call, object data = null) public static void RemoteCall(Action call, object data = null)
{ {
RemoteData = data;
Remote = true; Remote = true;
RemoteData = data;
nameof(QSBPatch).Try("doing remote call", call); nameof(QSBPatch).Try("doing remote call", call);
Remote = false; Remote = false;
RemoteData = null; RemoteData = null;
@ -26,8 +26,8 @@ public abstract class QSBPatch
public static T RemoteCall<T>(Func<T> call, object data = null) public static T RemoteCall<T>(Func<T> call, object data = null)
{ {
RemoteData = data;
Remote = true; Remote = true;
RemoteData = data;
var t = default(T); var t = default(T);
nameof(QSBPatch).Try("doing remote call", () => t = call()); nameof(QSBPatch).Try("doing remote call", () => t = call());
Remote = false; Remote = false;