Move remaining sector logic to SectorSync

This commit is contained in:
Ricardo Lopes 2020-02-13 21:34:01 +01:00
parent 2c1d0b1ea5
commit 22b6692642
3 changed files with 18 additions and 10 deletions

View File

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

View File

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

View File

@ -4,16 +4,25 @@ using UnityEngine.Networking;
namespace QSB {
public class SectorSync: MessageHandler {
public override short type { get => MessageType.Sector; }
public static Dictionary<uint, Transform> playerSectors;
protected override short type { get => MessageType.Sector; }
static Dictionary<uint, Transform> playerSectors;
static Sector[] _allSectors;
void Awake () {
playerSectors = new Dictionary<uint, Transform>();
_allSectors = FindObjectsOfType<Sector>();
}
public static Transform GetSectorByName (Sector.Name sectorName) {
var sectors = FindObjectsOfType<Sector>();
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;
}