/
StarChartHandler.cs
113 lines (93 loc) · 4.42 KB
/
StarChartHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using NewHorizons.Components.ShipLog;
using NewHorizons.External;
using NewHorizons.OtherMods.CustomShipLogModes;
using NewHorizons.Utility;
using NewHorizons.Utility.OWML;
using System.Collections.Generic;
using UnityEngine;
namespace NewHorizons.Handlers
{
public static class StarChartHandler
{
public static ShipLogStarChartMode ShipLogStarChartMode;
private static Dictionary<string, string> _starSystemToFactID;
private static Dictionary<string, string> _factIDToStarSystem;
private static NewHorizonsSystem[] _systems;
public static void Init(NewHorizonsSystem[] systems)
{
_systems = systems;
var shipLogRoot = SearchUtilities.Find("Ship_Body/Module_Cabin/Systems_Cabin/ShipLogPivot/ShipLog/ShipLogPivot/ShipLogCanvas");
var reticleImage = SearchUtilities.Find("Ship_Body/Module_Cabin/Systems_Cabin/ShipLogPivot/ShipLog/ShipLogPivot/ShipLogCanvas/DetectiveMode/ReticleImage (1)/");
if (shipLogRoot != null && reticleImage != null)
{
var starChartLog = new GameObject("StarChartMode");
starChartLog.SetActive(false);
starChartLog.transform.parent = shipLogRoot.transform;
starChartLog.transform.localScale = Vector3.one * 1f;
starChartLog.transform.localPosition = Vector3.zero;
starChartLog.transform.localRotation = Quaternion.Euler(0, 0, 0);
ShipLogStarChartMode = starChartLog.AddComponent<ShipLogStarChartMode>();
Object.Instantiate(reticleImage, starChartLog.transform);
var scaleRoot = new GameObject("ScaleRoot");
scaleRoot.transform.parent = starChartLog.transform;
scaleRoot.transform.localScale = Vector3.one;
scaleRoot.transform.localPosition = Vector3.zero;
scaleRoot.transform.localRotation = Quaternion.Euler(0, 0, 0);
var panRoot = new GameObject("PanRoot");
panRoot.transform.parent = scaleRoot.transform;
panRoot.transform.localScale = Vector3.one;
panRoot.transform.localPosition = Vector3.zero;
panRoot.transform.localRotation = Quaternion.Euler(0, 0, 0);
CustomShipLogModesHandler.AddInterstellarMode();
}
_starSystemToFactID = new Dictionary<string, string>();
_factIDToStarSystem = new Dictionary<string, string>();
foreach (NewHorizonsSystem system in _systems)
{
if (system.Config.factRequiredForWarp != default)
{
RegisterFactForSystem(system.Config.factRequiredForWarp, system.UniqueID);
}
}
}
public static bool CanWarp()
{
foreach (var system in _systems)
{
if (system.Config.canEnterViaWarpDrive && system.Spawn?.shipSpawn != null && HasUnlockedSystem(system.UniqueID))
{
return true;
}
}
return false;
}
public static bool HasUnlockedSystem(string system)
{
if (_starSystemToFactID == null || _starSystemToFactID.Count == 0)
return true;
// If we can't get a fact for the system, then its unlocked
if (!_starSystemToFactID.TryGetValue(system, out var factID))
return true;
// It's unlocked if known
return ShipLogHandler.KnowsFact(factID);
}
public static void OnRevealFact(string factID)
{
if (_factIDToStarSystem.TryGetValue(factID, out var systemUnlocked))
{
NHLogger.Log($"Just learned [{factID}] and unlocked [{systemUnlocked}]");
if (!Main.HasWarpDrive)
Main.Instance.EnableWarpDrive();
if (ShipLogStarChartMode != null)
ShipLogStarChartMode.AddSystemCard(systemUnlocked);
}
}
public static void RegisterFactForSystem(string factID, string system)
{
NHLogger.LogVerbose($"Need to know [{factID}] to unlock [{system}]");
_starSystemToFactID.Add(system, factID);
_factIDToStarSystem.Add(factID, system);
}
public static bool IsWarpDriveLockedOn() => StarChartHandler.ShipLogStarChartMode.GetTargetStarSystem() != null;
}
}