Repository navigation
Expand file tree
/
Copy pathSpeedModule.cs
More file actions
170 lines (153 loc) · 6.53 KB
/
Copy pathSpeedModule.cs
File metadata and controls
170 lines (153 loc) · 6.53 KB
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
using System;
using System.Reflection;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
namespace HTFToolkit
{
/// <summary>
/// 移动速度倍率:反射改写 PlayerMovement 的基础速度字段
/// (_walkSpeed / _sprintSpeed / _crouchSpeed —— 游戏的过渡系统 _curMoveSpeed 由它们插值而来)。
/// 任意倍数(小数支持),关闭时恢复原值。仅改本地玩家。
/// </summary>
internal static class SpeedModule
{
public static bool Enabled;
/// <summary>最近一次请求的倍率(热键开关复用此值)。</summary>
public static float LastRequested => _currentMult;
private static FieldInfo _walkF;
private static FieldInfo _sprintF;
private static FieldInfo _crouchF;
private static FieldInfo _jumpF;
private static bool _captured;
private static float _origWalk, _origSprint, _origCrouch, _origJump;
private static float _currentMult = 1f;
public static void BindConfig(ConfigFile cfg)
{
}
public static void SetMultiplier(float mult)
{
try
{
if (mult <= 0f)
{
ToastModule.Show("移速倍率需大于 0", Color.yellow);
return;
}
Player p = Player.LocalPlayer;
PlayerMovement mv = p != null ? p.Movement : null;
if (mv == null)
{
ToastModule.Show("移速: 玩家未初始化", Color.yellow);
return;
}
EnsureFields();
if (_walkF == null || _sprintF == null)
{
ToastModule.Show("移速: 游戏字段缺失", Color.yellow);
return;
}
// 首次记录原值(以当前倍率=1 时的状态为准;若已设过倍率则先还原再记录基准)
if (!_captured)
{
_origWalk = (float)_walkF.GetValue(mv);
_origSprint = (float)_sprintF.GetValue(mv);
if (_crouchF != null)
_origCrouch = (float)_crouchF.GetValue(mv);
if (_jumpF != null)
_origJump = (float)_jumpF.GetValue(mv);
_captured = true;
}
_walkF.SetValue(mv, _origWalk * mult);
_sprintF.SetValue(mv, _origSprint * mult);
if (_crouchF != null)
_crouchF.SetValue(mv, _origCrouch * mult);
// 垂直:跳跃/水里上浮的初速也随倍率(跳更高/游更快上浮)
if (_jumpF != null)
_jumpF.SetValue(mv, _origJump * mult);
_currentMult = mult;
Enabled = true;
ToastModule.Show($"移动速度 ×{mult:0.##}(含跳跃/上浮高度)", Color.white);
}
catch (Exception e)
{
Plugin.Log?.LogWarning($"speed set failed: {e.Message}");
ToastModule.Show("移速: 设置失败", Color.yellow);
}
}
public static void Reset()
{
try
{
if (_captured)
{
Player p = Player.LocalPlayer;
PlayerMovement mv = p != null ? p.Movement : null;
if (mv != null)
{
EnsureFields();
_walkF?.SetValue(mv, _origWalk);
_sprintF?.SetValue(mv, _origSprint);
_crouchF?.SetValue(mv, _origCrouch);
_jumpF?.SetValue(mv, _origJump);
}
}
_currentMult = 1f;
Enabled = false;
ToastModule.Show("移动速度已恢复 ×1", Color.white);
}
catch (Exception e)
{
Plugin.Log?.LogWarning($"speed reset failed: {e.Message}");
}
}
private static void EnsureFields()
{
if (_walkF == null)
_walkF = AccessTools.Field(typeof(PlayerMovement), "_walkSpeed");
if (_sprintF == null)
_sprintF = AccessTools.Field(typeof(PlayerMovement), "_sprintSpeed");
if (_crouchF == null)
_crouchF = AccessTools.Field(typeof(PlayerMovement), "_crouchSpeed");
if (_jumpF == null)
_jumpF = AccessTools.Field(typeof(PlayerMovement), "_jumpForce");
}
// ---------------- 菜单 UI(工具页内嵌编辑器)----------------
private static string _inputMult = "2";
private static GUIStyle _fieldStyle;
private static GUIStyle _hintStyle;
/// <summary>工具页内嵌:倍率输入框 + 应用/恢复按钮 + 当前状态。</summary>
public static void DrawEditor()
{
if (Event.current == null)
return;
if (_fieldStyle == null)
{
_fieldStyle = new GUIStyle(GUI.skin.textField)
{
fontSize = UIScale.TextFont,
alignment = TextAnchor.MiddleLeft
};
_hintStyle = new GUIStyle(GUI.skin.label)
{
normal = { textColor = new Color(0.75f, 0.75f, 0.75f) }
};
}
_fieldStyle.fontSize = UIScale.TextFont;
_hintStyle.fontSize = UIScale.HintFont;
GUILayout.BeginHorizontal();
_inputMult = GUILayout.TextField(_inputMult, _fieldStyle, GUILayout.Width(UIScale.W * 0.16f), GUILayout.Height(UIScale.ButtonH));
if (GUILayout.Button("设为该倍率", UIScale.ButtonStyle, GUILayout.Height(UIScale.ButtonH), GUILayout.Width(UIScale.W * 0.22f)))
{
if (float.TryParse((_inputMult ?? "").Trim(), out float v) && v > 0f)
SetMultiplier(v);
else
ToastModule.Show("倍率无效(需大于 0 的数字)", Color.yellow);
}
if (GUILayout.Button("恢复 ×1", UIScale.ButtonStyle, GUILayout.Height(UIScale.ButtonH), GUILayout.Width(UIScale.W * 0.18f)))
Reset();
GUILayout.EndHorizontal();
GUILayout.Label($"当前移速倍率: ×{_currentMult:0.##}(水平移动 + 跳跃/上浮高度同步生效)", _hintStyle);
}
}
}