コード例 #1
0
 private void EquipConsommableRPC(int consommableIndex)
 {
     consommableHolder.EquipConsommable(consommableIndex);
 }
コード例 #2
0
        /// <summary>
        /// Collect an item. Shoot a raycast to see if the player is targeting an item, then check distance.
        /// If the player can collect the item, try to find a slot on the inventory and add it if the inventory is not full
        /// </summary>
        public void Collect()
        {
            RaycastHit hit;

            Transform objectHit = null;

            if (Physics.Raycast(camera.position, camera.forward, out hit, 5f))
            {
                objectHit = hit.transform;
            }

            if (!objectHit)
            {
                return;
            }

            var playerGameObject = objectHit.gameObject;

            // - hit object is not from loot layer, exit
            if (playerGameObject.layer != 11)
            {
                return;
            }

            // - check distance
            if (hit.distance > 5f)
            {
                return;
            }


            var playerObject = playerGameObject.GetComponent <PlayerObjectScript>();

            var slotIndex = FindSlot(playerObject.GetId());

            // - inventory is not full
            if (slotIndex != -1 && playerObject.IsAvailable() && !playerObject.IsLooting())
            {
                // - the object is is looting state until the TakeObject RPC disable it
                playerObject.SetLooting(true);
                playerView.RPC("TakeObject", PhotonTargets.AllViaServer, playerObject.GetLootTrackerIndex(), slotIndex, playerView.ownerId);
                AddObject(playerObject, slotIndex);
                playerUI.SetItemUISlot(playerObject, slotIndex);

                // equip weapon if the index is already selected
                if (slotIndex == currentSlotIndex)
                {
                    if (playerObject.IsWeapon())
                    {
                        var weapon = playerObject.GetWeapon();
                        weaponHolder.SetWeapon(weapon, weapon.currentAmmo);
                        playerUI.SetAmmoCounter(weapon.currentAmmo);
                    }

                    else if (playerObject.isConsommable())
                    {
                        var consommableId = playerObject.GetConsommable().GetId();
                        weaponHolder.SetWeapon(null, 0f);
                        consommableHolder.EquipConsommable(consommableId);
                        playerUI.SetAmmoCounter(inventory[slotIndex].GetStackSize());
                    }
                }
            }
        }