quantum-space-buddies/QSB/Animation/AnimTriggerEvent.cs
2020-08-22 18:08:25 +01:00

40 lines
1.2 KiB
C#

using QSB.Events;
using QSB.Messaging;
namespace QSB.Animation
{
public class AnimTriggerEvent : QSBEvent<AnimTriggerMessage>
{
public override EventType Type => EventType.AnimTrigger;
public override void SetupListener()
{
GlobalMessenger<short, float>.AddListener(EventNames.QSBAnimTrigger, Handler);
}
public override void CloseListener()
{
GlobalMessenger<short, float>.RemoveListener(EventNames.QSBAnimTrigger, Handler);
}
private void Handler(short triggerId, float value) => SendEvent(CreateMessage(triggerId, value));
private AnimTriggerMessage CreateMessage(short triggerId, float value) => new AnimTriggerMessage
{
AboutId = LocalPlayerId,
TriggerId = triggerId,
Value = value
};
public override void OnReceiveRemote(AnimTriggerMessage message)
{
var animationSync = PlayerRegistry.GetSyncObject<AnimationSync>(message.AboutId);
if (animationSync == null)
{
return;
}
animationSync.HandleTrigger((AnimTrigger)message.TriggerId, message.Value);
}
}
}