コード例 #1
0
        protected virtual IEnumerator HideWeaponAnimation()
        {
            WeaponID        currentID        = cacheKeyToID[currentWeaponKey];
            Transform       curTransform     = cacheIDToTransform[currentID];
            IWeaponAnimator currrentAnimator = null;

            if (curTransform != null)
            {
                currrentAnimator = curTransform.GetComponent <IWeaponAnimator>();
            }
            else
            {
                hideWeaponAnimation = null;
                yield break;
            }

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

            curTransform.gameObject.SetActive(false);
            hideWeaponAnimation = null;
            yield break;
        }
コード例 #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>
        /// 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);
        }
コード例 #4
0
 /// <summary>
 /// Get weapon transform by id.
 /// </summary>
 public virtual Transform GetWeapon(WeaponID weapon)
 {
     if (weapon == null || !cacheKeyToID.ContainsValue(weapon))
     {
         return(null);
     }
     return(cacheIDToTransform[weapon]);
 }
コード例 #5
0
        protected virtual IEnumerator SwitchWeaponAnimation(KeyCode targetWeaponKey)
        {
            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;
            }

            WeaponID targetID = cacheKeyToID[targetWeaponKey];

            if (targetID == null)
            {
                switchWeaponAnimation = null;
                yield break;
            }
            Transform       targetTransform = cacheIDToTransform[targetID];
            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()));
            }

            currentWeaponKey      = targetWeaponKey;
            switchWeaponAnimation = null;
            yield break;
        }
コード例 #6
0
        /// <summary>
        /// Get active weapon transform.
        /// </summary>
        public virtual Transform GetActiveWeaponTransform()
        {
            if (currentWeaponKey == KeyCode.None || !cacheKeyToID.ContainsKey(currentWeaponKey))
            {
                return(null);
            }

            WeaponID id = cacheKeyToID[currentWeaponKey];

            if (id == null)
            {
                return(null);
            }

            return(cacheIDToTransform[id]);
        }
コード例 #7
0
        /// <summary>
        /// Activate weapon by ID.
        /// </summary>
        public virtual bool ActivateWeapon(WeaponID weapon)
        {
            if (!cacheKeyToID.ContainsValue(weapon))
            {
                return(false);
            }

            if (hideWeaponAnimation != null || switchWeaponAnimation != null || dropWeaponAnimation != null)
            {
                return(false);
            }

            switchWeaponAnimation = SwitchWeaponAnimation(weapon);
            StartCoroutine(switchWeaponAnimation);
            return(true);
        }
コード例 #8
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);
        }
コード例 #9
0
        protected virtual IEnumerator DropWeaponAnimation()
        {
            WeaponID        currentID        = cacheKeyToID[currentWeaponKey];
            Transform       curTransform     = cacheIDToTransform[currentID];
            DropProperties  dropProperties   = currentID.GetDropProperties();
            IWeaponAnimator currrentAnimator = null;

            if (curTransform != null)
            {
                currrentAnimator = curTransform.GetComponent <IWeaponAnimator>();
            }

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

            curTransform.gameObject.SetActive(false);
            Remove(currentID);

            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);
                    }
                }
            }

            currentWeaponKey    = KeyCode.None;
            dropWeaponAnimation = null;
            yield break;
        }
コード例 #10
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);
        }
コード例 #11
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;
        }
コード例 #12
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;
        }
 public void SetWeapon(WeaponID value)
 {
     weapon = value;
 }