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)

View File

@ -1,5 +1,6 @@
using Cysharp.Threading.Tasks;
using QSB.ItemSync.WorldObjects.Sockets;
using QSB.Patches;
using QSB.Player;
using QSB.SectorSync.WorldObjects;
using QSB.Utility;
@ -12,47 +13,42 @@ namespace QSB.ItemSync.WorldObjects.Items;
internal class QSBItem<T> : WorldObject<T>, IQSBItem
where T : OWItem
{
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; }
private Transform _lastParent;
private Vector3 _lastPosition;
private Quaternion _lastRotation;
private QSBSector _lastSector;
private QSBItemSocket _lastSocket;
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);
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)
{
var qsbObj = InitialParent.GetComponent<OWItemSocket>().GetWorldObject<QSBItemSocket>();
InitialSocket = qsbObj;
}
SetLastLocation();
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)
{
if (player.HeldItem != this)
@ -60,17 +56,17 @@ internal class QSBItem<T> : WorldObject<T>, IQSBItem
return;
}
if (InitialSocket != null)
if (_lastSocket != null)
{
InitialSocket.PlaceIntoSocket(this);
QSBPatch.RemoteCall(() => _lastSocket.PlaceIntoSocket(this));
return;
}
AttachedObject.transform.parent = InitialParent;
AttachedObject.transform.localPosition = InitialPosition;
AttachedObject.transform.localRotation = InitialRotation;
AttachedObject.transform.parent = _lastParent;
AttachedObject.transform.localPosition = _lastPosition;
AttachedObject.transform.localRotation = _lastRotation;
AttachedObject.transform.localScale = Vector3.one;
AttachedObject.SetSector(InitialSector?.AttachedObject);
AttachedObject.SetSector(_lastSector?.AttachedObject);
AttachedObject.SetColliderActivation(true);
}
@ -80,14 +76,20 @@ internal class QSBItem<T> : WorldObject<T>, IQSBItem
}
public ItemType GetItemType()
=> AttachedObject.GetItemType();
=> QSBPatch.RemoteCall(AttachedObject.GetItemType);
public void PickUpItem(Transform holdTransform)
=> AttachedObject.PickUpItem(holdTransform);
=> QSBPatch.RemoteCall(() => AttachedObject.PickUpItem(holdTransform));
public void DropItem(Vector3 position, Vector3 normal, Sector sector) =>
AttachedObject.DropItem(position, normal, sector.transform, sector, null);
public void DropItem(Vector3 position, Vector3 normal, Sector sector)
{
QSBPatch.RemoteCall(() => AttachedObject.DropItem(position, normal, sector.transform, sector, null));
SetLastLocation();
}
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)
{
RemoteData = data;
Remote = true;
RemoteData = data;
nameof(QSBPatch).Try("doing remote call", call);
Remote = false;
RemoteData = null;
@ -26,8 +26,8 @@ public abstract class QSBPatch
public static T RemoteCall<T>(Func<T> call, object data = null)
{
RemoteData = data;
Remote = true;
RemoteData = data;
var t = default(T);
nameof(QSBPatch).Try("doing remote call", () => t = call());
Remote = false;