예제 #1
0
        /// <summary>
        /// Add new weapon in available slot in inventory.
        /// </summary>
        public virtual bool Add(WeaponID weapon)
        {
            if (weapon == null || !cacheGroupToSlots.ContainsKey(weapon.GetGroup()) || (allowIdenticalWeapons && cacheKeyToID.ContainsValue(weapon)))
            {
                return(false);
            }

            List <InventorySlot> slots = cacheGroupToSlots[weapon.GetGroup()];

            if (slots == null || slots.Count == 0)
            {
                return(false);
            }

            for (int i = 0, length = slots.Count; i < length; i++)
            {
                InventorySlot slot = slots[i];
                if (slot.GetWeapon() == null || (i == length - 1 && slot.GetWeapon() != null))
                {
                    cacheKeyToID[slot.GetKey()] = weapon;
                    slot.SetWeapon(weapon);
                    cacheGroupToSlots[weapon.GetGroup()][i] = slot;
                    return(true);
                }
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Remove weapon from inventory.
        /// </summary>
        public virtual bool Remove(WeaponID weapon)
        {
            if (weapon == null || !cacheGroupToSlots.ContainsKey(weapon.GetGroup()) || !cacheKeyToID.ContainsValue(weapon))
            {
                return(false);
            }

            List <InventorySlot> slots = cacheGroupToSlots[weapon.GetGroup()];

            if (slots == null || slots.Count == 0)
            {
                return(false);
            }

            for (int i = 0, length = slots.Count; i < length; i++)
            {
                InventorySlot slot = slots[i];
                if (slot.GetWeapon() == weapon)
                {
                    cacheKeyToID[slot.GetKey()] = null;
                    slot.SetWeapon(null);
                    cacheGroupToSlots[weapon.GetGroup()][i] = slot;
                    return(true);
                }
            }
            return(false);
        }
예제 #3
0
        /// <summary>
        /// Replace inventory weapon on some new weapon.
        /// </summary>
        public bool Replace(WeaponID inventoryWeapon, WeaponID someWeapon)
        {
            if (inventoryWeapon == null || someWeapon != null ||
                currentWeaponKey == KeyCode.None ||
                !cacheGroupToSlots.ContainsKey(inventoryWeapon.GetGroup()) || !cacheKeyToID.ContainsValue(inventoryWeapon) ||
                !cacheGroupToSlots.ContainsKey(someWeapon.GetGroup()) || !cacheKeyToID.ContainsValue(someWeapon))
            {
                return(false);
            }

            if (inventoryWeapon.GetGroup() != someWeapon.GetGroup())
            {
                return(false);
            }

            WeaponID currentWeapon = GetActiveWeaponID();

            if (currentWeapon == inventoryWeapon)
            {
                replaceWeaponAnimation = ReplaceWeaponAnimation(someWeapon);
                StartCoroutine(replaceWeaponAnimation);
            }

            List <InventorySlot> slots = cacheGroupToSlots[inventoryWeapon.GetGroup()];

            if (slots == null || slots.Count == 0)
            {
                return(false);
            }

            for (int i = 0, length = slots.Count; i < length; i++)
            {
                InventorySlot slot = slots[i];
                if (slot.GetWeapon() == inventoryWeapon)
                {
                    cacheKeyToID[slot.GetKey()] = someWeapon;
                    slot.SetWeapon(someWeapon);
                    cacheGroupToSlots[someWeapon.GetGroup()][i] = slot;
                    switchWeaponAnimation = SwitchWeaponAnimation(someWeapon);
                    StartCoroutine(switchWeaponAnimation);
                    return(true);
                }
            }

            return(false);
        }
예제 #4
0
        /// <summary>
        /// Replace current weapon on new.
        /// </summary>
        public bool Replace(WeaponID weapon)
        {
            if (weapon == null || currentWeaponKey == KeyCode.None || !cacheGroupToSlots.ContainsKey(weapon.GetGroup()))
            {
                return(false);
            }

            WeaponID currentWeapon = GetActiveWeaponID();

            if (currentWeapon.GetGroup() != weapon.GetGroup())
            {
                return(false);
            }

            replaceWeaponAnimation = ReplaceWeaponAnimation(weapon);
            StartCoroutine(replaceWeaponAnimation);

            List <InventorySlot> slots = cacheGroupToSlots[weapon.GetGroup()];

            if (slots == null || slots.Count == 0)
            {
                return(false);
            }

            for (int i = 0, length = slots.Count; i < length; i++)
            {
                InventorySlot slot = slots[i];
                if (slot.GetWeapon() == currentWeapon)
                {
                    cacheKeyToID[slot.GetKey()] = weapon;
                    slot.SetWeapon(weapon);
                    cacheGroupToSlots[weapon.GetGroup()][i] = slot;
                    return(true);
                }
            }

            return(false);
        }
예제 #5
0
        protected virtual IEnumerator ReplaceWeaponAnimation(WeaponID targetWeaponID)
        {
            if (currentWeaponKey != KeyCode.None)
            {
                WeaponID        currentID        = cacheKeyToID[currentWeaponKey];
                Transform       currentTransform = cacheIDToTransform[currentID];
                DropProperties  dropProperties   = currentID.GetDropProperties();
                IWeaponAnimator currrentAnimator = null;
                if (currentTransform != null)
                {
                    currrentAnimator = currentTransform.GetComponent <IWeaponAnimator>();
                }
                else
                {
                    switchWeaponAnimation = null;
                    yield break;
                }

                if (currrentAnimator != null)
                {
                    currrentAnimator.PutAway();
                    yield return(new WaitForSeconds(currrentAnimator.GetPutAwayTime()));
                }

                if (dropProperties != DropProperties.Empty)
                {
                    Vector3    pos        = _FPCamera.position + (_FPCamera.forward * dropProperties.GetDistance());
                    GameObject dropObject = Instantiate(dropProperties.GetDropObject(), pos, Quaternion.Euler(dropProperties.GetRotation()));
                    if (dropObject != null)
                    {
                        Rigidbody rigidbody = dropObject.GetComponent <Rigidbody>();
                        if (rigidbody != null)
                        {
                            rigidbody.AddForce(_FPCamera.forward * dropProperties.GetForce(), ForceMode.Impulse);
                        }
                    }
                }

                currentTransform.gameObject.SetActive(false);
                currentWeaponKey = KeyCode.None;
            }

            Transform       targetTransform = cacheIDToTransform[targetWeaponID];
            IWeaponAnimator targetAnimator  = null;

            if (targetTransform != null)
            {
                targetAnimator = targetTransform.GetComponent <IWeaponAnimator>();
            }
            else
            {
                switchWeaponAnimation = null;
                yield break;
            }

            targetTransform.gameObject.SetActive(true);

            if (targetAnimator != null)
            {
                yield return(new WaitForSeconds(targetAnimator.GetTakeTime()));
            }

            List <InventorySlot> slots = cacheGroupToSlots[targetWeaponID.GetGroup()];

            for (int i = 0, length = slots.Count; i < length; i++)
            {
                InventorySlot slot = slots[i];
                if (slot.GetWeapon() == targetWeaponID)
                {
                    currentWeaponKey = slot.GetKey();
                    break;
                }
            }
            switchWeaponAnimation = null;
            yield break;
        }
예제 #6
0
        protected virtual IEnumerator SwitchWeaponAnimation(WeaponID targetWeaponID)
        {
            if (currentWeaponKey != KeyCode.None)
            {
                WeaponID        currentID        = cacheKeyToID[currentWeaponKey];
                Transform       currentTransform = cacheIDToTransform[currentID];
                IWeaponAnimator currrentAnimator = null;
                if (currentTransform != null)
                {
                    currrentAnimator = currentTransform.GetComponent <IWeaponAnimator>();
                }
                else
                {
                    switchWeaponAnimation = null;
                    yield break;
                }

                if (currrentAnimator != null)
                {
                    currrentAnimator.PutAway();
                    yield return(new WaitForSeconds(currrentAnimator.GetPutAwayTime()));
                }

                currentTransform.gameObject.SetActive(false);
                currentWeaponKey = KeyCode.None;
            }

            Transform       targetTransform = cacheIDToTransform[targetWeaponID];
            IWeaponAnimator targetAnimator  = null;

            if (targetTransform != null)
            {
                targetAnimator = targetTransform.GetComponent <IWeaponAnimator>();
            }
            else
            {
                switchWeaponAnimation = null;
                yield break;
            }

            targetTransform.gameObject.SetActive(true);

            if (targetAnimator != null)
            {
                yield return(new WaitForSeconds(targetAnimator.GetTakeTime()));
            }

            List <InventorySlot> slots = cacheGroupToSlots[targetWeaponID.GetGroup()];

            for (int i = 0, length = slots.Count; i < length; i++)
            {
                InventorySlot slot = slots[i];
                if (slot.GetWeapon() == targetWeaponID)
                {
                    currentWeaponKey = slot.GetKey();
                    break;
                }
            }
            switchWeaponAnimation = null;
            yield break;
        }