//拾起道具 public void PickupItemFromGrid(GridUnit fromGrid) { if (fromGrid == null || fromGrid.gridItem == null || package == null) { return; } int itemID = fromGrid.gridItem.item.itemID; int itemCount = fromGrid.gridItem.count; int finalCount = 0; bool result = package.TryAddItem(itemID, itemCount, ref finalCount); //成功捡起道具 if (result) { //先生成道具格子的事件 if (fromGrid != null) { fromGrid.OnItemPicked(); } BattleUnitPickupItemAction action = BattleUnitActionEvent.CreateEvent <BattleUnitPickupItemAction>(BattleUnitActionType.PickupItem, this); action.itemID = itemID; action.addCount = itemCount; action.finalCount = finalCount; battleField.AppendBattleAction(action); } }
//拾取道具 private IEnumerator PlayPickupItemAction(BattleUnitPickupItemAction action) { NormalMessage normalMessage = new NormalMessage(EGameConstL.EVENT_BATTLE_UNIT_PACKAGE_CHANGED); normalMessage.Body = battleUnit.package; EventManager.Instance.Run(EGameConstL.EVENT_BATTLE_UNIT_PACKAGE_CHANGED, normalMessage); yield break; }
/// <summary> /// 尝试添加道具 /// </summary> /// <param name="itemID"></param> /// <param name="addition"></param> /// <returns>是否成功</returns> public bool TryAddItem(int itemID, int addition, ref int finalCount) { if (items == null) { items = new List <PackageItem>(); } BattleUnitPickupItemAction action = null; PackageItem emptyItem = null; for (int i = 0; i < items.Count; i++) { if (items[i].item != null && items[i].item.itemID == itemID) { items[i].count += addition; finalCount = items[i].count; return(true); } else if (items[i].item == null) { emptyItem = items[i]; } } //找到了一个空位 if (emptyItem != null) { emptyItem.item = PackageItemManager.Instance.GetItem(itemID); if (emptyItem.item != null) { emptyItem.count += addition; finalCount = emptyItem.count; return(true); } } //背包已满 if (items.Count >= capacity) { UtilityHelper.LogWarning(string.Format("Add item failed. Package if full : {0} -> {1}/{2}", itemID, items.Count, capacity)); return(false); } //添加一个道具 emptyItem = PackageItem.CreateInstance(); emptyItem.item = PackageItemManager.Instance.GetItem(itemID); if (emptyItem.item != null) { items.Add(emptyItem); } emptyItem.count += addition; finalCount = emptyItem.count; return(true); }