public void CmdUseItem(int slotNum, byte[] itemData) { ItemInventorySlot s = info.belt.getSlots()[slotNum]; Item item = Item.ReadItem(itemData); if (s.getItem().IsSameType(item)) { if (s.getItem() != null) { if (s.getItem().use(this)) { s.removeOne(); } if (!isLocalPlayer) { NetworkIdentity iden = GetComponent <NetworkIdentity> (); TargetUseItem(iden.connectionToClient, slotNum); } else { beltUI.updateUI(); } } } else { Debug.LogError("Item sync issue. Client had item " + item.ToString() + " while we had " + s.getItem().ToString()); } }
public int AddItemManyToSlot(Item item, int quantity, int slotNum) { if (slotNum >= slots.Length || slotNum < 0) { return(-1); } ItemInventorySlot s = slots [slotNum]; if (s.isEmpty()) { return(setSlot(item, quantity, slotNum)); } else { if (s.isAddableItem(item)) { return(s.addItemMany(item, quantity)); } else { return(-1); } } }
public Inventory(int numSlots) { slots = new ItemInventorySlot[numSlots]; for (int i = 0; i < numSlots; i++) { slots [i] = new ItemInventorySlot(); } }
public void TargetUseItem(NetworkConnection conn, int slotNum) { ItemInventorySlot s = beltUI.getInventoryBacking().getSlots()[slotNum]; if (s.getItem() != null && s.getItem().use(this)) { s.removeOne(); } beltUI.updateUI(); }
public int removeManyFromSlot(int quantity, int slotNum) { if (slotNum >= slots.Length || slotNum < 0) { return(-1); } ItemInventorySlot s = slots [slotNum]; return(s.removeMany(quantity)); }
// public void swapSlotPosition(int slotA, int slotB) { // if (slotA < 0 || slotB < 0 || slotA >= slots.Length || slotB >= slots.Length) { // return; // } else { // ItemInventorySlot temp = slots [slotA]; // slots [slotB] = slots [slotA]; // slots [slotA] = temp; // } // } public override void Deserialize(NetworkReader reader) { //get number of slots to read int numSlots = (int)reader.ReadPackedUInt32(); slots = new ItemInventorySlot[numSlots]; for (int i = 0; i < numSlots; i++) { slots [i] = new ItemInventorySlot(); slots [i].Deserialize(reader); } }
public void setRightClaw(ClawItem equipment) { ItemInventorySlot slot = new ItemInventorySlot(); if (equipment == null) { slot.setSlot(null, 0); } else { slot.setSlot(equipment, 1); } this.rightClaw.setSlotBackingInfo(slot); }
public void setLowerBody(LowerBodyItem equipment) { ItemInventorySlot slot = new ItemInventorySlot(); if (equipment == null) { slot.setSlot(null, 0); } else { slot.setSlot(equipment, 1); } this.lowerBody.setSlotBackingInfo(slot); }
public void setHelmet(HelmetItem equipment) { ItemInventorySlot slot = new ItemInventorySlot(); if (equipment == null) { slot.setSlot(null, 0); } else { slot.setSlot(equipment, 1); } this.helmet.setSlotBackingInfo(slot); }
public void setBoots(BootsItem equipment) { ItemInventorySlot slot = new ItemInventorySlot(); if (equipment == null) { slot.setSlot(null, 0); } else { slot.setSlot(equipment, 1); } this.boots.setSlotBackingInfo(slot); }
public override void Deserialize(NetworkReader reader) { belt = new BeltInventory(0); belt.Deserialize(reader); backpack = Item.ReadItem(reader) as BackpackItem; helmet = Item.ReadItem(reader) as HelmetItem; upperBody = Item.ReadItem(reader) as UpperBodyItem; lowerBody = Item.ReadItem(reader) as LowerBodyItem; boots = Item.ReadItem(reader) as BootsItem; leftClaw = Item.ReadItem(reader) as ClawItem; rightClaw = Item.ReadItem(reader) as ClawItem; cursorSlot = new ItemInventorySlot(); cursorSlot.Deserialize(reader); }
public int addItemMany(Item item, int quantity) { ItemInventorySlot slot = getFirstSlotForItem(item); if (slot == null) { return(quantity); } int remainder = slot.addItemMany(item, quantity); if (remainder > 0) { return(addItemMany(item, remainder)); } else { return(remainder); } }
public void CmdRequestInventoryActions(byte[] operationInfo) { NetworkReader reader = new NetworkReader(operationInfo); InventoryOperationRequest request = new InventoryOperationRequest(InventoryOperationRequest.Operation.FromCursor, InventoryOperationRequest.InventoryType.Ground, 0); request.Deserialize(reader); Debug.Log("recieved inventory request\nop:" + request.op + "\nquant: " + request.quantity + "\nswapinv: " + request.swapInv + "\nswapslot" + request.swapInvSlot); if (InventoryOperationRequest.validateRequest(request, info)) { ItemInventorySlot dropped = InventoryOperationRequest.performInventoryRequest(request, this); SpawnManager spawnManager = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent <SpawnManager> (); Debug.Log("player dropping " + dropped.getQuantity() + " " + dropped.getItem() + " items"); spawnManager.SpawnPlayerDroppedItem(dropped.getItem(), dropped.getQuantity(), controller); } else { Debug.Log("recieved bad inventory request! client may be out of sync"); } }
private static bool validateToCursorOperation(ItemInventorySlot swapSlot, EquipmentItem swapEquipItem, int quantity, PlayerInfo info) { //cursor is always empty for a valid tocursor operation if (info.cursorSlot == null) { Debug.Log("InventoryOperation validation error: cursor slot is null."); return(false); } if (!info.cursorSlot.isEmpty()) { Debug.Log("InventoryOperation validation error: cursor slot is not empty. Has " + info.cursorSlot.getQuantity() + " " + info.cursorSlot.getItem() + " items"); return(false); } //if not coming from anywhere, invalid operation if (swapSlot == null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: source slot is null"); return(false); } // if quantity exceeds source quantity, invalid. if (swapSlot == null && quantity > swapEquipItem.stackSize) { Debug.Log("InventoryOperation validation error: trying to take more than one equipment item"); return(false); } else if (swapSlot != null && quantity > swapSlot.getQuantity()) { Debug.Log("InventoryOperation validation error: trying to take more items than exists in source slot"); return(false); } return(true); }
public void updateUI() { if (image == null) { image = GetComponentInChildren <Image> (); } image.overrideSprite = getIcon(); string s; if (slot == null) { slot = new ItemInventorySlot(); } if (quantityText == null) { quantityText = GetComponentInChildren <Text> (); } int q = slot.getQuantity(); if (q < 2) { quantityText.text = ""; } else if (slot.getQuantity() < 100) { quantityText.text = q.ToString(); } else { quantityText.text = "99+"; } }
public PlayerInfo(BeltInventory belt) { this.cursorSlot = new ItemInventorySlot(); this.belt = belt; }
//returns items to be dropped/spawned public static ItemInventorySlot performInventoryRequest(InventoryOperationRequest request, Player p) { switch (request.op) { case InventoryOperationRequest.Operation.ToCursor: switch (request.swapInv) { case InventoryOperationRequest.InventoryType.Belt: ItemInventorySlot beltslot = p.info.belt.getSlots() [request.swapInvSlot]; ItemInventorySlot tempSlot = new ItemInventorySlot(); if (request.quantity <= 0) { tempSlot.setSlot(beltslot.getItem(), beltslot.getQuantity()); beltslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity()); p.info.cursorSlot.setSlot(tempSlot.getItem(), tempSlot.getQuantity()); } else { p.info.cursorSlot.addItemMany(beltslot.getItem(), request.quantity); beltslot.removeMany(request.quantity); } break; case InventoryOperationRequest.InventoryType.Backpack: ItemInventorySlot backpackslot = p.info.backpack.inventory.getSlots() [request.swapInvSlot]; ItemInventorySlot tempSlot2 = new ItemInventorySlot(); if (request.quantity <= 0) { tempSlot2.setSlot(backpackslot.getItem(), backpackslot.getQuantity()); backpackslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity()); p.info.cursorSlot.setSlot(tempSlot2.getItem(), tempSlot2.getQuantity()); } else { p.info.cursorSlot.addItemMany(backpackslot.getItem(), request.quantity); backpackslot.removeMany(request.quantity); } break; case InventoryOperationRequest.InventoryType.Equip: switch ((InventoryOperationRequest.EquipSlots)request.swapInvSlot) { case InventoryOperationRequest.EquipSlots.Helmet: p.info.cursorSlot.setSlot(p.info.helmet, 1); p.info.helmet = null; break; case InventoryOperationRequest.EquipSlots.UpperBody: p.info.cursorSlot.setSlot(p.info.upperBody, 1); p.info.upperBody = null; break; case InventoryOperationRequest.EquipSlots.LowerBody: p.info.cursorSlot.setSlot(p.info.lowerBody, 1); p.info.lowerBody = null; break; case InventoryOperationRequest.EquipSlots.Boots: p.info.cursorSlot.setSlot(p.info.boots, 1); p.info.boots = null; break; case InventoryOperationRequest.EquipSlots.RightClaw: p.info.cursorSlot.setSlot(p.info.rightClaw, 1); p.info.rightClaw = null; break; case InventoryOperationRequest.EquipSlots.LeftClaw: p.info.cursorSlot.setSlot(p.info.leftClaw, 1); p.info.leftClaw = null; break; case InventoryOperationRequest.EquipSlots.Backpack: p.info.cursorSlot.setSlot(p.info.backpack, 1); p.info.backpack = null; break; } break; } break; case InventoryOperationRequest.Operation.FromCursor: Item cursorItem = p.info.cursorSlot.getItem(); switch (request.swapInv) { //if moving to an inventory slot, slot is either empty or has the item in it already. case InventoryOperationRequest.InventoryType.Belt: ItemInventorySlot beltslot = p.info.belt.getSlots() [request.swapInvSlot]; if (request.quantity == 0) { if (!p.info.cursorSlot.getItem().IsSameType(beltslot.getItem())) { ItemInventorySlot tempSlot = new ItemInventorySlot(); tempSlot.setSlot(beltslot.getItem(), beltslot.getQuantity()); beltslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity()); p.info.cursorSlot.setSlot(tempSlot.getItem(), tempSlot.getQuantity()); } else { int remainder = beltslot.addItemMany(cursorItem, p.info.cursorSlot.getQuantity()); p.info.cursorSlot.removeMany(p.info.cursorSlot.getQuantity() - remainder); } } else { beltslot.addItemMany(cursorItem, request.quantity); p.info.cursorSlot.removeMany(request.quantity); } break; case InventoryOperationRequest.InventoryType.Backpack: ItemInventorySlot backpackslot = p.info.backpack.inventory.getSlots() [request.swapInvSlot]; if (request.quantity == 0) { if (!p.info.cursorSlot.getItem().IsSameType(backpackslot.getItem())) { ItemInventorySlot tempSlot2 = new ItemInventorySlot(); tempSlot2.setSlot(backpackslot.getItem(), backpackslot.getQuantity()); backpackslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity()); p.info.cursorSlot.setSlot(tempSlot2.getItem(), tempSlot2.getQuantity()); } else { int remainder = backpackslot.addItemMany(cursorItem, p.info.cursorSlot.getQuantity()); p.info.cursorSlot.removeMany(p.info.cursorSlot.getQuantity() - remainder); } } else { backpackslot.addItemMany(cursorItem, request.quantity); p.info.cursorSlot.removeMany(request.quantity); } break; case InventoryOperationRequest.InventoryType.Equip: switch ((InventoryOperationRequest.EquipSlots)request.swapInvSlot) { case InventoryOperationRequest.EquipSlots.Helmet: Item tempHelmet = p.info.helmet; p.info.helmet = p.info.cursorSlot.getItem() as HelmetItem; p.info.cursorSlot.setSlot(tempHelmet, 1); break; case InventoryOperationRequest.EquipSlots.UpperBody: Item tempUpperBody = p.info.upperBody; p.info.upperBody = p.info.cursorSlot.getItem() as UpperBodyItem; p.info.cursorSlot.setSlot(tempUpperBody, 1); break; case InventoryOperationRequest.EquipSlots.LowerBody: Item tempLowerBody = p.info.lowerBody; p.info.lowerBody = p.info.cursorSlot.getItem() as LowerBodyItem; p.info.cursorSlot.setSlot(tempLowerBody, 1); break; case InventoryOperationRequest.EquipSlots.Boots: Item tempBoots = p.info.boots; p.info.boots = p.info.cursorSlot.getItem() as BootsItem; p.info.cursorSlot.setSlot(tempBoots, 1); break; case InventoryOperationRequest.EquipSlots.RightClaw: Item tempRightClaw = p.info.rightClaw; p.info.rightClaw = p.info.cursorSlot.getItem() as ClawItem; p.info.cursorSlot.setSlot(tempRightClaw, 1); break; case InventoryOperationRequest.EquipSlots.LeftClaw: Item tempLeftClaw = p.info.leftClaw; p.info.leftClaw = p.info.cursorSlot.getItem() as ClawItem; p.info.cursorSlot.setSlot(tempLeftClaw, 1); break; case InventoryOperationRequest.EquipSlots.Backpack: Item tempBackpack = p.info.backpack; p.info.backpack = p.info.cursorSlot.getItem() as BackpackItem; p.info.cursorSlot.setSlot(tempBackpack, 1); break; } break; case InventoryOperationRequest.InventoryType.Ground: ItemInventorySlot dropped = new ItemInventorySlot(); if (request.quantity == 0) { dropped.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity()); p.info.cursorSlot.removeMany(p.info.cursorSlot.getQuantity()); } else { dropped.setSlot(p.info.cursorSlot.getItem(), request.quantity); p.info.cursorSlot.removeMany(request.quantity); } return(dropped); } break; } return(new ItemInventorySlot()); }
public static bool validateRequest(InventoryOperationRequest request, PlayerInfo info) { BackpackInventory backpack = null; if (info.backpack != null) { backpack = info.backpack.inventory; } BeltInventory belt = info.belt; ItemInventorySlot cursor = info.cursorSlot; ItemInventorySlot swapSlot = null; EquipmentItem swapEquipItem = null; bool drop = false; switch (request.swapInv) { case InventoryType.Ground: drop = true; break; case InventoryType.Backpack: swapSlot = assertSlotExists(backpack.getSlots(), request.swapInvSlot); if (swapSlot == null) { Debug.Log("InventoryOperation validation error: trying to swap to bad backpack slot"); return(false); } break; case InventoryType.Belt: swapSlot = assertSlotExists(belt.getSlots(), request.swapInvSlot); if (swapSlot == null) { Debug.Log("InventoryOperation validation error: trying to swap to bad belt slot"); return(false); } break; case InventoryType.Equip: swapEquipItem = getEquipItem(info, request.swapInvSlot); break; default: Debug.Log("InventoryOperation validation error: unsupported inventory detected"); return(false); } switch (request.op) { case Operation.ToCursor: return(validateToCursorOperation(swapSlot, swapEquipItem, request.quantity, info)); case Operation.FromCursor: return(validateFromCursorOperation(swapSlot, swapEquipItem, request.swapInvSlot, drop, request.quantity, info)); default: Debug.Log("InventoryOperation validation error: unsupported operation detected"); return(false); } }
private static bool validateFromCursorOperation(ItemInventorySlot swapSlot, EquipmentItem swapEquipItem, int slotNum, bool drop, int quantity, PlayerInfo info) { //cursor must have something in it for a valid transfer if (info.cursorSlot == null || info.cursorSlot.isEmpty()) { Debug.Log("InventoryOperation validation error: cursor is null or has no contents to transfer"); return(false); } Item transferItem = info.cursorSlot.getItem(); int cursorQuantity = info.cursorSlot.getQuantity(); //quantity tranferred cannot be greater than that in cursor if (quantity > cursorQuantity) { Debug.Log("InventoryOperation validation error: cannot transfer more items than cursor has"); return(false); } bool handleEquipment = true; //if there is an equipment slot or if dropping, not performing an equipment change if (swapSlot != null || drop == true) { handleEquipment = false; } if (handleEquipment) { //equippables dont stack if (cursorQuantity > 1) { Debug.Log("InventoryOperation validation error: cursor cannot carry more than one equipment item"); return(false); } EquipSlots eSlot = (EquipSlots)slotNum; switch (eSlot) { case EquipSlots.Helmet: //check for inconsistency. if (info.helmet != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to helmet slot or slot is occupied"); return(false); } return(transferItem is HelmetItem); case EquipSlots.UpperBody: if (info.upperBody != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to upper body slot or slot is occupied"); return(false); } return(transferItem is UpperBodyItem); case EquipSlots.LowerBody: if (info.lowerBody != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to lower body slot or slot is occupied"); return(false); } return(transferItem is LowerBodyItem); case EquipSlots.Boots: if (info.boots != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to boots slot or slot is occupied"); return(false); } return(transferItem is BootsItem); case EquipSlots.LeftClaw: if (info.leftClaw != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to left claw slot or slot is occupied"); return(false); } return(transferItem is ClawItem); case EquipSlots.RightClaw: if (info.rightClaw != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to right claw slot or slot is occupied"); return(false); } return(transferItem is ClawItem); case EquipSlots.Backpack: if (info.backpack != null && swapEquipItem == null) { Debug.Log("InventoryOperation validation error: trying to equip incorrect equipment to backpack slot or slot is occupied"); return(false); } return(transferItem is BackpackItem); default: Debug.Log("InventoryOperation validation error: unrecognized equipment type"); return(false); } } else { if (swapSlot != null) { //if enough space and item is same as destination, success. if (swapSlot.isAddableItems(transferItem, quantity)) { return(true); } else { Debug.Log("InventoryOperation validation error: trying to unequip non-existing helmet"); return(false); } } else { //if not a swap slot, must be drop. Return drop success. return(drop); } } }
void handleSlotSelect(ItemInventorySlotUI slotUI, string menuIdentifier, string slotIdentifier, int quantity) { InventoryOperationRequest.InventoryType menuType = InventoryOperationRequest.InventoryType.Ground; InventoryOperationRequest req = null; if (menuIdentifier.Equals("BeltSlot")) { menuType = InventoryOperationRequest.InventoryType.Belt; } else if (menuIdentifier.Equals("BackpackSlot")) { menuType = InventoryOperationRequest.InventoryType.Backpack; } else if (menuIdentifier.Equals("EquipSlot")) { menuType = InventoryOperationRequest.InventoryType.Equip; } if (slotUI == null) //THREW IT ON THE GROUND { req = new InventoryOperationRequest(InventoryOperationRequest.Operation.FromCursor, InventoryOperationRequest.InventoryType.Ground, System.Int32.Parse(slotIdentifier), quantity); } else if (cursorSlot.getSlotBackingInfo().isEmpty()) { req = new InventoryOperationRequest(InventoryOperationRequest.Operation.ToCursor, menuType, System.Int32.Parse(slotIdentifier), quantity); } else { req = new InventoryOperationRequest(InventoryOperationRequest.Operation.FromCursor, menuType, System.Int32.Parse(slotIdentifier), quantity); } //perform local changes before sending info to server. Annoying to wait on server for ui change. If sync error, server should let us know eventually and fix if (InventoryOperationRequest.validateRequest(req, localPlayer.info)) //perform check server will do to verify before performing locally { ItemInventorySlot dropped = InventoryOperationRequest.performInventoryRequest(req, localPlayer); if (!localPlayer.isServer) { sendInventoryUpdateToServer(req); } else { //host drops items if (!dropped.isEmpty()) { SpawnManager spawnManager = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent <SpawnManager>(); spawnManager.SpawnPlayerDroppedItem(dropped.getItem(), dropped.getQuantity(), localPlayer.controller); } } cursorSlot.updateUI(); switch (menuType) { case InventoryOperationRequest.InventoryType.Belt: beltUI.updateUI(); break; case InventoryOperationRequest.InventoryType.Backpack: backpackUI.updateUI(); break; case InventoryOperationRequest.InventoryType.Equip: if (slotIdentifier.Equals("6")) { if (localPlayer.info.backpack == null) { backpackUI.loadInventory(null); } else { backpackUI.loadInventory(localPlayer.info.backpack.inventory); } } equipUI.updateUI(localPlayer.info); break; } } }
public void setSlotBackingInfo(ItemInventorySlot slot) { this.slot = slot; updateUI(); }