-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemSystem.cs
More file actions
468 lines (402 loc) · 13.5 KB
/
ItemSystem.cs
File metadata and controls
468 lines (402 loc) · 13.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using UnityEngine;
using Mirror;
public class ItemSystem : NetworkBehaviour
{
public Transform ItemPos; // Assign in Inspector (Empty GameObject for holding items)
// Track the held items (up to 3)
private NetworkItem[] heldItems = new NetworkItem[3];
[SyncVar(hook = nameof(OnHeldItem1IdChanged))]
private uint heldItem1NetworkId = 0;
[SyncVar(hook = nameof(OnHeldItem2IdChanged))]
private uint heldItem2NetworkId = 0;
[SyncVar(hook = nameof(OnHeldItem3IdChanged))]
private uint heldItem3NetworkId = 0;
[SyncVar(hook = nameof(OnActiveItemChanged))]
private int activeItemIndex = -1; // -1 means no item is active
// Debug visualization
public bool debugMode = true;
[Header("UI Integration")]
public InventoryUI inventoryUI; // Assign in inspector if you want direct integration
private void OnHeldItem1IdChanged(uint oldValue, uint newValue)
{
UpdateHeldItemReference(0, newValue);
}
private void OnHeldItem2IdChanged(uint oldValue, uint newValue)
{
UpdateHeldItemReference(1, newValue);
}
private void OnHeldItem3IdChanged(uint oldValue, uint newValue)
{
UpdateHeldItemReference(2, newValue);
}
private void UpdateHeldItemReference(int slot, uint newValue)
{
if (debugMode)
{
Debug.Log($"[Client] Player {netId} held item slot {slot} ID changed to: {newValue}");
}
// Update heldItem reference based on the new network ID
if (newValue != 0 && NetworkClient.spawned.ContainsKey(newValue))
{
NetworkIdentity itemIdentity = NetworkClient.spawned[newValue];
if (itemIdentity != null)
{
heldItems[slot] = itemIdentity.GetComponent<NetworkItem>();
}
}
else
{
heldItems[slot] = null;
}
// Update visibility based on active item
UpdateItemVisibility();
// Notify UI of change
NotifyInventoryChanged();
}
private void OnActiveItemChanged(int oldValue, int newValue)
{
if (debugMode)
{
Debug.Log($"[Client] Player {netId} active item changed: {oldValue} -> {newValue}");
}
UpdateItemVisibility();
// Notify UI of change
NotifyInventoryChanged();
}
private void UpdateItemVisibility()
{
for (int i = 0; i < heldItems.Length; i++)
{
if (heldItems[i] != null)
{
bool shouldBeVisible = (i == activeItemIndex);
heldItems[i].SetVisibility(shouldBeVisible);
}
}
}
private void Update()
{
if (!isOwned) return;
// Handle item pickup/drop
if (Input.GetKeyDown(KeyCode.E))
{
if (debugMode)
{
Debug.Log($"[Client] Player {netId} E pressed - attempting pickup");
}
TryPickupItem();
}
else if (Input.GetKeyDown(KeyCode.G))
{
if (debugMode)
{
Debug.Log($"[Client] Player {netId} G pressed - attempting drop");
}
if (HasActiveItem()) // Only drop if there's actually an item in the active slot
{
TryDropItem();
}
else if (debugMode)
{
Debug.Log($"[Client] Player {netId} cannot drop - no item in active slot {activeItemIndex}");
}
}
// Handle item switching
if (Input.GetKeyDown(KeyCode.Alpha1))
{
SwitchToItem(0);
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
SwitchToItem(1);
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
SwitchToItem(2);
}
}
private void SwitchToItem(int itemIndex)
{
if (activeItemIndex == itemIndex)
{
// If pressing the same number as active item, deactivate it (empty hand)
if (debugMode)
{
Debug.Log($"[Client] Player {netId} deactivating item slot {itemIndex} (empty hand)");
}
CmdSetActiveItem(-1);
}
else
{
// Switch to this slot regardless of whether it has an item or not
if (debugMode)
{
Debug.Log($"[Client] Player {netId} switching to slot {itemIndex} (item: {(heldItems[itemIndex] != null ? "yes" : "no")})");
}
CmdSetActiveItem(itemIndex);
}
}
[Command]
private void CmdSetActiveItem(int itemIndex)
{
if (itemIndex >= -1 && itemIndex < 3)
{
// Allow switching to any slot (even empty ones) or deactivating
activeItemIndex = itemIndex;
if (debugMode)
{
string itemStatus = itemIndex == -1 ? "none (empty hand)" :
(GetHeldItemNetworkId(itemIndex) != 0 ? "with item" : "empty slot");
Debug.Log($"[Server] Player {netId} active item set to slot {itemIndex} ({itemStatus})");
}
// Update visibility for all held items
UpdateAllItemsVisibility();
}
}
private void UpdateAllItemsVisibility()
{
if (!isServer) return;
for (int i = 0; i < 3; i++)
{
uint itemNetId = GetHeldItemNetworkId(i);
if (itemNetId != 0 && NetworkClient.spawned.ContainsKey(itemNetId))
{
NetworkItem item = NetworkClient.spawned[itemNetId].GetComponent<NetworkItem>();
if (item != null)
{
bool shouldBeVisible = (i == activeItemIndex);
item.SetServerVisibility(shouldBeVisible);
}
}
}
}
private void TryPickupItem()
{
// Find first empty slot
int emptySlot = -1;
for (int i = 0; i < heldItems.Length; i++)
{
if (heldItems[i] == null)
{
emptySlot = i;
break;
}
}
if (emptySlot == -1)
{
if (debugMode)
{
Debug.Log($"[Client] Player {netId} inventory is full");
}
return; // No empty slots
}
Collider[] colliders = Physics.OverlapSphere(transform.position, 2f);
foreach (var col in colliders)
{
NetworkItem item = col.GetComponent<NetworkItem>();
if (item != null && item.IsAvailable())
{
if (debugMode)
{
Debug.Log($"[Client] Player {netId} requesting pickup of item {item.netId} to slot {emptySlot}");
}
// Request pickup from the server
heldItems[emptySlot] = item;
item.CmdRequestPickup(netIdentity, ItemPos.position, ItemPos.rotation, emptySlot);
break;
}
}
}
private void TryDropItem()
{
if (HasActiveItem())
{
NetworkItem itemToDrop = heldItems[activeItemIndex];
if (debugMode)
{
Debug.Log($"[Client] Player {netId} trying to drop item {itemToDrop.netId} from slot {activeItemIndex}");
}
// Always use the Command approach for clients to ensure the server gets the message
CmdRequestDropItem(itemToDrop.netId, activeItemIndex);
// Clear reference locally immediately for responsive UI
heldItems[activeItemIndex] = null;
}
}
[Command]
private void CmdRequestDropItem(uint itemNetworkId, int slot)
{
// Find the actual NetworkIdentity by ID
if (NetworkClient.spawned.ContainsKey(itemNetworkId))
{
NetworkIdentity networkIdentity = NetworkClient.spawned[itemNetworkId];
NetworkItem item = networkIdentity.GetComponent<NetworkItem>();
if (item != null)
{
if (debugMode)
{
Debug.Log($"[Server] Player {netId} requesting drop of item {itemNetworkId} from slot {slot}");
}
// The server handles the actual drop
item.ServerDropItem(netIdentity);
// Clear holding state for this slot
SetHeldItemNetworkId(slot, 0);
// Keep the same active slot (now empty) instead of deactivating
// This allows the player to stay on the same slot but with empty hands
if (debugMode)
{
Debug.Log($"[Server] Slot {slot} is now empty but remains active");
}
}
}
}
public void AssignHeldItem(NetworkItem item, int slot)
{
if (slot < 0 || slot >= heldItems.Length) return;
heldItems[slot] = item;
if (isServer)
{
SetHeldItemNetworkId(slot, item != null ? item.netId : 0);
// If no item is currently active, make this the active item
if (activeItemIndex == -1 && item != null)
{
activeItemIndex = slot;
}
// Update visibility immediately
UpdateAllItemsVisibility();
}
else
{
CmdUpdateHoldingState(slot, item != null ? item.netId : 0);
}
if (debugMode)
{
Debug.Log($"[{(isServer ? "Server" : "Client")}] Player {netId} assigned held item {(item != null ? item.netId.ToString() : "null")} to slot {slot}");
}
}
[Command]
private void CmdUpdateHoldingState(int slot, uint itemNetworkId)
{
SetHeldItemNetworkId(slot, itemNetworkId);
// If no item is currently active and we just added an item, make it active
if (activeItemIndex == -1 && itemNetworkId != 0)
{
activeItemIndex = slot;
}
// Update visibility for all items
UpdateAllItemsVisibility();
}
private void SetHeldItemNetworkId(int slot, uint networkId)
{
switch (slot)
{
case 0:
heldItem1NetworkId = networkId;
break;
case 1:
heldItem2NetworkId = networkId;
break;
case 2:
heldItem3NetworkId = networkId;
break;
}
}
private uint GetHeldItemNetworkId(int slot)
{
switch (slot)
{
case 0:
return heldItem1NetworkId;
case 1:
return heldItem2NetworkId;
case 2:
return heldItem3NetworkId;
default:
return 0;
}
}
// Helper method to clear held item (can be called by NetworkItem when dropping)
public void ClearHeldItem(NetworkItem item)
{
for (int i = 0; i < heldItems.Length; i++)
{
if (heldItems[i] == item)
{
heldItems[i] = null;
if (isServer)
{
SetHeldItemNetworkId(i, 0);
// Keep the slot active if it was the active slot, just make it empty
// Don't change activeItemIndex - let the player keep their selected slot
}
else if (isOwned)
{
CmdUpdateHoldingState(i, 0);
}
break;
}
}
}
// Public getter to check if player is holding any items
public bool IsHoldingAnyItem()
{
for (int i = 0; i < heldItems.Length; i++)
{
if (heldItems[i] != null) return true;
}
return false;
}
// Public getter to check if a specific slot has an item
public bool IsHoldingItemInSlot(int slot)
{
if (slot < 0 || slot >= heldItems.Length) return false;
return heldItems[slot] != null;
}
// Public getter for the active held item
public NetworkItem GetActiveHeldItem()
{
if (activeItemIndex >= 0 && activeItemIndex < heldItems.Length)
{
return heldItems[activeItemIndex];
}
return null; // Returns null if no slot is active or if active slot is empty
}
// Check if player has an active slot selected (even if empty)
public bool HasActiveSlot()
{
return activeItemIndex >= 0 && activeItemIndex < heldItems.Length;
}
// Check if the active slot actually contains an item
public bool HasActiveItem()
{
return GetActiveHeldItem() != null;
}
// Public getter for a specific slot's item
public NetworkItem GetHeldItem(int slot)
{
if (slot >= 0 && slot < heldItems.Length)
{
return heldItems[slot];
}
return null;
}
// Get the active item index
public int GetActiveItemIndex()
{
return activeItemIndex;
}
private void NotifyInventoryChanged()
{
if (inventoryUI != null && isOwned)
{
inventoryUI.RefreshDisplay();
}
}
public string GetItemName(int slot)
{
if (slot >= 0 && slot < heldItems.Length && heldItems[slot] != null)
{
return heldItems[slot].name;
}
return "";
}
}