75 lines
2.8 KiB
C#
Raw Normal View History

2022-04-01 13:42:20 -07:00
using QSB.ItemSync.WorldObjects;
using QSB.ItemSync.WorldObjects.Items;
2021-12-25 22:35:32 -08: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-02 19:46:33 -08:00
namespace QSB.ItemSync.Messages;
2022-04-01 13:42:20 -07: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 13:42:20 -07:00
public DropItemMessage(Vector3 worldPosition, Vector3 worldNormal, Transform parent, Sector sector, IItemDropTarget customDropTarget, OWRigidbody targetRigidbody)
2022-04-01 19:08:03 +01: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 13:42:20 -07:00
IItemDropTarget customDropTarget,
2022-04-01 19:08:03 +01: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 14:08:08 -07:00
tuple.dropTargetId = ((MonoBehaviour)customDropTarget).GetWorldObject<IQSBDropTarget>().ObjectId;
2022-04-01 19:08:03 +01:00
}
2022-04-06 16:04:37 -07:00
tuple.sectorId = sector ? sector.GetWorldObject<QSBSector>().ObjectId : -1;
2022-04-01 19:08:03 +01:00
tuple.localPosition = parent.InverseTransformPoint(worldPosition);
tuple.localNormal = parent.InverseTransformDirection(worldNormal);
return tuple;
}
2022-03-02 19:46:33 -08:00
public override void OnReceiveRemote()
{
2022-04-01 13:30:36 -07:00
var customDropTarget = Data.dropTargetId == -1
2022-04-01 19:08:03 +01:00
? null
2022-04-01 14:08:08 -07:00
: Data.dropTargetId.GetWorldObject<IQSBDropTarget>().AttachedObject;
2022-04-01 19:08:03 +01:00
2022-04-01 13:42:20 -07:00
var parent = customDropTarget == null
2022-04-01 13:30:36 -07:00
? Data.rigidBodyId.GetWorldObject<QSBOWRigidbody>().AttachedObject.transform
2022-04-01 19:08:03 +01:00
: customDropTarget.GetItemDropTargetTransform(null);
var worldPos = parent.TransformPoint(Data.localPosition);
var worldNormal = parent.TransformDirection(Data.localNormal);
2022-04-06 16:04:37 -07:00
var sector = Data.sectorId != -1 ? Data.sectorId.GetWorldObject<QSBSector>().AttachedObject : null;
2022-04-01 19:08:03 +01:00
WorldObject.DropItem(worldPos, worldNormal, parent, sector, customDropTarget);
2022-07-25 13:09:51 +01:00
WorldObject.HasBeenPickedUp = true;
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-02 19:46:33 -08:00
var player = QSBPlayerManager.GetPlayer(From);
2022-03-23 15:26:42 -07:00
player.HeldItem = null;
2022-03-02 19:46:33 -08:00
player.AnimationSync.VisibleAnimator.SetTrigger("DropHeldItem");
2021-02-24 10:45:25 +00:00
}
2022-03-04 11:37:24 -08:00
}