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

35 lines
1.1 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-09 09:37:32 +02:00
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-09 13:48:55 +01:00
GlobalMessenger<string>.AddListener("QSBPlayerJoin", name => SendEvent(
new PlayerJoinMessage {
SenderId = PlayerTransformSync.LocalInstance.netId.Value,
PlayerName = name
}));
2020-08-08 12:23:23 +01:00
}
2020-08-09 12:19:51 +01:00
public override void OnReceive(PlayerJoinMessage message)
2020-08-08 12:23:23 +01:00
{
2020-08-09 12:19:51 +01:00
var player = PlayerRegistry.CreatePlayer(message.SenderId);
2020-08-09 09:37:32 +02:00
player.Name = message.PlayerName;
2020-08-08 12:23:23 +01:00
DebugLog.ToAll($"{player.Name} joined!");
}
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 12:19:51 +01:00
DebugLog.ToAll($"Connected to server as {player.Name}.");
2020-08-08 12:23:23 +01:00
}
}
}