-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRoomMirrorer.cs
326 lines (272 loc) · 11.8 KB
/
RoomMirrorer.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using Modding;
using System.Collections.Generic;
using UnityEngine;
using UObject = UnityEngine.Object;
using UCamera = UnityEngine.Camera;
using InControl;
using GlobalEnums;
using System;
using MonoMod.Cil;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMaker;
using System.Reflection;
using System.Collections;
/* Mirroring Code partially from Mimijackz MirroredHallownest Mod
*
*/
namespace Fyrenest
{
/// <summary>
/// Added to cameras to mark whether they have been flipped
/// </summary>
public class IsFlippedComponent : MonoBehaviour
{
public bool flipped = false;
}
/// <summary>
/// Handles horizontal mirroring of rooms
/// </summary>
public class RoomMirrorer
{
private bool isFlipping = false;
/// <summary>
/// Are the inputs currently inverted?
/// </summary>
private bool inputsInverted = false;
/// <summary>
/// List of all objects in this scene that shouldn't be ignored by the text correction code
/// </summary>
List<string> excludedObjects = new List<string>();
//hook all the things that need hooking
public void Hook()
{
On.tk2dCamera.UpdateCameraMatrix += OnUpdateCameraMatrix;
On.GameCameras.StartScene += OnNewSceneCam;
On.UIManager.GoToKeyboardMenu += OnOpenKeyboardMenu;
On.UIManager.GoToRemapControllerMenu += OnOpenGamepadMenu;
On.UIManager.HideCurrentMenu += OnHideMenu;
On.HutongGames.PlayMaker.Actions.SendEventByName.OnEnter += OnSendEventByName;
On.ObjectPool.Spawn_GameObject_Transform_Vector3_Quaternion += OnSpawnObject;
On.GameManager.OnNextLevelReady += OnSceneLoad;
}
/// <summary>
/// Handles inversion of the player controls (since the normal controls are effectively inverted when the camera is flipped)
/// </summary>
public void SetInvertInputs(bool invert)
{
if (invert != inputsInverted)
{
inputsInverted = invert;
PlayerAction tmp = InputHandler.Instance.inputActions.left;
InputHandler.Instance.inputActions.left = InputHandler.Instance.inputActions.right;
InputHandler.Instance.inputActions.right = tmp;
//InputHandler.Instance.inputActions.moveVector.InvertXAxis = !InputHandler.Instance.inputActions.moveVector.InvertXAxis;
InputHandler.Instance.inputActions.moveVector.InvertXAxis = invert;
}
}
/// <summary>
/// Uninvert the controls when the inventory is opened (since it uses a camera that isn't flipped)
/// </summary>
public void OnSendEventByName(On.HutongGames.PlayMaker.Actions.SendEventByName.orig_OnEnter orig, global::HutongGames.PlayMaker.Actions.SendEventByName self)
{
string event_ = self.sendEvent.Value;
if (event_ == "INVENTORY OPENED") SetInvertInputs(false);
if (event_ == "INVENTORY CLOSED") SetInvertInputs(isFlipping);
orig(self);
}
/// <summary>
/// Unflip the controls before any menu is closed (since that action saves the controls in the remapping menus)
/// </summary>
public IEnumerator OnHideMenu(On.UIManager.orig_HideCurrentMenu orig, global::UIManager self)
{
SetInvertInputs(false);
var ret = orig(self);
SetInvertInputs(isFlipping);
return ret;
}
/// <summary>
/// Make sure the keyboard remapping menu is only openable in unflipped rooms
/// </summary>
public IEnumerator OnOpenKeyboardMenu(On.UIManager.orig_GoToKeyboardMenu orig, global::UIManager self)
{
if (isFlipping) return self.GoToOptionsMenu(); //it seems to break stuff, so just don't allow it
SetInvertInputs(false);
return orig(self);
}
/// <summary>
/// Make sure the gamepad remapping menu is only openable in unflipped rooms
/// </summary>
public IEnumerator OnOpenGamepadMenu(On.UIManager.orig_GoToRemapControllerMenu orig, global::UIManager self)
{
if (isFlipping) return self.GoToOptionsMenu(); //it seems to break stuff, so just don't allow it
SetInvertInputs(false);
return orig(self);
}
/// <summary>
/// Called before the scene is loaded, sets whether the current room is flipped
/// </summary>
public void UpdateFlipping()
{
if (Fyrenest.instance.ActiveRoom != null) isFlipping = Fyrenest.instance.ActiveRoom.IsFlipped;
else isFlipping = false;
}
/// <summary>
/// Adds an object to be ignored by the text correction code
/// </summary>
/// <param name="name"></param>
public void AddExcludedObject(string name)
{
excludedObjects.Add(name);
}
/// <summary>
/// Called before a new scene is loaded, clears all previous excluded objects
/// </summary>
public void BeforeSceneLoad()
{
excludedObjects.Clear();
}
/// <summary>
/// Called once a scene has finished loading, ensures the control inversion is correct, and that all text is either flipped or unflipped properly
/// </summary>
private void OnSceneLoad(On.GameManager.orig_OnNextLevelReady orig, global::GameManager self)
{
orig(self);
SetInvertInputs(isFlipping);
foreach (GameObject prefab in GameObject.FindObjectsOfType<GameObject>())
{
//invert all text if it's in a flipped rooms scene
if (isFlipping && prefab.scene.name != "DontDestroyOnLoad" && prefab.scene.name != "HideAndDontSave" && !excludedObjects.Contains(prefab.name))
{
PromptMarker prefabPrompt;
TMPro.TextMeshPro textMesh;
prefab.gameObject.TryGetComponent<PromptMarker>(out prefabPrompt);
prefab.gameObject.TryGetComponent(out textMesh);
if ((prefabPrompt != null || textMesh != null) && prefab.transform.localScale.x > 0)
{
Vector3 oldScale = prefab.transform.localScale;
prefab.transform.localScale = new Vector3(-oldScale.x, oldScale.y, oldScale.z);
}
}
//otherwise, make sure it's not inverted
else
{
PromptMarker prefabPrompt;
TMPro.TextMeshPro textMesh;
prefab.gameObject.TryGetComponent<PromptMarker>(out prefabPrompt);
prefab.gameObject.TryGetComponent(out textMesh);
if ((prefabPrompt != null || textMesh != null) && prefab.transform.localScale.x < 0)
{
Vector3 oldScale = prefab.transform.localScale;
prefab.transform.localScale = new Vector3(-oldScale.x, oldScale.y, oldScale.z);
}
}
}
}
/// <summary>
/// Called when the cameras have finished loading, makes sure they are properly flipped/unflipped
/// </summary>
private void OnNewSceneCam(On.GameCameras.orig_StartScene orig, GameCameras self)
{
orig(self);
if (isFlipping)
{
if (!hasBlurCam(self))
{
return;
}
foreach (UCamera cam in self.tk2dCam.transform.GetComponentsInChildren<UCamera>())
{
if (cam.GetComponent<IsFlippedComponent>() == null) cam.gameObject.AddComponent<IsFlippedComponent>();
if (!cam.GetComponent<IsFlippedComponent>().flipped)
{
cam.GetComponent<IsFlippedComponent>().flipped = true;
FlipUCam(cam);
}
}
}
else
{
foreach (UCamera cam in self.tk2dCam.transform.GetComponentsInChildren<UCamera>())
{
if (cam.GetComponent<IsFlippedComponent>() == null) cam.gameObject.AddComponent<IsFlippedComponent>();
if (cam.GetComponent<IsFlippedComponent>().flipped)
{
Fyrenest.instance.Log("Unflip " + cam.name);
cam.GetComponent<IsFlippedComponent>().flipped = false;
FlipUCam(cam);
}
}
}
}
/// <summary>
/// Flip any newly spawned text if required
/// </summary>
private GameObject OnSpawnObject(On.ObjectPool.orig_Spawn_GameObject_Transform_Vector3_Quaternion orig, GameObject prefab, Transform parent, Vector3 pos, Quaternion rot)
{
prefab = orig(prefab, parent, pos, rot);
//flip text in a flipped room
if (isFlipping && !excludedObjects.Contains(prefab.name))
{
PromptMarker prefabPrompt;
TMPro.TextMeshPro textMesh;
prefab.gameObject.TryGetComponent<PromptMarker>(out prefabPrompt);
prefab.gameObject.TryGetComponent(out textMesh);
if ((prefabPrompt != null || textMesh != null) && prefab.transform.localScale.x > 0)
{
Vector3 oldScale = prefab.transform.localScale;
prefab.transform.localScale = new Vector3(-oldScale.x, oldScale.y, oldScale.z);
}
//otherwise unflip it
}
else
{
PromptMarker prefabPrompt;
TMPro.TextMeshPro textMesh;
prefab.gameObject.TryGetComponent<PromptMarker>(out prefabPrompt);
prefab.gameObject.TryGetComponent(out textMesh);
if ((prefabPrompt != null || textMesh != null) && prefab.transform.localScale.x < 0)
{
Vector3 oldScale = prefab.transform.localScale;
prefab.transform.localScale = new Vector3(-oldScale.x, oldScale.y, oldScale.z);
}
}
return prefab;
}
/// <summary>
/// Make sure the cameras stay flipped
/// </summary>
private void OnUpdateCameraMatrix(On.tk2dCamera.orig_UpdateCameraMatrix orig, tk2dCamera self)
{
orig(self);
if (!isFlipping) return;
if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Menu_Title") return;
// Can't use ?. on a Unity type because they override == to null.
if (GameCameras.instance == null || GameCameras.instance.tk2dCam == null)
return;
UCamera cam = self.GetComponent<UCamera>();
if (cam == null)
return;
Matrix4x4 p = cam.projectionMatrix;
Matrix4x4 _reflectMatrix = Matrix4x4.identity;
_reflectMatrix[0, 0] = -1;
p *= _reflectMatrix;
cam.projectionMatrix = p;
}
/// <summary>
/// Utility function that handles flipping cameras
/// </summary>
void FlipUCam(UCamera cam)
{
Matrix4x4 mat = cam.projectionMatrix;
mat *= Matrix4x4.Scale(new Vector3(-1, 1, 1));
cam.projectionMatrix = mat;
}
/// <summary>
/// Does this scene have a blurry background?
/// </summary>
bool hasBlurCam(GameCameras self)
{
return !(self.tk2dCam == null || self.tk2dCam.transform.GetComponentInChildren<Camera>() == null);
}
}
}