/// <summary> /// 更改枪械的数值 /// </summary> /// <param name="id">枪械id</param> /// <param name="type">装备的类型</param> /// <param name="gain">装备的数值</param> public void ChangeGunInfo(int id, EquipType type, float gain) { GunItem it = m_equipModel.GetGunItem(id); //容量 int capacity = 0; //伤害数值 int hurtNum = 0; //稳定性 float steady = 0f; Debug.Log("gain: " + gain); //改变枪支稳定性 if (type == EquipType.equip_butt || type == EquipType.equip_handle || type == EquipType.equip_muzzle) { steady = gain; it.steadyNum += steady; } //改变枪支容量 else if (type == EquipType.equip_magazine) { capacity = (int)gain; it.capacity += capacity; } //顺序为:id,伤害数值,稳定数值,弹匣容量 LiteEventManager.Instance.TriggerEvent(AvatarValueKey.GunValue, new ValueInfo <int, int, float, int>(id, hurtNum, steady, capacity)); }
//拷贝构造函数 public GunItem(GunItem it) { this.slotTrans = it.slotTrans; this.id = it.id; this.type = it.type; this.hurtNum = it.hurtNum; this.steadyNum = it.steadyNum; this.name = it.name; this.ScenePrefabPath = it.ScenePrefabPath; this.capacity = it.capacity; //深拷贝 equipSlots = new Dictionary <int, EquipSlot>(); List <int> keyList = new List <int>(); keyList.AddRange(it.equipSlots.Keys); for (int i = 0; i < keyList.Count; i++) { equipSlots.Add(keyList[i], it.equipSlots[keyList[i]]); } //两种不同的字典深拷贝方式 equipItems = new Dictionary <int, BagItemInfo>(); var enumerator = it.equipItems.GetEnumerator(); while (enumerator.MoveNext()) { equipItems.Add(enumerator.Current.Key, enumerator.Current.Value); } }
/// <summary> /// 从装备栏挂载到背包栏 /// </summary> /// <param name="gun"></param> /// <param name="itemId">装备槽的id</param> private void EquipItemToBagItem(GunItem gun, int itemId, bool flag = true) { //拷贝上一个槽的item数值 BagItemInfo tempItem = new BagItemInfo(gun.equipItems[itemId]); //挂载item到新的槽(其实是将上一个槽的数值赋给新的槽) AddItem(tempItem, 0); //清空上一个槽的数值 if (flag == true) { m_equipModel.RemoveEquip(gun, itemId); } }
//添加装备,主武器 public bool AddGun(DataBaseManager.GunDBItem it) { //查看主武器槽的挂载情况 int firstEmptySlot = -1; int slotCount = m_equipModel.gunSlots.Count; for (int i = 0; i < slotCount; i++) { if (m_equipModel.gunSlots[i].isEmpty == true) { firstEmptySlot = i; break; } } //有空位 if (firstEmptySlot != -1) { //初始化item并挂载 InitGunItem(it, firstEmptySlot); } //没有空位,此时可以确定每个槽都挂载了item else { //最后一个武器出队 Debug.Log("没有空位了"); GunItem lastItem = m_equipModel.gunItems[slotCount - 1]; //将物体放回场景中 Debug.Log(lastItem.id); Vector3 pos = AvatarInfoManager.Instance.GetAvatarTransform().position; ResManager.Instance.CreateGameObject(lastItem.ScenePrefabPath, new Vector3(pos.x, -0.8f, pos.z), new PickInfo(lastItem.id, null, lastItem.type)); //被丢弃的枪支内的装备放回背包中 var equipList = lastItem.equipItems.GetEnumerator(); while (equipList.MoveNext()) { Debug.Log("equipItems Keys:" + equipList.Current.Key); EquipItemToBagItem(lastItem, equipList.Current.Key, false); } //删除物体 GameObject.Destroy(lastItem.slotTrans.gameObject); //从字典中移除 m_equipModel.gunItems.Remove(slotCount); //从后往前进行数据交换 for (int i = slotCount - 2; i >= 0; i--) { GunItem item = new GunItem(m_equipModel.gunItems[i]); //Debug.Log(i+" "+item.name); m_equipModel.gunItems[i + 1] = item; //更新枪支装备槽的parentId,parentId记录字典key值 var enumerator = item.equipSlots.GetEnumerator(); while (enumerator.MoveNext()) { enumerator.Current.Value.info.parentId = i + 1; } //重新挂载 item.slotTrans.SetParent(m_equipModel.gunSlots[i + 1].transform, false); item.slotTrans.localPosition = Vector3.zero; //更新人物枪支数值 LiteEventManager.Instance.TriggerEvent(EquipType.weapon_gun, new ValueInfo <int, int, float, int>(i + 1, item.hurtNum, item.steadyNum, item.capacity)); } //将新加入的item挂载在第一个槽上 InitGunItem(it, 0); } //测试 //var enumerator = m_equipModel.gunItems.GetEnumerator(); //while (enumerator.MoveNext()) //{ // Debug.Log("第" + enumerator.Current.Key.ToString() + "个item:" + m_equipModel.gunItems[enumerator.Current.Key].name); //} return(true); }
//从背包、另一把枪的装备栏 拖去装备栏 public bool DropToEquipSlot(Transform parentSlot, Transform item, GameObject dropSlot, int dicKey) { string tag = parentSlot.tag; int parentId = parentSlot.GetComponent <BagGrIdIns>().id; int dropSlotId = dropSlot.GetComponent <BagGrIdIns>().id; //得到该装备槽对应的枪 int gunId = dropSlot.GetComponent <BagGrIdIns>().parentId; Debug.Log("parentId: " + parentId); //如果是从背包栏中托过来的 if (tag == "Grid") { //取出上一个槽的item数值引用 BagItemInfo last_It = m_bagModel.BagItemsDic[dicKey]; //拷贝上一个槽的item数值 BagItemInfo temp_It = new BagItemInfo(last_It); //挂载item到新的槽(其实是将上一个槽的数值赋给新的槽) if (m_equipModel.gunItems.TryGetValue(gunId, out GunItem gun)) { //确认拖入的装备类型和槽是否对应 if (gun.equipSlots[dropSlotId].slotType != temp_It.type) { Debug.Log("类型不对应: " + gun.equipSlots[dropSlotId].slotType + temp_It.type); return(false); } //将item加入该枪的装备字典中 gun.equipItems.Add(dropSlotId, temp_It); temp_It.ItemCtl.DicKey = dropSlotId; //改变位置,物理挂载 temp_It.Obj.transform.SetParent(gun.equipSlots[dropSlotId].slotTrans, false); temp_It.Obj.transform.localPosition = Vector3.zero; Debug.Log("开始改变枪支数值"); ChangeItemSize(temp_It, ItemSize.equipSize); //改变枪支数值 ChangeGunInfo(gunId, temp_It.type, temp_It.gain); //清空上一个槽的数值 m_bagModel.BagItemsDic.Remove(dicKey); m_bagView.m_slots[dicKey].isEmpty = true; Debug.Log("完成"); } else { Debug.Log("没找到枪"); } } else if (tag == "EquipSlot") { int slotGunId = parentSlot.GetComponent <BagGrIdIns>().parentId; //获取 if (m_equipModel.gunItems.TryGetValue(slotGunId, out GunItem gun)) { //拷贝上一个槽的item数值 BagItemInfo lastItem = new BagItemInfo(gun.equipItems[dicKey]); //确认拖入的装备类型和槽是否对应 if (gun.equipSlots[dropSlotId].slotType != lastItem.type) { Debug.Log("类型不对应: " + gun.equipSlots[dropSlotId].slotType + lastItem.type); return(false); } //获取dropSlot有关信息 int dropGunId = dropSlot.GetComponent <BagGrIdIns>().parentId; GunItem dropGun = m_equipModel.gunItems[dropGunId]; //挂载 lastItem.Obj.transform.SetParent(dropGun.equipSlots[dropSlotId].slotTrans, false); lastItem.Obj.transform.localPosition = Vector3.zero; dropGun.equipItems.Add(dropSlotId, lastItem); lastItem.ItemCtl.DicKey = dropSlotId; //改变上一把枪支数值 ChangeGunInfo(slotGunId, lastItem.type, -lastItem.gain); //改变下一把枪的数值 ChangeGunInfo(dropGunId, dropGun.type, lastItem.gain); //清空上一个槽的数值 gun.equipItems.Remove(parentId); } } return(true); }
//根据传入it的信息,初始化一个gunItem,slotNum为挂载在第几个槽 private void InitGunItem(DataBaseManager.GunDBItem it, int slotNum) { //实例化gunItem GameObject obj = ResManager.Instance.LoadPrefabFromRes("Prefab/GunItem", true); Text tx = obj.transform.GetChild(0).GetComponent <Text>(); tx.text = it.name; //加载枪械图片 Sprite sprite = ResManager.Instance.LoadSpriteFromRes(it.imagePath); Image img = obj.transform.GetChild(1).GetComponent <Image>(); img.sprite = sprite; Transform partList = obj.transform.GetChild(2); //初始化item对应的装备槽数据 List <EquipType> type = it.equipList; int equipListCount = type.Count; //装备槽位对照表 List <EquipType> allEquipType = new List <EquipType>() { EquipType.equip_butt, EquipType.equip_magazine, EquipType.equip_handle, EquipType.equip_muzzle, EquipType.equip_scope }; Dictionary <int, EquipSlot> equipSlotDic = new Dictionary <int, EquipSlot>(); //不同的枪所能挂载的配件不同,能显示的装备槽数量也不同 for (int i = 0; i < 5; i++) { Transform part = partList.GetChild(i); //给每个槽的parentId赋值 part.GetComponent <BagGrIdIns>().parentId = slotNum; for (int j = 0; j < equipListCount; j++) { if (type[j] == allEquipType[i]) { //将这个槽显示出来 part.gameObject.SetActive(true); //加入装备槽字典 equipSlotDic.Add(i, new EquipSlot(part, i, slotNum, type[j])); } } } //正式挂载 //物理挂载 obj.transform.SetParent(m_equipModel.gunSlots[slotNum].transform, false); obj.transform.localPosition = Vector3.zero; //数据挂载 GunItem item = new GunItem(obj.transform, equipSlotDic, it.name, it.id, it.weaponType, it.hurtNum, it.steadyNum, it.prefabPath, it.bulletCap); Debug.Log("prefabPath:" + item.ScenePrefabPath); if (m_equipModel.gunItems.TryGetValue(slotNum, out GunItem element)) { m_equipModel.gunItems[slotNum] = item; } else { m_equipModel.gunItems.Add(slotNum, item); } //触发更新人物枪支数据 Debug.Log("槽数:" + slotNum); LiteEventManager.Instance.TriggerEvent(EquipType.weapon_gun, new ValueInfo <int, int, float, int>(slotNum, it.hurtNum, it.steadyNum, it.bulletCap)); //挂载完成,将该槽设置为已挂载 m_equipModel.gunSlots[slotNum].isEmpty = false; }
//移除某个武器的某个装备 public void RemoveEquip(GunItem gun, int equipKey) { gun.equipItems.Remove(equipKey); }