Move logic to Network Manager

This commit is contained in:
Ricardo Lopes 2020-02-13 20:46:07 +01:00
parent e454653c29
commit b52c1dac07
3 changed files with 38 additions and 42 deletions

View File

@ -10,7 +10,7 @@ namespace QSB {
void Start () {
QSB.Log("Start NetworkPlayer", netId.Value);
QSB.playerSectors[netId.Value] = Locator.GetAstroObject(AstroObject.Name.TimberHearth).transform;
QSBNetworkManager.playerSectors[netId.Value] = Locator.GetAstroObject(AstroObject.Name.TimberHearth).transform;
transform.parent = Locator.GetRootTransform();
@ -24,19 +24,12 @@ namespace QSB {
_body.Find("player_mesh_noSuit:Traveller_HEA_Player/player_mesh_noSuit:Player_Head").gameObject.layer = 0;
_body.Find("Traveller_Mesh_v01:Traveller_Geo/Traveller_Mesh_v01:PlayerSuit_Helmet").gameObject.layer = 0;
}
// It's dumb that this is here, should be somewhere else.
if (isServer) {
NetworkServer.RegisterHandler(MsgType.Highest + 1, QSB.OnReceiveMessage);
} else {
NetworkManager.singleton.client.RegisterHandler(SectorMessage.Type, QSB.OnReceiveMessage);
}
}
public void EnterSector (Sector sector) {
var name = sector.GetName();
if (name != Sector.Name.Unnamed && name != Sector.Name.Ship && name != Sector.Name.Sun) {
QSB.playerSectors[netId.Value] = QSB.GetSectorByName(sector.GetName());
QSBNetworkManager.playerSectors[netId.Value] = QSBNetworkManager.GetSectorByName(sector.GetName());
SectorMessage msg = new SectorMessage();
msg.sectorId = (int) sector.GetName();
@ -54,7 +47,7 @@ namespace QSB {
return;
}
var sectorTransform = QSB.playerSectors[netId.Value];
var sectorTransform = QSBNetworkManager.playerSectors[netId.Value];
if (isLocalPlayer) {
transform.position = sectorTransform.InverseTransformPoint(_body.position);
transform.rotation = sectorTransform.InverseTransformRotation(_body.rotation);

View File

@ -8,7 +8,6 @@ namespace QSB {
public class QSB: ModBehaviour {
public static IModHelper Helper;
static QSB _instance;
public static Dictionary<uint, Transform> playerSectors;
void Awake () {
Application.runInBackground = true;
@ -25,8 +24,6 @@ namespace QSB {
_instance = this;
Helper = ModHelper;
playerSectors = new Dictionary<uint, Transform>();
gameObject.AddComponent<QSBNetworkManager>();
gameObject.AddComponent<NetworkManagerHUD>();
@ -55,33 +52,6 @@ namespace QSB {
NotificationManager.SharedInstance.PostNotification(data, false);
}
public static Transform GetSectorByName (Sector.Name sectorName) {
var sectors = GameObject.FindObjectsOfType<Sector>();
foreach (var sector in sectors) {
if (sectorName == sector.GetName()) {
return sector.transform;
}
}
return null;
}
public static void OnReceiveMessage (NetworkMessage netMsg) {
QSB.LogToScreen("Global message receive");
SectorMessage msg = netMsg.ReadMessage<SectorMessage>();
var sectorName = (Sector.Name) msg.sectorId;
var sectorTransform = GetSectorByName(sectorName);
if (sectorTransform == null) {
QSB.LogToScreen("Sector", sectorName, "not found");
return;
}
QSB.LogToScreen("Found sector", sectorName, ", setting for", msg.senderId);
playerSectors[msg.senderId] = sectorTransform;
}
static class Patches {
static void OnAddSector (Sector sector, PlayerSectorDetector __instance) {
if (NetworkPlayer.localInstance != null) {

View File

@ -1,18 +1,51 @@
using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace QSB {
class QSBNetworkManager: NetworkManager {
public static Dictionary<uint, Transform> playerSectors;
void Awake () {
var assetBundle = QSB.Helper.Assets.LoadBundle("assets/network");
playerPrefab = assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
playerPrefab.AddComponent<NetworkPlayer>();
playerSectors = new Dictionary<uint, Transform>();
}
public override void OnStartClient (NetworkClient client) {
base.OnStartClient(client);
QSB.Log("start client");
NetworkServer.RegisterHandler(SectorMessage.Type, OnReceiveMessage);
client.RegisterHandler(SectorMessage.Type, OnReceiveMessage);
}
public static Transform GetSectorByName (Sector.Name sectorName) {
var sectors = GameObject.FindObjectsOfType<Sector>();
foreach (var sector in sectors) {
if (sectorName == sector.GetName()) {
return sector.transform;
}
}
return null;
}
public static void OnReceiveMessage (NetworkMessage netMsg) {
QSB.LogToScreen("Global message receive");
SectorMessage msg = netMsg.ReadMessage<SectorMessage>();
var sectorName = (Sector.Name) msg.sectorId;
var sectorTransform = GetSectorByName(sectorName);
if (sectorTransform == null) {
QSB.LogToScreen("Sector", sectorName, "not found");
return;
}
QSB.LogToScreen("Found sector", sectorName, ", setting for", msg.senderId);
playerSectors[msg.senderId] = sectorTransform;
}
}
}