quantum-space-buddies/QSB/TransformSync/SectorSync.cs

144 lines
4.4 KiB
C#
Raw Normal View History

2020-02-21 22:36:07 +00:00
using System.Collections.Generic;
using QSB.Messaging;
2020-02-13 20:23:12 +00:00
using UnityEngine;
using System.Linq;
using UnityEngine.SceneManagement;
2020-02-13 19:23:26 +00:00
2020-02-21 22:36:07 +00:00
namespace QSB.TransformSync
2020-02-15 19:48:02 +00:00
{
2020-02-21 22:36:07 +00:00
public class SectorSync : MonoBehaviour
2020-02-15 19:48:02 +00:00
{
2020-02-21 22:36:07 +00:00
public static SectorSync Instance { get; private set; }
2020-02-21 22:36:07 +00:00
private Dictionary<uint, Transform> _playerSectors;
private Sector[] _allSectors;
private MessageHandler<SectorMessage> _sectorHandler;
private readonly Sector.Name[] _sectorWhitelist = {
Sector.Name.BrambleDimension,
Sector.Name.BrittleHollow,
Sector.Name.Comet,
Sector.Name.DarkBramble,
Sector.Name.EyeOfTheUniverse,
Sector.Name.GiantsDeep,
Sector.Name.HourglassTwin_A,
Sector.Name.HourglassTwin_B,
Sector.Name.OrbitalProbeCannon,
Sector.Name.QuantumMoon,
Sector.Name.SunStation,
Sector.Name.TimberHearth,
Sector.Name.TimberMoon,
Sector.Name.VolcanicMoon,
Sector.Name.WhiteHole
};
2020-02-13 20:23:12 +00:00
private void Awake()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
2020-02-15 19:48:02 +00:00
private void Start()
{
2020-02-21 22:36:07 +00:00
Instance = this;
DebugLog.Screen("Start SectorSync");
2020-02-15 19:48:02 +00:00
_playerSectors = new Dictionary<uint, Transform>();
2020-02-13 20:48:23 +00:00
2020-02-21 22:36:07 +00:00
_sectorHandler = new MessageHandler<SectorMessage>();
_sectorHandler.OnClientReceiveMessage += OnClientReceiveMessage;
_sectorHandler.OnServerReceiveMessage += OnServerReceiveMessage;
QSB.Helper.HarmonyHelper.AddPrefix<SectorDetector>("AddSector", typeof(Patches), "PreAddSector");
2020-02-13 20:23:12 +00:00
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
_allSectors = null;
}
2020-02-21 22:36:07 +00:00
public void SetSector(uint id, Transform sectorTransform)
2020-02-15 19:48:02 +00:00
{
_playerSectors[id] = sectorTransform;
}
2020-02-13 21:18:41 +00:00
2020-02-21 22:36:07 +00:00
public void SetSector(uint id, Sector.Name sectorName)
2020-02-15 19:48:02 +00:00
{
DebugLog.Screen("Gonna set sector");
2020-02-15 19:48:02 +00:00
_playerSectors[id] = FindSectorTransform(sectorName);
2020-02-15 19:48:02 +00:00
var msg = new SectorMessage
{
SectorId = (int)sectorName,
SenderId = id
};
2020-02-21 22:36:07 +00:00
_sectorHandler.SendToServer(msg);
}
2020-02-21 22:36:07 +00:00
public Transform GetSector(uint id)
2020-02-15 19:48:02 +00:00
{
return _playerSectors[id];
}
2020-02-21 22:36:07 +00:00
private Transform FindSectorTransform(Sector.Name sectorName)
2020-02-15 19:48:02 +00:00
{
if (_allSectors == null)
{
_allSectors = FindObjectsOfType<Sector>();
}
2020-02-15 19:48:02 +00:00
foreach (var sector in _allSectors)
{
if (sectorName == sector.GetName())
{
2020-02-13 20:23:12 +00:00
return sector.transform;
}
}
return null;
}
2020-02-21 22:36:07 +00:00
private void OnClientReceiveMessage(SectorMessage message)
2020-02-15 19:48:02 +00:00
{
DebugLog.Screen("OnClientReceiveMessage SectorSync");
2020-02-13 20:23:12 +00:00
2020-02-21 22:36:07 +00:00
var sectorName = (Sector.Name)message.SectorId;
var sectorTransform = FindSectorTransform(sectorName);
2020-02-13 20:23:12 +00:00
2020-02-15 19:48:02 +00:00
if (sectorTransform == null)
{
DebugLog.Screen("Sector", sectorName, "not found");
2020-02-13 20:23:12 +00:00
return;
}
2020-02-21 22:36:07 +00:00
DebugLog.Screen("Found sector", sectorName, ", setting for", message.SenderId);
_playerSectors[message.SenderId] = sectorTransform;
2020-02-13 20:23:12 +00:00
}
2020-02-13 20:48:23 +00:00
2020-02-21 22:36:07 +00:00
private void OnServerReceiveMessage(SectorMessage message)
2020-02-15 19:48:02 +00:00
{
DebugLog.Screen("OnServerReceiveMessage SectorSync");
2020-02-21 22:36:07 +00:00
_sectorHandler.SendToAll(message);
2020-02-13 20:48:23 +00:00
}
2020-02-15 19:48:02 +00:00
private static class Patches
{
private static void PreAddSector(Sector sector, DynamicOccupant ____occupantType)
2020-02-15 19:48:02 +00:00
{
if (!Instance._sectorWhitelist.Contains(sector.GetName()))
2020-02-15 19:48:02 +00:00
{
return;
}
2020-02-21 22:36:07 +00:00
if (____occupantType == DynamicOccupant.Player && PlayerTransformSync.LocalInstance != null)
2020-02-15 19:48:02 +00:00
{
2020-02-21 22:36:07 +00:00
PlayerTransformSync.LocalInstance.EnterSector(sector);
return;
}
if (____occupantType == DynamicOccupant.Ship && ShipTransformSync.LocalInstance != null)
{
ShipTransformSync.LocalInstance.EnterSector(sector);
2020-02-13 20:48:23 +00:00
}
}
}
2020-02-15 19:48:02 +00:00
2020-02-13 19:23:26 +00:00
}
}