Skip to content

Commit aefd1b8

Browse files
committed
Fixed velocity, crash, instant win, doors
1 parent f37fd2b commit aefd1b8

File tree

1 file changed

+107
-49
lines changed

1 file changed

+107
-49
lines changed

KarlsonUtils/Main.cs

+107-49
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,44 @@
44
using MelonLoader;
55
using TMPro;
66
using UnityEngine;
7-
8-
/*
9-
10-
TOFIX:
11-
- Enemies // Low prio
12-
- Barrels // Low prio
13-
- Glass // Low prio
14-
- Doors // High prio (cuz it breaks the fucking gameeeee)
15-
16-
*/
7+
using UnityEngine.UI;
178

189
namespace ReplayMod
1910
{
2011
public class Main : MelonMod
2112
{
22-
private List<Transform> transforms = new List<Transform>();
13+
private List<Transform> transforms = new List<Transform>();
14+
15+
private MemoryStream memoryStream = null;
16+
private BinaryWriter binaryWriter = null;
17+
private BinaryReader binaryReader = null;
18+
19+
private bool recordingInitialized = false;
20+
private bool recording = false;
21+
private bool replaying = false;
2322

24-
private MemoryStream memoryStream = null;
25-
private BinaryWriter binaryWriter = null;
26-
private BinaryReader binaryReader = null;
23+
private int currentRecordingFrames = 0;
24+
public int maxRecordingFrames = 1600; // approx. 30 - 40 seconds of recording
2725

28-
private bool recordingInitialized = false;
29-
private bool recording = false;
30-
private bool replaying = false;
26+
public int replayFrameLength = 2;
27+
private int replayFrameTimer = 0;
3128

32-
private int currentRecordingFrames = 0;
33-
public int maxRecordingFrames = 360;
29+
private GameObject replayUI;
30+
private TextMeshProUGUI replayText;
31+
private RectTransform replayTransform;
3432

35-
public int replayFrameLength = 2;
36-
private int replayFrameTimer = 0;
33+
private GameObject timerObj;
34+
private Timer tmr;
3735

38-
private Timer tmr = null;
39-
private PlayerMovement pm = null;
40-
private Rigidbody prb = null;
36+
private GameObject player;
37+
private PlayerMovement pm;
38+
private Rigidbody prb;
39+
private Transform ptransform;
4140

42-
private float timer = 0f;
41+
private GameObject camera;
42+
private Transform ctransform;
43+
44+
private float timer = 0f;
4345

4446
public override void OnApplicationStart()
4547
{
@@ -48,17 +50,21 @@ public override void OnApplicationStart()
4850

4951
private void GetAllTransforms()
5052
{
51-
foreach (var gameObj in UnityEngine.Object.FindObjectsOfType<GameObject>())
53+
if (ptransform != null)
5254
{
53-
if (gameObj.transform != null)
55+
foreach (Transform child in ptransform)
56+
{
57+
if (!transforms.Contains(ptransform))
58+
transforms.Add(GameObject.Find("Player").transform);
59+
60+
if (!transforms.Contains(child.transform))
61+
transforms.Add(child.transform);
62+
}
63+
64+
if (ctransform != null)
5465
{
55-
if (!transforms.Contains(gameObj.transform))
56-
{
57-
if (gameObj.layer != 5 && gameObj.layer != 9 && gameObj.GetComponent<Lava>() == null && gameObj.GetComponent<Light>() == null && gameObj.GetComponent<Glass>() == null && gameObj.GetComponent<Barrel>() == null && gameObj.GetComponent<Break>() == null)
58-
{
59-
transforms.Add(gameObj.transform);
60-
}
61-
}
66+
if (!transforms.Contains(ctransform))
67+
transforms.Add(ctransform);
6268
}
6369
}
6470
}
@@ -76,22 +82,60 @@ public override void OnLevelWasLoaded(int level)
7682

7783
if (level >= 2)
7884
{
79-
// Stop any ongoing recording or replaying. Clear the memory stream and transforms list. Else bad stuff happens.. °W°
8085
if (recording)
8186
StopRecording();
8287

8388
else if (replaying)
8489
StopReplaying();
8590

91+
if (memoryStream != null && binaryWriter != null)
92+
ResetReplayFrame();
93+
94+
ResetReplayFrameTimer();
95+
8696
if (memoryStream != null)
8797
memoryStream.SetLength(0);
8898

8999
transforms.Clear();
90100
GetAllTransforms();
91101

92-
tmr = GameObject.Find("Managers (1)/UI/Game/Timer").GetComponent<Timer>();
93-
pm = GameObject.Find("Player").GetComponent<PlayerMovement>();
94-
prb = GameObject.Find("Player").GetComponent<Rigidbody>();
102+
timerObj = GameObject.Find("Managers (1)/UI/Game/Timer");
103+
tmr = timerObj.GetComponent<Timer>();
104+
105+
player = GameObject.Find("Player");
106+
pm = player.GetComponent<PlayerMovement>();
107+
prb = player.GetComponent<Rigidbody>();
108+
ptransform = player.transform;
109+
110+
camera = GameObject.Find("Camera");
111+
ctransform = camera.transform;
112+
113+
// ReplayUI setup
114+
if (GameObject.Find("Managers (1)/UI/ReplayText") == null)
115+
{
116+
replayUI = new GameObject("ReplayText");
117+
replayUI.layer = 5; // UI
118+
replayUI.transform.parent = GameObject.Find("Managers (1)/UI").transform;
119+
replayUI.transform.localPosition = new Vector3(0, 0, 0);
120+
replayUI.transform.localScale = new Vector3(1, 1, 1);
121+
replayUI.SetActive(false);
122+
123+
replayText = replayUI.AddComponent<TextMeshProUGUI>();
124+
}
125+
126+
if (replayText != null)
127+
{
128+
// Text
129+
replayText.font = timerObj.GetComponent<TextMeshProUGUI>().font;
130+
replayText.richText = true;
131+
replayText.text = "owo";
132+
replayText.fontSize = 32f;
133+
134+
// Text position
135+
replayTransform = replayText.GetComponent<RectTransform>();
136+
replayTransform.localPosition = new Vector3(-290f, 200f, 0f);
137+
replayTransform.sizeDelta = new Vector2(200, 50);
138+
}
95139
}
96140
}
97141

@@ -102,22 +146,33 @@ public override void OnUpdate()
102146

103147
if (Input.GetKeyUp(KeyCode.T))
104148
StartStopReplaying();
105-
106-
if (Input.GetKeyUp(KeyCode.Y)) // Debug stuff to force update the transform list.
107-
{
108-
transforms.Clear();
109-
GetAllTransforms();
110-
MelonLogger.Log("Forced transforms : " + transforms.Count);
111-
}
112149
}
113150

114151
public override void OnFixedUpdate()
115152
{
116-
if (recording)
153+
if (!replaying || !recording)
154+
if (replayUI != null)
155+
replayUI.SetActive(false);
156+
157+
if (recording && replayUI != null)
158+
{
159+
if (!replayUI.activeSelf)
160+
{
161+
replayText.text = "<color=red>Recording<color=white>";
162+
replayUI.SetActive(true);
163+
}
117164
UpdateRecording();
165+
}
118166

119-
else if (replaying)
167+
if (replaying && replayUI != null)
168+
{
169+
if (!replayUI.activeSelf)
170+
{
171+
replayText.text = "<color=red>Replaying<color=white>";
172+
replayUI.SetActive(true);
173+
}
120174
UpdateReplaying();
175+
}
121176
}
122177

123178
public void StartStopRecording()
@@ -191,6 +246,9 @@ private void ResetReplayFrame()
191246

192247
public void StartStopReplaying()
193248
{
249+
if (recording)
250+
StopRecording();
251+
194252
if (!replaying)
195253
StartReplaying();
196254
else
@@ -298,12 +356,12 @@ private void LoadState(Transform transform)
298356
y = binaryReader.ReadSingle();
299357
z = binaryReader.ReadSingle();
300358
transform.eulerAngles = new Vector3(x, y, z);
301-
359+
302360
// Velocity
303361
x = binaryReader.ReadSingle();
304362
y = binaryReader.ReadSingle();
305363
z = binaryReader.ReadSingle();
306-
prb.velocity.Set(x, y, z);
364+
prb.velocity = new Vector3(x, y, z);
307365

308366
// Scales
309367
x = binaryReader.ReadSingle();

0 commit comments

Comments
 (0)