quantum-space-buddies/QSB/ShipSync/Events/Hull/HullChangeIntegrityEvent.cs
Mister_Nebula 85a3f4770e cleanup
2021-07-12 22:02:50 +01:00

34 lines
1.2 KiB
C#

using QSB.Events;
using QSB.ShipSync.WorldObjects;
using QSB.WorldSync;
namespace QSB.ShipSync.Events.Hull
{
internal class HullChangeIntegrityEvent : QSBEvent<HullChangeIntegrityMessage>
{
public override EventType Type => EventType.HullChangeIntegrity;
public override void SetupListener() => GlobalMessenger<ShipHull, float>.AddListener(EventNames.QSBHullChangeIntegrity, Handler);
public override void CloseListener() => GlobalMessenger<ShipHull, float>.RemoveListener(EventNames.QSBHullChangeIntegrity, Handler);
private void Handler(ShipHull hull, float integrity) => SendEvent(CreateMessage(hull, integrity));
private HullChangeIntegrityMessage CreateMessage(ShipHull hull, float integrity)
{
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBShipHull, ShipHull>(hull);
return new HullChangeIntegrityMessage
{
AboutId = LocalPlayerId,
ObjectId = worldObject.ObjectId,
Integrity = integrity
};
}
public override void OnReceiveRemote(bool server, HullChangeIntegrityMessage message)
{
var worldObject = QSBWorldSync.GetWorldFromId<QSBShipHull>(message.ObjectId);
worldObject.ChangeIntegrity(message.Integrity);
}
}
}