quantum-space-buddies/QSB/ItemSync/Messages/DropItemMessage.cs

75 lines
2.8 KiB
C#
Raw Normal View History

2022-04-01 20:42:20 +00:00
using QSB.ItemSync.WorldObjects;
using QSB.ItemSync.WorldObjects.Items;
2021-12-26 06:35:32 +00:00
using QSB.Messaging;
using QSB.Player;
2021-02-24 10:45:25 +00:00
using QSB.SectorSync.WorldObjects;
using QSB.WorldSync;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.ItemSync.Messages;
2022-04-01 20:42:20 +00:00
internal class DropItemMessage : QSBWorldObjectMessage<IQSBItem,
(Vector3 localPosition, Vector3 localNormal, int sectorId, int dropTargetId, int rigidBodyId)>
2021-02-24 10:45:25 +00:00
{
2022-04-01 20:42:20 +00:00
public DropItemMessage(Vector3 worldPosition, Vector3 worldNormal, Transform parent, Sector sector, IItemDropTarget customDropTarget, OWRigidbody targetRigidbody)
2022-04-01 18:08:03 +00:00
: base(ProcessInputs(worldPosition, worldNormal, parent, sector, customDropTarget, targetRigidbody)) { }
private static (Vector3 localPosition, Vector3 localNormal, int sectorId, int dropTargetId, int rigidBodyId) ProcessInputs(
Vector3 worldPosition,
Vector3 worldNormal,
Transform parent,
Sector sector,
2022-04-01 20:42:20 +00:00
IItemDropTarget customDropTarget,
2022-04-01 18:08:03 +00:00
OWRigidbody targetRigidbody)
{
(Vector3 localPosition, Vector3 localNormal, int sectorId, int dropTargetId, int rigidBodyId) tuple = new();
if (customDropTarget == null)
{
tuple.rigidBodyId = targetRigidbody.GetWorldObject<QSBOWRigidbody>().ObjectId;
tuple.dropTargetId = -1;
}
else
{
tuple.rigidBodyId = -1;
2022-04-01 21:08:08 +00:00
tuple.dropTargetId = ((MonoBehaviour)customDropTarget).GetWorldObject<IQSBDropTarget>().ObjectId;
2022-04-01 18:08:03 +00:00
}
2022-04-06 23:04:37 +00:00
tuple.sectorId = sector ? sector.GetWorldObject<QSBSector>().ObjectId : -1;
2022-04-01 18:08:03 +00:00
tuple.localPosition = parent.InverseTransformPoint(worldPosition);
tuple.localNormal = parent.InverseTransformDirection(worldNormal);
return tuple;
}
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
2022-04-01 20:30:36 +00:00
var customDropTarget = Data.dropTargetId == -1
2022-04-01 18:08:03 +00:00
? null
2022-04-01 21:08:08 +00:00
: Data.dropTargetId.GetWorldObject<IQSBDropTarget>().AttachedObject;
2022-04-01 18:08:03 +00:00
2022-04-01 20:42:20 +00:00
var parent = customDropTarget == null
2022-04-01 20:30:36 +00:00
? Data.rigidBodyId.GetWorldObject<QSBOWRigidbody>().AttachedObject.transform
2022-04-01 18:08:03 +00:00
: customDropTarget.GetItemDropTargetTransform(null);
var worldPos = parent.TransformPoint(Data.localPosition);
var worldNormal = parent.TransformDirection(Data.localNormal);
2022-04-06 23:04:37 +00:00
var sector = Data.sectorId != -1 ? Data.sectorId.GetWorldObject<QSBSector>().AttachedObject : null;
2022-04-01 18:08:03 +00:00
WorldObject.DropItem(worldPos, worldNormal, parent, sector, customDropTarget);
WorldObject.ItemState.HasBeenInteractedWith = true;
2022-07-25 12:09:51 +00:00
WorldObject.ItemState.State = ItemStateType.OnGround;
WorldObject.ItemState.LocalPosition = Data.localPosition;
WorldObject.ItemState.Parent = parent;
WorldObject.ItemState.LocalNormal = Data.localNormal;
WorldObject.ItemState.Sector = sector;
WorldObject.ItemState.CustomDropTarget = customDropTarget;
WorldObject.ItemState.Rigidbody = parent.GetComponent<OWRigidbody>();
2022-03-03 03:46:33 +00:00
var player = QSBPlayerManager.GetPlayer(From);
2022-03-23 22:26:42 +00:00
player.HeldItem = null;
2022-03-03 03:46:33 +00:00
player.AnimationSync.VisibleAnimator.SetTrigger("DropHeldItem");
2021-02-24 10:45:25 +00:00
}
2022-03-04 19:37:24 +00:00
}