quantum-space-buddies/QSB/ItemSync/Events/DropItemEvent.cs

37 lines
1.2 KiB
C#
Raw Normal View History

2021-02-24 10:45:25 +00:00
using QSB.Events;
using QSB.ItemSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.ItemSync.Events
{
internal class DropItemEvent : QSBEvent<DropItemMessage>
{
public override QSB.Events.EventType Type => QSB.Events.EventType.DropItem;
public override void SetupListener()
2021-02-25 13:52:49 +00:00
=> GlobalMessenger<int, Vector3, Vector3, Sector>.AddListener(EventNames.QSBDropItem, Handler);
2021-02-24 10:45:25 +00:00
public override void CloseListener()
2021-02-25 13:52:49 +00:00
=> GlobalMessenger<int, Vector3, Vector3, Sector>.RemoveListener(EventNames.QSBDropItem, Handler);
2021-02-24 10:45:25 +00:00
2021-02-25 13:52:49 +00:00
private void Handler(int objectId, Vector3 position, Vector3 normal, Sector sector)
=> SendEvent(CreateMessage(objectId, position, normal, sector));
2021-02-24 10:45:25 +00:00
2021-02-25 13:52:49 +00:00
private DropItemMessage CreateMessage(int objectId, Vector3 position, Vector3 normal, Sector sector) => new DropItemMessage
2021-02-24 10:45:25 +00:00
{
ObjectId = objectId,
Position = position,
Normal = normal,
2021-02-26 09:04:22 +00:00
Sector = sector
2021-02-24 10:45:25 +00:00
};
public override void OnReceiveRemote(bool server, DropItemMessage message)
{
var worldObject = QSBWorldSync.GetWorldFromId<IQSBOWItem>(message.ObjectId);
2021-02-25 13:52:49 +00:00
worldObject.DropItem(message.Position, message.Normal, message.Sector);
2021-02-24 10:45:25 +00:00
}
}
}