示例#1
0
    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");
        }
    }
示例#2
0
    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+";
        }
    }
    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);
    }
示例#4
0
    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;
            }
        }
    }
    //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());
    }