Merge branch 'master' into raicuparta/transformsync-collisions

This commit is contained in:
Ricardo Lopes 2020-05-20 22:28:13 +02:00
commit fee691f750
15 changed files with 171 additions and 18 deletions

View File

@ -3,7 +3,7 @@ using UnityEngine.Networking;
namespace QSB.Events
{
public class DeathMessage : NameMessage
public class DeathMessage : PlayerMessage
{
public override MessageType MessageType => MessageType.Death;

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using QSB.Messaging;
using UnityEngine.Networking;
namespace QSB.Events
{
public class FullStateMessage : PlayerMessage
{
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
View 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);
}
}
}

View File

@ -1,9 +1,24 @@
using QSB.Messaging;
using UnityEngine.Networking;
namespace QSB.Events
{
public class JoinMessage : NameMessage
public class JoinMessage : PlayerMessage
{
public override MessageType MessageType => MessageType.Join;
public string PlayerName { get; set; }
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
PlayerName = reader.ReadString();
}
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(PlayerName);
}
}
}

View File

@ -5,7 +5,7 @@ using UnityEngine.Networking;
namespace QSB.Events
{
public class LeaveMessage : NameMessage
public class LeaveMessage : PlayerMessage
{
public override MessageType MessageType => MessageType.Leave;

View File

@ -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;

View File

@ -19,7 +19,6 @@ namespace QSB.Events
{
var message = new LeaveMessage
{
PlayerName = PlayerJoin.PlayerNames[playerId],
SenderId = playerId,
ObjectIds = objectIds
};
@ -28,7 +27,8 @@ namespace QSB.Events
private void OnClientReceiveMessage(LeaveMessage message)
{
DebugLog.All(message.PlayerName, "left");
var playerName = PlayerJoin.PlayerNames[message.SenderId];
DebugLog.All(playerName, "left");
PlayerJoin.PlayerNames.Remove(message.SenderId);
foreach (var objectId in message.ObjectIds)
{

View File

@ -3,20 +3,17 @@ using UnityEngine.Networking;
namespace QSB.Events
{
public abstract class NameMessage : QSBMessage
public abstract class PlayerMessage : QSBMessage
{
public uint SenderId { get; set; }
public string PlayerName { get; set; }
public override void Deserialize(NetworkReader reader)
{
PlayerName = reader.ReadString();
SenderId = reader.ReadUInt32();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(PlayerName);
writer.Write(SenderId);
}
}

View File

@ -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.
}
}

View File

@ -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" />
@ -114,12 +116,13 @@
<Compile Include="DebugLog.cs" />
<Compile Include="Events\DeathMessage.cs" />
<Compile Include="Events\PlayerJoin.cs" />
<Compile Include="Events\NameMessage.cs" />
<Compile Include="Events\PlayerMessage.cs" />
<Compile Include="Messaging\MessageHandler.cs" />
<Compile Include="Messaging\MessageType.cs" />
<Compile Include="Messaging\QSBMessage.cs" />
<Compile Include="TimeSync\PreventShipDestruction.cs" />
<Compile Include="TimeSync\RespawnOnDeath.cs" />
<Compile Include="TransformSync\PlayerHUDMarker.cs" />
<Compile Include="TransformSync\QuaternionHelper.cs" />
<Compile Include="TimeSync\PreserveTimeScale.cs" />
<Compile Include="TransformSync\RigidbodySync.cs" />

View File

@ -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();

View File

@ -142,7 +142,7 @@ namespace QSB.TimeSync
private void OnClientReceiveMessage(DeathMessage message)
{
var playerName = PlayerJoin.PlayerNames.TryGetValue(message.SenderId, out var n) ? n : message.PlayerName;
var playerName = PlayerJoin.PlayerNames[message.SenderId];
var deathMessage = Necronomicon.GetPhrase(message.DeathType);
DebugLog.All(string.Format(deathMessage, playerName));
}
@ -168,7 +168,6 @@ namespace QSB.TimeSync
{
var message = new DeathMessage
{
PlayerName = PlayerJoin.MyName,
SenderId = PlayerTransformSync.LocalInstance.netId.Value,
DeathType = deathType
};

View File

@ -0,0 +1,53 @@
using QSB.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace QSB.TransformSync
{
class PlayerHUDMarker : HUDDistanceMarker
{
private uint _netId = uint.MaxValue;
private bool _isReady;
protected override void InitCanvasMarker()
{
_markerRadius = 0.3f;
_markerTarget = new GameObject().transform;
_markerTarget.parent = transform;
// I'm not really sure why this has to be 20 instead of 2 (the player height in units).
// But that's the only way it looks right.
_markerTarget.localPosition = Vector3.up * 20;
}
public void SetId(uint netId)
{
_netId = netId;
_isReady = true;
}
protected override void RefreshOwnVisibility()
{
if (_canvasMarker != null)
{
_canvasMarker.SetVisibility(true);
}
}
void Update()
{
if (_isReady && PlayerJoin.PlayerNames.ContainsKey(_netId))
{
_markerLabel = PlayerJoin.PlayerNames[_netId];
_isReady = false;
base.InitCanvasMarker();
}
}
}
}

View File

@ -1,4 +1,6 @@
using QSB.Animation;
using OWML.ModHelper.Events;
using QSB.Animation;
using QSB.Events;
using UnityEngine;
namespace QSB.TransformSync
@ -42,6 +44,9 @@ namespace QSB.TransformSync
rigidBodySync.Init<PlayerBody>(body);
rigidBodySync.IgnoreCollision(Locator.GetShipTransform().gameObject);
var marker = body.gameObject.AddComponent<PlayerHUDMarker>();
marker.SetId(netId.Value);
return body;
}

View File

@ -1,7 +1,7 @@
{
"filename": "QSB.dll",
"author": "Raicuparta",
"name": "QuantumSpaceBuddies",
"name": "Quantum Space Buddies",
"uniqueName": "Raicuparta.QuantumSpaceBuddies",
"version": "0.1",
"owmlVersion": "0.3.37"