4
4
using MelonLoader ;
5
5
using TMPro ;
6
6
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 ;
17
8
18
9
namespace ReplayMod
19
10
{
20
11
public class Main : MelonMod
21
12
{
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 ;
23
22
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
27
25
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 ;
31
28
32
- private int currentRecordingFrames = 0 ;
33
- public int maxRecordingFrames = 360 ;
29
+ private GameObject replayUI ;
30
+ private TextMeshProUGUI replayText ;
31
+ private RectTransform replayTransform ;
34
32
35
- public int replayFrameLength = 2 ;
36
- private int replayFrameTimer = 0 ;
33
+ private GameObject timerObj ;
34
+ private Timer tmr ;
37
35
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 ;
41
40
42
- private float timer = 0f ;
41
+ private GameObject camera ;
42
+ private Transform ctransform ;
43
+
44
+ private float timer = 0f ;
43
45
44
46
public override void OnApplicationStart ( )
45
47
{
@@ -48,17 +50,21 @@ public override void OnApplicationStart()
48
50
49
51
private void GetAllTransforms ( )
50
52
{
51
- foreach ( var gameObj in UnityEngine . Object . FindObjectsOfType < GameObject > ( ) )
53
+ if ( ptransform != null )
52
54
{
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 )
54
65
{
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 ) ;
62
68
}
63
69
}
64
70
}
@@ -76,22 +82,60 @@ public override void OnLevelWasLoaded(int level)
76
82
77
83
if ( level >= 2 )
78
84
{
79
- // Stop any ongoing recording or replaying. Clear the memory stream and transforms list. Else bad stuff happens.. °W°
80
85
if ( recording )
81
86
StopRecording ( ) ;
82
87
83
88
else if ( replaying )
84
89
StopReplaying ( ) ;
85
90
91
+ if ( memoryStream != null && binaryWriter != null )
92
+ ResetReplayFrame ( ) ;
93
+
94
+ ResetReplayFrameTimer ( ) ;
95
+
86
96
if ( memoryStream != null )
87
97
memoryStream . SetLength ( 0 ) ;
88
98
89
99
transforms . Clear ( ) ;
90
100
GetAllTransforms ( ) ;
91
101
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
+ }
95
139
}
96
140
}
97
141
@@ -102,22 +146,33 @@ public override void OnUpdate()
102
146
103
147
if ( Input . GetKeyUp ( KeyCode . T ) )
104
148
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
- }
112
149
}
113
150
114
151
public override void OnFixedUpdate ( )
115
152
{
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
+ }
117
164
UpdateRecording ( ) ;
165
+ }
118
166
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
+ }
120
174
UpdateReplaying ( ) ;
175
+ }
121
176
}
122
177
123
178
public void StartStopRecording ( )
@@ -191,6 +246,9 @@ private void ResetReplayFrame()
191
246
192
247
public void StartStopReplaying ( )
193
248
{
249
+ if ( recording )
250
+ StopRecording ( ) ;
251
+
194
252
if ( ! replaying )
195
253
StartReplaying ( ) ;
196
254
else
@@ -298,12 +356,12 @@ private void LoadState(Transform transform)
298
356
y = binaryReader . ReadSingle ( ) ;
299
357
z = binaryReader . ReadSingle ( ) ;
300
358
transform . eulerAngles = new Vector3 ( x , y , z ) ;
301
-
359
+
302
360
// Velocity
303
361
x = binaryReader . ReadSingle ( ) ;
304
362
y = binaryReader . ReadSingle ( ) ;
305
363
z = binaryReader . ReadSingle ( ) ;
306
- prb . velocity . Set ( x , y , z ) ;
364
+ prb . velocity = new Vector3 ( x , y , z ) ;
307
365
308
366
// Scales
309
367
x = binaryReader . ReadSingle ( ) ;
0 commit comments