mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-10 21:40:19 +00:00
Sync game state on connect (#86)
* Sync full state on init * Sync game state on start
This commit is contained in:
parent
f3ec54f75c
commit
83e8112c0a
38
QSB/Events/FullStateMessage.cs
Normal file
38
QSB/Events/FullStateMessage.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using QSB.Messaging;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace QSB.Events
|
||||
{
|
||||
class FullStateMessage : NameMessage
|
||||
{
|
||||
public override MessageType MessageType => MessageType.FullState;
|
||||
|
||||
public Dictionary<uint, string> PlayerNames;
|
||||
|
||||
public override void Deserialize(NetworkReader reader)
|
||||
{
|
||||
base.Deserialize(reader);
|
||||
PlayerNames = new Dictionary<uint, string>();
|
||||
|
||||
reader.ReadString().Split(',').ToList().ForEach(pair =>
|
||||
{
|
||||
var splitPair = pair.Split(':');
|
||||
var key = Convert.ToUInt16(splitPair[0]);
|
||||
var value = splitPair[1];
|
||||
PlayerNames[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
public override void Serialize(NetworkWriter writer)
|
||||
{
|
||||
base.Serialize(writer);
|
||||
|
||||
var playerNamePairs = PlayerNames.ToList().Select(pair => $"{pair.Key}:{pair.Value}").ToArray();
|
||||
|
||||
writer.Write(string.Join(",", playerNamePairs));
|
||||
}
|
||||
}
|
||||
}
|
35
QSB/Events/GameState.cs
Normal file
35
QSB/Events/GameState.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using QSB.Messaging;
|
||||
using QSB.TransformSync;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace QSB.Events
|
||||
{
|
||||
class GameState : NetworkBehaviour
|
||||
{
|
||||
private MessageHandler<FullStateMessage> _messageHandler;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_messageHandler = new MessageHandler<FullStateMessage>();
|
||||
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
||||
}
|
||||
|
||||
private void OnClientReceiveMessage(FullStateMessage message)
|
||||
{
|
||||
PlayerJoin.PlayerNames = message.PlayerNames;
|
||||
}
|
||||
|
||||
public void Send()
|
||||
{
|
||||
var message = new FullStateMessage()
|
||||
{
|
||||
PlayerNames = PlayerJoin.PlayerNames
|
||||
};
|
||||
|
||||
_messageHandler.SendToAll(message);
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ namespace QSB.Events
|
||||
{
|
||||
public class PlayerJoin : NetworkBehaviour
|
||||
{
|
||||
public static readonly Dictionary<uint, string> PlayerNames = new Dictionary<uint, string>();
|
||||
public static Dictionary<uint, string> PlayerNames = new Dictionary<uint, string>();
|
||||
public static string MyName { get; private set; }
|
||||
|
||||
private MessageHandler<JoinMessage> _joinHandler;
|
||||
|
@ -9,7 +9,8 @@ namespace QSB.Messaging
|
||||
AnimTrigger = MsgType.Highest + 3,
|
||||
Join = MsgType.Highest + 4,
|
||||
Death = MsgType.Highest + 5,
|
||||
Leave = MsgType.Highest + 6
|
||||
Leave = MsgType.Highest + 6,
|
||||
FullState = MsgType.Highest + 7
|
||||
// Add other message types here, incrementing the value.
|
||||
}
|
||||
}
|
||||
|
@ -107,6 +107,8 @@
|
||||
<Compile Include="Animation\AnimTriggerMessage.cs" />
|
||||
<Compile Include="Animation\AnimTrigger.cs" />
|
||||
<Compile Include="DebugActions.cs" />
|
||||
<Compile Include="Events\FullStateMessage.cs" />
|
||||
<Compile Include="Events\GameState.cs" />
|
||||
<Compile Include="Events\Necronomicon.cs" />
|
||||
<Compile Include="Events\JoinMessage.cs" />
|
||||
<Compile Include="Events\LeaveMessage.cs" />
|
||||
|
@ -96,19 +96,26 @@ namespace QSB
|
||||
base.OnServerAddPlayer(conn, playerControllerId);
|
||||
|
||||
NetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), conn);
|
||||
|
||||
var gameState = gameObject.AddComponent<GameState>();
|
||||
gameState.Send();
|
||||
}
|
||||
|
||||
public override void OnClientConnect(NetworkConnection conn)
|
||||
{
|
||||
base.OnClientConnect(conn);
|
||||
|
||||
DebugLog.Screen("OnClientConnect");
|
||||
gameObject.AddComponent<SectorSync>();
|
||||
gameObject.AddComponent<PlayerJoin>().Join(_playerName);
|
||||
gameObject.AddComponent<PlayerLeave>();
|
||||
gameObject.AddComponent<RespawnOnDeath>();
|
||||
gameObject.AddComponent<PreventShipDestruction>();
|
||||
|
||||
if (!Network.isServer)
|
||||
{
|
||||
gameObject.AddComponent<GameState>();
|
||||
}
|
||||
|
||||
_canEditName = false;
|
||||
|
||||
OnNetworkManagerReady.Invoke();
|
||||
|
Loading…
x
Reference in New Issue
Block a user