Repository navigation
Expand file tree
/
Copy pathItemGalleryModule.cs
More file actions
303 lines (273 loc) · 12.5 KB
/
Copy pathItemGalleryModule.cs
File metadata and controls
303 lines (273 loc) · 12.5 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
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
using System.Collections.Generic;
using BepInEx.Configuration;
using UnityEngine;
namespace HTFToolkit
{
/// <summary>
/// 物品图鉴:所有非生物物品以网格展示(每格 = 3D 预览图 + 名称),
/// 点击格子 = 获得该物品。
/// 主机/单人:本地 Instantiate + Server.Spawn。
/// 客户端:走游戏本体"购买"服务器通道 BuyItem(isFree=true)——零校验 ServerRpc,
/// 与商店购买同款校验(需携带当前手上物品引用 HeldItem),免费跳过扣钱。
/// 名称走 Item.GetName()(Unity Localization 本地化,中文游戏显示中文名)。
/// 预览图分帧渲染(PreviewQueue),边加载边显示。
/// </summary>
internal static class ItemGalleryModule
{
private static List<Item> _items;
private static bool _requested;
private static int _page;
private static readonly Dictionary<Item, string> _nameCache = new Dictionary<Item, string>();
private static GUIStyle _nameStyle;
private static GUIStyle _hintStyle;
public static void BindConfig(ConfigFile cfg)
{
}
private static bool CanUse
{
get
{
try
{
return Compat.IsHost || Compat.ClientRpcReady;
}
catch
{
return false;
}
}
}
private static void EnsureItems()
{
if (_items != null)
return;
_items = new List<Item>();
Item[] all = Resources.LoadAll<Item>("Items");
foreach (Item it in all)
{
if (it != null)
_items.Add(it);
}
// 活鱼/生物在 Items/Creatures 下,单独并入(GameInfo._allItems 收录二者 → 客户端 BuyItem 也能拿到)
// ⚠️ Boss 排除:Boss 有专属生成/掉落流程,图鉴生成的 boss AI 不动、死后尸体无法拾取
Creature[] creatures = Resources.LoadAll<Creature>("Items/Creatures");
foreach (Creature c in creatures)
{
if (c != null && c.BossType == BossType.None && !_items.Contains(c))
_items.Add(c);
}
_items.Sort((a, b) => a.ID.CompareTo(b.ID));
// 预取一次显示名并缓存(本地化就绪时 GetName() 即中文;菜单绘制逐格调 GetName() 会卡顿)
_nameCache.Clear();
foreach (Item it in _items)
_ = ItemName(it);
PreviewQueue.Reset();
PreviewQueue.RequestAll(_items, skins: false);
}
/// <summary>物品显示名:本地化名(中文名)优先,未就绪时回退内部名。带缓存。</summary>
internal static string ItemName(Item item)
{
if (item == null)
return "?";
// 已解析出本地化名 → 直接命中;缓存值仍是内部名(本地化未就绪时的回退)→ 继续重试
if (_nameCache.TryGetValue(item, out string cached) && cached != item.name)
return cached;
string n = null;
try
{
n = item.GetName();
}
catch
{
// 本地化系统未就绪
}
if (string.IsNullOrWhiteSpace(n))
n = item.name;
_nameCache[item] = n;
return n;
}
/// <summary>
/// 生成物品。killOnSpawn=true 时生物(鱼)生成即死亡(官方 ServerKillOnSpawn,死鱼可拾取售卖)。
/// drip=true 时生物生成即"极品":SetDrip()(游戏官方方法,异色发光)+ 重量拉满 1.75
/// (游戏随机范围 0.25~1.75,CreatureUtils.cs:8)。两者均仅主机可用(SyncVar 服务端写)。
/// </summary>
public static void SpawnItem(Item prefab, bool killOnSpawn = false, bool drip = false)
{
if (!CanUse)
{
ToastModule.Show("物品: 需在游戏内使用", Color.yellow);
return;
}
if (!Compat.GameInfoReady || GameInfo.CurCamera == null)
{
ToastModule.Show("物品: 游戏未初始化", Color.yellow);
return;
}
Vector3 pos = GameInfo.CurCamera.transform.position
+ GameInfo.CurCamera.transform.forward * 2.5f
+ Vector3.up * 1.2f;
try
{
if (Compat.IsHost)
{
Item item = UnityEngine.Object.Instantiate(prefab, pos, Quaternion.identity);
Server.Instance.Spawn(item.gameObject);
if (item is Creature c)
{
if (drip)
{
c.SetDrip(); // 官方方法:spawn 后 IsServerInitialized 才有效
c._syncedRandomWeight.Value = 1.75f; // 游戏随机上限(极品重量)
}
if (killOnSpawn)
c.ServerKillOnSpawn();
}
if (killOnSpawn)
ToastModule.Show($"已获得(死): {ItemName(prefab)}", Color.white);
else if (drip)
ToastModule.Show($"已获得(极品): {ItemName(prefab)}", Color.white);
else
ToastModule.Show($"已获得: {ItemName(prefab)}", Color.white);
}
else
{
Player p = Player.LocalPlayer;
if (p == null || p.Holding == null)
{
ToastModule.Show("物品: 玩家未初始化", Color.yellow);
return;
}
if (prefab.ID < 0 || prefab.ID >= byte.MaxValue)
{
ToastModule.Show("物品: 该物品无法通过购买通道获取", Color.yellow);
return;
}
if (killOnSpawn || drip)
ToastModule.Show("客户端仅支持生成活物(死鱼/极品需主机操作)", Color.yellow);
// 与游戏本体商店购买完全同款:客户端读本地 HeldItem 发 BuyItem,
// isFree=true 免费拿。手上没东西传 null 即可(校验只要求与 HeldItem 相等)。
Server.Instance.BuyItem((byte)prefab.ID, p, p.Holding.HeldItem, pos, Quaternion.identity, true);
ToastModule.Show($"已请求: {ItemName(prefab)}", Color.white);
}
}
catch (System.Exception e)
{
Plugin.Log?.LogWarning($"spawn {prefab.name} failed: {e.Message}");
ToastModule.Show($"物品: 生成失败 ({ItemName(prefab)})", Color.yellow);
}
}
public static void Draw()
{
if (Event.current == null)
return;
EnsureItems();
if (_nameStyle == null)
{
_nameStyle = new GUIStyle(GUI.skin.label)
{
alignment = TextAnchor.MiddleCenter,
wordWrap = true,
richText = true
};
_hintStyle = new GUIStyle(GUI.skin.label)
{
normal = { textColor = new Color(0.75f, 0.75f, 0.75f) }
};
}
_nameStyle.fontSize = UIScale.HintFont;
_hintStyle.fontSize = UIScale.HintFont;
if (_items == null || _items.Count == 0)
{
GUILayout.Label("没有可获得的物品", _hintStyle);
return;
}
GUILayout.Label($"<b>物品图鉴</b> <size={UIScale.HintFont}>点击图标获得 · 共 {_items.Count} 件</size>", _nameStyle);
GUILayout.Label("<color=#777777>左键 = 生成(鱼为活的)· 右键 = 生成死的 · 中键 = 极品(异色+最重,仅主机)</color>", _hintStyle);
// 渲染由 Plugin.Update 驱动(URP 下不能在 OnGUI 阶段渲染),这里只显示进度
if (PreviewQueue.PendingCount > 0)
{
GUILayout.Label($"<color=#88ccff>生成预览中…(剩余 {PreviewQueue.PendingCount})</color>", _hintStyle);
}
if (!CanUse)
GUILayout.Label("<color=#ffcc55>需在游戏内使用</color>", _hintStyle);
// ---- 分页网格(无滚动):格子尺寸按内容区自适应 ----
int cols = UIScale.GalleryCols;
float cellW = (UIScale.W - 30f) / cols;
float cellH = cellW + UIScale.ButtonH * 1.05f; // 图 + 名称行
float availH = UIScale.ContentH - UIScale.ButtonH * 2.1f; // 标题 + 页码栏
int rows = Mathf.Max(1, Mathf.FloorToInt(availH / cellH));
int perPage = rows * cols;
int totalPages = Mathf.Max(1, Mathf.CeilToInt(_items.Count / (float)perPage));
_page = Mathf.Clamp(_page, 0, totalPages - 1);
if (ToolkitMenu.PageNav(ref _page, totalPages, $"共 {_items.Count} 件"))
{
// 翻页
}
int start = _page * perPage;
int count = Mathf.Min(perPage, _items.Count - start);
DrawGrid(cols, cellW, start, count);
}
/// <summary>进入物品页时重置页码到有效范围。</summary>
public static void OnPageEnter()
{
_page = 0;
}
private static void DrawGrid(int cols, float cellW, int start, int count)
{
GUILayout.BeginHorizontal();
for (int i = 0; i < count; i++)
{
if (i % cols != 0)
GUILayout.FlexibleSpace();
DrawCell(_items[start + i], cellW);
if (i % cols == cols - 1 || i == count - 1)
{
int remainder = (i % cols) + 1;
for (int k = remainder; k < cols; k++)
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
if (i != count - 1)
GUILayout.BeginHorizontal();
}
}
}
private static void DrawCell(Item item, float cellW)
{
float imgSize = cellW - 16f;
Texture2D tex = PreviewQueue.Get(item, -1);
GUILayout.BeginVertical(GUILayout.Width(cellW));
// 预览图 = 点击获得(左键活物 / 右键死的 / 中键极品:Drip+最大重量,均限生物,仅主机)
if (GUILayout.Button("", UIScale.ButtonStyle, GUILayout.Width(imgSize), GUILayout.Height(imgSize)))
{
int btn = Event.current != null ? Event.current.button : 0;
bool isCreature = item is Creature;
SpawnItem(item,
killOnSpawn: btn == 1 && isCreature,
drip: btn == 2 && isCreature);
}
Rect btnRect = GUILayoutUtility.GetLastRect();
if (tex != null)
{
GUI.DrawTexture(btnRect, tex);
}
else
{
Color prev = GUI.color;
GUI.color = new Color(0.25f, 0.28f, 0.32f);
GUI.DrawTexture(btnRect, Texture2D.whiteTexture);
GUI.color = prev;
}
// 边框提示可点击
GUI.color = new Color(0.45f, 0.5f, 0.6f);
GUI.DrawTexture(new Rect(btnRect.x, btnRect.y, btnRect.width, 1f), Texture2D.whiteTexture);
GUI.DrawTexture(new Rect(btnRect.x, btnRect.y + btnRect.height - 1f, btnRect.width, 1f), Texture2D.whiteTexture);
GUI.DrawTexture(new Rect(btnRect.x, btnRect.y, 1f, btnRect.height), Texture2D.whiteTexture);
GUI.DrawTexture(new Rect(btnRect.x + btnRect.width - 1f, btnRect.y, 1f, btnRect.height), Texture2D.whiteTexture);
GUI.color = Color.white;
GUILayout.Space(UIScale.ButtonH * 0.12f);
string label = $"{ItemName(item)} <size={Mathf.Max(10, UIScale.HintFont - 1)}>ID{item.ID}</size>";
GUILayout.Label(label, _nameStyle);
GUILayout.EndVertical();
}
}
}