quantum-space-buddies/QSB/Events/PlayerJoinEvent.cs

47 lines
1.5 KiB
C#
Raw Normal View History

2020-08-09 09:17:00 +02:00
using QSB.Messaging;
using QSB.TransformSync;
2020-08-08 12:23:23 +01:00
using QSB.Utility;
namespace QSB.Events
{
2020-08-10 18:17:54 +02:00
public class PlayerJoinEvent : QSBEvent<PlayerJoinMessage>
2020-08-08 12:23:23 +01:00
{
2020-08-09 09:17:00 +02:00
public override MessageType Type => MessageType.PlayerJoin;
2020-08-08 12:23:23 +01:00
public override void SetupListener()
{
2020-08-15 21:52:43 +02:00
GlobalMessenger<string>.AddListener(EventNames.QSBPlayerJoin, Handler);
2020-08-15 20:32:58 +01:00
}
public override void CloseListener()
{
2020-08-15 21:52:43 +02:00
GlobalMessenger<string>.RemoveListener(EventNames.QSBPlayerJoin, Handler);
2020-08-15 20:32:58 +01:00
}
2020-08-17 16:51:56 +01:00
private void Handler(string name) => SendEvent(CreateMessage(name));
2020-08-08 12:23:23 +01:00
2020-08-10 18:17:54 +02:00
private PlayerJoinMessage CreateMessage(string name) => new PlayerJoinMessage
{
2020-08-17 16:51:56 +01:00
FromId = PlayerTransformSync.LocalInstance.netId.Value,
AboutId = PlayerTransformSync.LocalInstance.netId.Value,
2020-08-10 18:17:54 +02:00
PlayerName = name
};
2020-08-10 14:40:06 +01:00
public override void OnReceiveRemote(PlayerJoinMessage message)
2020-08-08 12:23:23 +01:00
{
2020-08-17 16:51:56 +01:00
var player = PlayerRegistry.CreatePlayer(message.AboutId);
2020-08-09 09:37:32 +02:00
player.Name = message.PlayerName;
2020-08-09 14:26:33 +01:00
var text = $"{player.Name} joined!";
2020-08-10 18:17:54 +02:00
DebugLog.ToAll(OWML.Common.MessageType.Info, text);
2020-08-08 12:23:23 +01:00
}
2020-08-09 09:37:32 +02:00
public override void OnReceiveLocal(PlayerJoinMessage message)
2020-08-08 12:23:23 +01:00
{
2020-08-08 13:27:49 +01:00
var player = PlayerRegistry.CreatePlayer(PlayerTransformSync.LocalInstance.netId.Value);
2020-08-09 09:37:32 +02:00
player.Name = message.PlayerName;
2020-08-09 14:26:33 +01:00
var text = $"Connected to server as {player.Name}.";
2020-08-10 18:17:54 +02:00
DebugLog.ToAll(OWML.Common.MessageType.Info, text);
2020-08-08 12:23:23 +01:00
}
}
}