-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerEquipment.cs
266 lines (261 loc) · 9.66 KB
/
PlayerEquipment.cs
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
using UnityEngine;
using System;
using System.Collections.Generic;
using UnityEngine.Serialization;
using Mirror;
[Serializable]
public struct EquipmentInfo
{
public string requiredCategory;
public Transform location;
public ScriptableItemAndAmount defaultItem;
}
[RequireComponent(typeof(Animator))]
public class PlayerEquipment : Equipment
{
public Animator animator;
public EquipmentInfo[] slotInfo =
{
new EquipmentInfo{requiredCategory="Head", location=null, defaultItem=new ScriptableItemAndAmount()},
new EquipmentInfo{requiredCategory="Chest", location=null, defaultItem=new ScriptableItemAndAmount()},
new EquipmentInfo{requiredCategory="Legs", location=null, defaultItem=new ScriptableItemAndAmount()},
new EquipmentInfo{requiredCategory="Feet", location=null, defaultItem=new ScriptableItemAndAmount()}
};
[FormerlySerializedAs("rightHandLocation")]
public Transform weaponMount;
Dictionary<string, Transform> skinBones = new Dictionary<string, Transform>();
void Awake()
{
foreach (SkinnedMeshRenderer skin in GetComponentsInChildren<SkinnedMeshRenderer>())
foreach (Transform bone in skin.bones)
skinBones[bone.name] = bone;
if (weaponMount != null && weaponMount.childCount > 0)
Debug.LogWarning(name + " PlayerEquipment.weaponMount should have no children, otherwise they will be destroyed.");
}
public override void OnStartClient()
{
base.OnStartClient();
slots.Callback += OnEquipmentChanged;
for (int i = 0; i < slots.Count; ++i)
RefreshLocation(i);
}
bool CanReplaceAllBones(SkinnedMeshRenderer equipmentSkin)
{
foreach (Transform bone in equipmentSkin.bones)
if (!skinBones.ContainsKey(bone.name))
return false;
return true;
}
void ReplaceAllBones(SkinnedMeshRenderer equipmentSkin)
{
Transform[] bones = equipmentSkin.bones;
for (int i = 0; i < bones.Length; ++i)
{
string boneName = bones[i].name;
if (!skinBones.TryGetValue(boneName, out bones[i]))
Debug.LogWarning(equipmentSkin.name + " bone " + boneName + " not found in original player bones. Make sure to check CanReplaceAllBones before.");
}
equipmentSkin.bones = bones;
}
void RebindAnimators()
{
foreach (var anim in GetComponentsInChildren<Animator>())
anim.Rebind();
}
public void RefreshLocation(Transform location, ItemSlot slot)
{
if (slot.amount > 0)
{
ScriptableItem itemData = slot.item.data;
if (location.childCount == 0 || itemData.modelPrefab == null ||
location.GetChild(0).name != itemData.modelPrefab.name)
{
if (location.childCount > 0)
Destroy(location.GetChild(0).gameObject);
if (itemData.modelPrefab != null)
{
GameObject go = Instantiate(itemData.modelPrefab, location, false);
go.name = itemData.modelPrefab.name;
SkinnedMeshRenderer equipmentSkin = go.GetComponentInChildren<SkinnedMeshRenderer>();
if (equipmentSkin != null && CanReplaceAllBones(equipmentSkin))
ReplaceAllBones(equipmentSkin);
Animator anim = go.GetComponent<Animator>();
if (anim != null)
{
anim.runtimeAnimatorController = animator.runtimeAnimatorController;
RebindAnimators();
}
}
}
}
else
{
if (location.childCount > 0)
Destroy(location.GetChild(0).gameObject);
}
}
void RefreshLocation(int index)
{
ItemSlot slot = slots[index];
EquipmentInfo info = slotInfo[index];
if (info.requiredCategory != "" && info.location != null)
RefreshLocation(info.location, slot);
}
void OnEquipmentChanged(SyncListItemSlot.Operation op, int index, ItemSlot oldSlot, ItemSlot newSlot)
{
ScriptableItem oldItem = oldSlot.amount > 0 ? oldSlot.item.data : null;
ScriptableItem newItem = newSlot.amount > 0 ? newSlot.item.data : null;
if (oldItem != newItem)
{
RefreshLocation(index);
}
}
[Server]
public void SwapInventoryEquip(int inventoryIndex, int equipmentIndex)
{
if (health.current > 0 &&
0 <= inventoryIndex && inventoryIndex < inventory.slots.Count &&
0 <= equipmentIndex && equipmentIndex < slots.Count)
{
ItemSlot slot = inventory.slots[inventoryIndex];
if (slot.amount == 0 ||
slot.item.data is EquipmentItem &&
((EquipmentItem)slot.item.data).CanEquip(this, inventoryIndex, equipmentIndex))
{
ItemSlot temp = slots[equipmentIndex];
slots[equipmentIndex] = slot;
inventory.slots[inventoryIndex] = temp;
}
}
}
[Command]
public void CmdSwapInventoryEquip(int inventoryIndex, int equipmentIndex)
{
SwapInventoryEquip(inventoryIndex, equipmentIndex);
}
[Command]
public void CmdMergeInventoryEquip(int inventoryIndex, int equipmentIndex)
{
if (health.current > 0 &&
0 <= inventoryIndex && inventoryIndex < inventory.slots.Count &&
0 <= equipmentIndex && equipmentIndex < slots.Count)
{
ItemSlot slotFrom = inventory.slots[inventoryIndex];
ItemSlot slotTo = slots[equipmentIndex];
if (slotFrom.amount > 0 && slotTo.amount > 0)
{
if (slotFrom.item.Equals(slotTo.item))
{
int put = slotTo.IncreaseAmount(slotFrom.amount);
slotFrom.DecreaseAmount(put);
inventory.slots[inventoryIndex] = slotFrom;
slots[equipmentIndex] = slotTo;
}
}
}
}
[Command]
public void CmdMergeEquipInventory(int equipmentIndex, int inventoryIndex)
{
if (health.current > 0 &&
0 <= inventoryIndex && inventoryIndex < inventory.slots.Count &&
0 <= equipmentIndex && equipmentIndex < slots.Count)
{
ItemSlot slotFrom = slots[equipmentIndex];
ItemSlot slotTo = inventory.slots[inventoryIndex];
if (slotFrom.amount > 0 && slotTo.amount > 0)
{
if (slotFrom.item.Equals(slotTo.item))
{
int put = slotTo.IncreaseAmount(slotFrom.amount);
slotFrom.DecreaseAmount(put);
slots[equipmentIndex] = slotFrom;
inventory.slots[inventoryIndex] = slotTo;
}
}
}
}
[Command]
public void CmdDropItem(int index)
{
if (health.current > 0 &&
0 <= index && index < slots.Count && slots[index].amount > 0)
{
DropItemAndClearSlot(index);
}
}
public int GetEquipmentTypeIndex(string category)
{
for (int i = 0; i < slotInfo.Length; ++i)
if (slotInfo[i].requiredCategory == category)
return i;
return -1;
}
[Server]
public void DropItemAndClearSlot(int index)
{
ItemSlot slot = slots[index];
((PlayerInventory)inventory).DropItem(slot.item, slot.amount);
slot.amount = 0;
slots[index] = slot;
}
[Server]
public void OnDeath()
{
for (int i = 0; i < slots.Count; ++i)
if (slots[i].amount > 0)
DropItemAndClearSlot(i);
}
[Server]
public void OnRespawn()
{
for (int i = 0; i < slotInfo.Length; ++i)
slots[i] = slotInfo[i].defaultItem.item != null ? new ItemSlot(new Item(slotInfo[i].defaultItem.item), slotInfo[i].defaultItem.amount) : new ItemSlot();
}
public void OnReceivedDamage(Entity attacker, int damage)
{
for (int i = 0; i < slots.Count; ++i)
{
if (slots[i].amount > 0)
{
ItemSlot slot = slots[i];
slot.item.durability = Mathf.Clamp(slot.item.durability - damage, 0, slot.item.maxDurability);
slots[i] = slot;
}
}
}
void OnDragAndDrop_InventorySlot_EquipmentSlot(int[] slotIndices)
{
if (inventory.slots[slotIndices[0]].amount > 0 && slots[slotIndices[1]].amount > 0 &&
inventory.slots[slotIndices[0]].item.Equals(slots[slotIndices[1]].item))
{
CmdMergeInventoryEquip(slotIndices[0], slotIndices[1]);
}
else
{
CmdSwapInventoryEquip(slotIndices[0], slotIndices[1]);
}
}
void OnDragAndDrop_EquipmentSlot_InventorySlot(int[] slotIndices)
{
if (slots[slotIndices[0]].amount > 0 && inventory.slots[slotIndices[1]].amount > 0 &&
slots[slotIndices[0]].item.Equals(inventory.slots[slotIndices[1]].item))
{
CmdMergeEquipInventory(slotIndices[0], slotIndices[1]);
}
else
{
CmdSwapInventoryEquip(slotIndices[1], slotIndices[0]);
}
}
void OnDragAndClear_EquipmentSlot(int slotIndex)
{
CmdDropItem(slotIndex);
}
void OnValidate()
{
for (int i = 0; i < slotInfo.Length; ++i)
if (slotInfo[i].defaultItem.item != null && slotInfo[i].defaultItem.amount == 0)
slotInfo[i].defaultItem.amount = 1;
}
}