quantum-space-buddies/QSB/HUD/MultiplayerHUDManager.cs

237 lines
8.0 KiB
C#
Raw Normal View History

2023-02-10 12:01:31 +00:00
using QSB.HUD.Messages;
using QSB.Messaging;
using QSB.Player;
2023-03-03 20:05:15 +00:00
using QSB.ServerSettings;
2023-02-10 12:01:31 +00:00
using QSB.Utility;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace QSB.HUD;
internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
{
2023-03-04 13:30:50 +00:00
public static MultiplayerHUDManager Instance;
2023-02-10 12:01:31 +00:00
private Transform _playerList;
public static Sprite UnknownSprite;
public static Sprite DeadSprite;
public static Sprite SpaceSprite;
public static Sprite ShipSprite;
public static Sprite TimberHearth;
public static Sprite Attlerock;
public static Sprite CaveTwin;
public static Sprite TowerTwin;
public static Sprite BrittleHollow;
public static Sprite HollowsLantern;
public static Sprite GiantsDeep;
public static Sprite DarkBramble;
public static Sprite Interloper;
public static Sprite WhiteHole;
public static ListStack<HUDIcon> HUDIconStack = new();
private void Start()
{
2023-03-04 13:30:50 +00:00
Instance = this;
2023-02-10 12:01:31 +00:00
GlobalMessenger.AddListener(OWEvents.WakeUp, OnWakeUp);
QSBPlayerManager.OnAddPlayer += OnAddPlayer;
QSBPlayerManager.OnRemovePlayer += OnRemovePlayer;
UnknownSprite = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_unknown.png");
DeadSprite = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_dead.png");
ShipSprite = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_ship.png");
CaveTwin = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_cavetwin.png");
TowerTwin = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_towertwin.png");
TimberHearth = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_timberhearth.png");
Attlerock = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_attlerock.png");
BrittleHollow = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_brittlehollow.png");
HollowsLantern = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_hollowslantern.png");
GiantsDeep = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_giantsdeep.png");
DarkBramble = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_darkbramble.png");
Interloper = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_interloper.png");
WhiteHole = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_whitehole.png");
SpaceSprite = QSBCore.HUDAssetBundle.LoadAsset<Sprite>("Assets/MULTIPLAYER_UI/playerbox_space.png");
}
2023-03-03 20:05:15 +00:00
private void Update()
{
2023-03-04 13:30:50 +00:00
if (!QSBWorldSync.AllObjectsReady || _playerList == null)
{
return;
}
2023-03-03 20:05:15 +00:00
_playerList.gameObject.SetActive(ServerSettingsManager.ShowExtraHUD);
}
2023-02-10 12:01:31 +00:00
private void OnWakeUp()
{
var hudController = Locator.GetPlayerCamera().transform.Find("Helmet").Find("HUDController").GetComponent<HUDCanvas>();
var hudCamera = hudController._hudCamera;
var hudCanvas = hudCamera.transform.parent.Find("UICanvas");
var multiplayerGroup = Instantiate(QSBCore.HUDAssetBundle.LoadAsset<GameObject>("assets/Prefabs/multiplayergroup.prefab"));
multiplayerGroup.transform.parent = hudCanvas;
multiplayerGroup.transform.localPosition = new Vector3(457.4747f, -34.9757f, 41.7683f);
multiplayerGroup.transform.localRotation = Quaternion.Euler(355.0921f, 17.1967f, 359.7854f);
multiplayerGroup.transform.localScale = Vector3.one;
_playerList = multiplayerGroup.transform.Find("PlayerList");
foreach (var player in QSBPlayerManager.PlayerList)
{
AddBox(player);
2023-03-04 13:30:50 +00:00
foreach (var item in QSBWorldSync.GetUnityObjects<Minimap>())
{
AddMinimapMarker(player, item);
}
2023-02-10 12:01:31 +00:00
}
CreateTrigger("TowerTwin_Body/Sector_TowerTwin", HUDIcon.TOWER_TWIN);
CreateTrigger("CaveTwin_Body/Sector_CaveTwin", HUDIcon.CAVE_TWIN);
CreateTrigger("TimberHearth_Body/Sector_TH", HUDIcon.TIMBER_HEARTH);
CreateTrigger("Moon_Body/Sector_THM", HUDIcon.ATTLEROCK);
CreateTrigger("BrittleHollow_Body/Sector_BH", HUDIcon.BRITTLE_HOLLOW);
CreateTrigger("VolcanicMoon_Body/Sector_VM", HUDIcon.HOLLOWS_LANTERN);
CreateTrigger("GiantsDeep_Body/Sector_GD", HUDIcon.GIANTS_DEEP);
CreateTrigger("DarkBramble_Body/Sector_DB", HUDIcon.DARK_BRAMBLE);
CreateTrigger("Comet_Body/Sector_CO", HUDIcon.INTERLOPER);
CreateTrigger("WhiteHole_Body/Sector_WhiteHole", HUDIcon.WHITE_HOLE);
HUDIconStack.Clear();
HUDIconStack.Push(HUDIcon.SPACE);
HUDIconStack.Push(HUDIcon.TIMBER_HEARTH);
new PlanetMessage(HUDIcon.TIMBER_HEARTH).Send();
}
2023-03-04 13:30:50 +00:00
public void UpdateMinimapMarkers(Minimap minimap)
{
foreach (var player in QSBPlayerManager.PlayerList)
{
if (player.IsDead || player.IsLocalPlayer || !player.IsReady)
{
continue;
}
if (player.RulesetDetector.GetPlanetoidRuleset() == null)
{
// it's broken or we in space bois
continue;
}
player.MinimapPlayerMarker.localPosition = GetLocalMapPosition(player, minimap);
player.MinimapPlayerMarker.localRotation = GetLocalMapRotation(player, minimap);
}
}
private void AddMinimapMarker(PlayerInfo player, Minimap minimap)
{
DebugLog.DebugWrite($"Adding Minimap Marker for {player} on minimap {minimap.name}");
player.MinimapPlayerMarker = Instantiate(minimap._playerMarkerTransform);
player.MinimapPlayerMarker.localPosition = Vector3.zero;
player.MinimapPlayerMarker.localRotation = Quaternion.identity;
}
2023-02-10 12:01:31 +00:00
private void AddBox(PlayerInfo player)
{
var box = Instantiate(QSBCore.HUDAssetBundle.LoadAsset<GameObject>("assets/Prefabs/playerbox.prefab"));
box.transform.parent = _playerList;
box.transform.localScale = new Vector3(1, 1, 1);
box.transform.localPosition = Vector3.zero;
box.transform.localRotation = Quaternion.identity;
var boxScript = box.GetComponent<PlayerBox>();
boxScript.AssignPlayer(player);
}
2023-03-04 13:30:50 +00:00
private Vector3 GetLocalMapPosition(PlayerInfo player, Minimap minimap)
{
return Vector3.Scale(player.RulesetDetector.GetPlanetoidRuleset().transform.InverseTransformPoint(player.Body.transform.position).normalized * 0.51f, minimap._globeMeshTransform.localScale);
}
private Quaternion GetLocalMapRotation(PlayerInfo player, Minimap minimap)
{
var planetoidTransform = player.RulesetDetector.GetPlanetoidRuleset().transform;
var quaternion = Quaternion.Inverse(planetoidTransform.rotation);
var vector = quaternion * player.Body.transform.rotation * Vector3.forward;
var vector2 = quaternion * (player.Body.transform.position - transform.position);
return Quaternion.LookRotation(Vector3.ProjectOnPlane(vector, vector2), vector2);
}
2023-02-10 12:01:31 +00:00
private void OnAddPlayer(PlayerInfo player)
{
if (!QSBWorldSync.AllObjectsReady)
{
return;
}
AddBox(player);
2023-03-04 13:30:50 +00:00
foreach (var item in QSBWorldSync.GetUnityObjects<Minimap>())
{
AddMinimapMarker(player, item);
}
2023-02-10 12:01:31 +00:00
}
private void OnRemovePlayer(PlayerInfo player)
{
2023-03-03 17:15:38 +00:00
Destroy(player.HUDBox.gameObject);
2023-02-10 12:01:31 +00:00
}
private PlanetTrigger CreateTrigger(string parentPath, HUDIcon icon)
=> CreateTrigger(Find(parentPath), icon);
private PlanetTrigger CreateTrigger(GameObject parent, HUDIcon icon)
{
if (parent == null)
{
return null;
}
var triggerGO = parent.FindChild("HUD_PLANET_TRIGGER");
if (triggerGO != null)
{
var trigger = triggerGO.GetAddComponent<PlanetTrigger>();
trigger.Icon = icon;
return trigger;
}
else
{
triggerGO = new GameObject("HUD_PLANET_TRIGGER");
triggerGO.transform.SetParent(parent.transform, false);
triggerGO.SetActive(false);
var trigger = triggerGO.AddComponent<PlanetTrigger>();
trigger.Icon = icon;
triggerGO.SetActive(true);
return trigger;
}
}
public static GameObject Find(string path)
{
var go = GameObject.Find(path);
if (go == null)
{
// find inactive use root + transform.find
var names = path.Split('/');
var rootName = names[0];
var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName);
if (root == null)
{
return null;
}
var childPath = string.Join("/", names.Skip(1));
go = root.FindChild(childPath);
}
return go;
}
}