diff options
Diffstat (limited to 'GameState.cs')
-rw-r--r-- | GameState.cs | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/GameState.cs b/GameState.cs new file mode 100644 index 0000000..d3bea06 --- /dev/null +++ b/GameState.cs | |||
@@ -0,0 +1,167 @@ | |||
1 | using System; | ||
2 | using System.Linq; | ||
3 | using System.Reflection; | ||
4 | using UnityEngine; | ||
5 | using UnityEngine.SceneManagement; | ||
6 | |||
7 | namespace ManifoldGardenArchipelago | ||
8 | { | ||
9 | public class GameState | ||
10 | { | ||
11 | public static LevelSystems GetLevelSystems(Component component) | ||
12 | { | ||
13 | return component.gameObject.scene.GetRootGameObjects().Single((obj) => obj.name == "Level Systems").GetComponent<LevelSystems>(); | ||
14 | } | ||
15 | |||
16 | public static LevelSystems GetLevelSystems(Scene scene) | ||
17 | { | ||
18 | return scene.GetRootGameObjects().Single((obj) => obj.name == "Level Systems").GetComponent<LevelSystems>(); | ||
19 | } | ||
20 | |||
21 | public static SceneItemReference GetChainListenerSceneReference(Component component) | ||
22 | { | ||
23 | LevelSystems levelSystem = GetLevelSystems(component); | ||
24 | |||
25 | for (int i = 0; i < levelSystem.chainListeners.Count(); i++) | ||
26 | { | ||
27 | if (levelSystem.chainListeners[i] == component) | ||
28 | { | ||
29 | return new(component.gameObject.scene.name, i); | ||
30 | } | ||
31 | } | ||
32 | |||
33 | throw new Exception("Shouldn't happen"); | ||
34 | } | ||
35 | |||
36 | public static SceneItemReference GetButtonSceneReference(ButtonLineActivator arg) | ||
37 | { | ||
38 | LevelSystems levelSystem = GetLevelSystems(arg); | ||
39 | |||
40 | for (int i = 0; i < levelSystem.buttonLineActivators.Count(); i++) | ||
41 | { | ||
42 | if (levelSystem.buttonLineActivators[i] == arg) | ||
43 | { | ||
44 | return new(arg.gameObject.scene.name, i); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | throw new Exception("Shouldn't happen"); | ||
49 | } | ||
50 | |||
51 | public static SceneItemReference GetPadSceneReference(CubeLineActivator arg) | ||
52 | { | ||
53 | LevelSystems levelSystem = GetLevelSystems(arg); | ||
54 | |||
55 | for (int i = 0; i < levelSystem.cubeLineActivators.Count(); i++) | ||
56 | { | ||
57 | if (levelSystem.cubeLineActivators[i] == arg) | ||
58 | { | ||
59 | return new(arg.gameObject.scene.name, i); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | throw new Exception("Shouldn't happen"); | ||
64 | } | ||
65 | |||
66 | public static SceneItemReference GetSocketSceneReference(CubeReceiverController arg) | ||
67 | { | ||
68 | LevelSystems levelSystem = GetLevelSystems(arg); | ||
69 | |||
70 | for (int i = 0; i < levelSystem.cubeReceiverControllers.Count(); i++) | ||
71 | { | ||
72 | if (levelSystem.cubeReceiverControllers[i] == arg) | ||
73 | { | ||
74 | return new(arg.gameObject.scene.name, i); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | throw new Exception("Shouldn't happen"); | ||
79 | } | ||
80 | |||
81 | public static SceneItemReference GetSphereSceneReference(SphereController arg) | ||
82 | { | ||
83 | LevelSystems levelSystem = GetLevelSystems(arg); | ||
84 | |||
85 | for (int i = 0; i < levelSystem.sphereControllers.Count(); i++) | ||
86 | { | ||
87 | if (levelSystem.sphereControllers[i] == arg) | ||
88 | { | ||
89 | return new(arg.gameObject.scene.name, i); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | throw new Exception("Shouldn't happen"); | ||
94 | } | ||
95 | |||
96 | public static SceneItemReference GetWaterwheelSceneReference(WaterDetector arg) | ||
97 | { | ||
98 | LevelSystems levelSystem = GetLevelSystems(arg); | ||
99 | |||
100 | for (int i = 0; i < levelSystem.waterDetectors.Count(); i++) | ||
101 | { | ||
102 | if (levelSystem.waterDetectors[i] == arg) | ||
103 | { | ||
104 | return new(arg.gameObject.scene.name, i); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | throw new Exception("Shouldn't happen"); | ||
109 | } | ||
110 | |||
111 | public static void EvaluateGameStateListeners(GameStateListeners listeners) | ||
112 | { | ||
113 | foreach (var location in listeners.locations) | ||
114 | { | ||
115 | if (location.Value.Check()) | ||
116 | { | ||
117 | Plugin.archipelagoManager.CheckLocation(location.Key); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | foreach (var door in listeners.doors) | ||
122 | { | ||
123 | bool shouldOpen = door.Value.Check(); | ||
124 | Plugin.Logger.LogInfo($"{door.Key}: {door.Value} -> {shouldOpen}"); | ||
125 | |||
126 | if (SceneManager.GetSceneByName(door.Key.scene) is Scene doorScene && doorScene.isLoaded) | ||
127 | { | ||
128 | LevelSystems levelSystems = GetLevelSystems(doorScene); | ||
129 | if (levelSystems.isActiveAndEnabled) | ||
130 | { | ||
131 | LineDoorController ldc = levelSystems.chainListeners[door.Key.index].GetComponent<LineDoorController>(); | ||
132 | ldc.chains.Clear(); | ||
133 | |||
134 | FieldInfo fieldInfo = typeof(LineDoorController).GetField("doorShouldOpenImmediatelyOnEnable", BindingFlags.Instance | BindingFlags.NonPublic); | ||
135 | fieldInfo.SetValue(ldc, shouldOpen); | ||
136 | |||
137 | MethodInfo methodInfo = typeof(LineDoorController).GetMethod("SetDoorOpenCloseStateAnimated", BindingFlags.NonPublic | BindingFlags.Instance); | ||
138 | methodInfo.Invoke(ldc, [shouldOpen, false]); | ||
139 | |||
140 | ldc.saveData.isOpen = shouldOpen; | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | |||
146 | |||
147 | foreach (var worldGrow in listeners.worldGrows) | ||
148 | { | ||
149 | if (worldGrow.Value.Check()) | ||
150 | { | ||
151 | if (SceneManager.GetSceneByName(worldGrow.Key.scene) is Scene gardenScene && gardenScene.isLoaded) | ||
152 | { | ||
153 | LevelSystems levelSystems = GetLevelSystems(gardenScene); | ||
154 | if (levelSystems.isActiveAndEnabled) | ||
155 | { | ||
156 | DarkModeCollapsedCubeWorldGrow ldc = levelSystems.chainListeners[worldGrow.Key.index].GetComponent<DarkModeCollapsedCubeWorldGrow>(); | ||
157 | ldc.chains.Clear(); | ||
158 | |||
159 | FieldInfo fieldInfo = typeof(DarkModeCollapsedCubeWorldGrow).GetField("m_grown", BindingFlags.Instance | BindingFlags.NonPublic); | ||
160 | fieldInfo.SetValue(ldc, true); | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | } | ||