From 22b669264209f79a91d4daffb1add1db2b6433a4 Mon Sep 17 00:00:00 2001 From: Ricardo Lopes Date: Thu, 13 Feb 2020 21:34:01 +0100 Subject: [PATCH] Move remaining sector logic to SectorSync --- QSB/MessageHandler.cs | 2 +- QSB/NetworkPlayer.cs | 7 +++---- QSB/SectorSync.cs | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/QSB/MessageHandler.cs b/QSB/MessageHandler.cs index 2c5bdb2d..0706b8dd 100644 --- a/QSB/MessageHandler.cs +++ b/QSB/MessageHandler.cs @@ -3,7 +3,7 @@ using UnityEngine.Networking; namespace QSB { public abstract class MessageHandler: MonoBehaviour { - public abstract short type { get; } + protected abstract short type { get; } public MessageHandler () { NetworkServer.RegisterHandler(SectorMessage.Type, OnReceiveMessage); diff --git a/QSB/NetworkPlayer.cs b/QSB/NetworkPlayer.cs index c26775a0..cd687e5b 100644 --- a/QSB/NetworkPlayer.cs +++ b/QSB/NetworkPlayer.cs @@ -10,7 +10,7 @@ namespace QSB { void Start () { QSB.Log("Start NetworkPlayer", netId.Value); - SectorSync.playerSectors[netId.Value] = Locator.GetAstroObject(AstroObject.Name.TimberHearth).transform; + SectorSync.SetSector(netId, Sector.Name.TimberHearth); transform.parent = Locator.GetRootTransform(); @@ -29,8 +29,7 @@ namespace QSB { public void EnterSector (Sector sector) { var name = sector.GetName(); if (name != Sector.Name.Unnamed && name != Sector.Name.Ship && name != Sector.Name.Sun) { - SectorSync.playerSectors[netId.Value] = SectorSync.GetSectorByName(sector.GetName()); - + SectorSync.SetSector(netId, sector.GetName()); SectorMessage msg = new SectorMessage(); msg.sectorId = (int) sector.GetName(); msg.senderId = netId.Value; @@ -47,7 +46,7 @@ namespace QSB { return; } - var sectorTransform = SectorSync.playerSectors[netId.Value]; + var sectorTransform = SectorSync.GetSector(netId); if (isLocalPlayer) { transform.position = sectorTransform.InverseTransformPoint(_body.position); transform.rotation = sectorTransform.InverseTransformRotation(_body.rotation); diff --git a/QSB/SectorSync.cs b/QSB/SectorSync.cs index 2a42b87e..8ee58e1e 100644 --- a/QSB/SectorSync.cs +++ b/QSB/SectorSync.cs @@ -4,16 +4,25 @@ using UnityEngine.Networking; namespace QSB { public class SectorSync: MessageHandler { - public override short type { get => MessageType.Sector; } - public static Dictionary playerSectors; + protected override short type { get => MessageType.Sector; } + static Dictionary playerSectors; + static Sector[] _allSectors; void Awake () { playerSectors = new Dictionary(); + _allSectors = FindObjectsOfType(); } - public static Transform GetSectorByName (Sector.Name sectorName) { - var sectors = FindObjectsOfType(); - foreach (var sector in sectors) { + public static void SetSector (NetworkInstanceId netId, Sector.Name sectorName) { + playerSectors[netId.Value] = GetSectorByName(sectorName); + } + + public static Transform GetSector (NetworkInstanceId netId) { + return playerSectors[netId.Value]; + } + + static Transform GetSectorByName (Sector.Name sectorName) { + foreach (var sector in _allSectors) { if (sectorName == sector.GetName()) { return sector.transform; }