Exemplo n.º 1
0
        public bool Try_DropItem(SavableItem item, Slot parentSlot = null)
        {
            if (item && item.ItemData.WorldObject && !item.ItemData.IsBuildable)
            {
                var        cameraTransform = GameController.WorldCamera.transform;
                GameObject droppedItem     = Instantiate(item.ItemData.WorldObject, cameraTransform.position + cameraTransform.TransformVector(m_DropOffset), Random.rotation) as GameObject;

                var rigidbody = droppedItem.GetComponent <Rigidbody>();
                if (rigidbody)
                {
                    rigidbody.angularVelocity = Random.rotation.eulerAngles * m_DropAngularFactor;
                    rigidbody.AddForce(cameraTransform.forward * m_DropSpeed, ForceMode.VelocityChange);

                    Physics.IgnoreCollision(m_Player.GetComponent <Collider>(), droppedItem.GetComponent <Collider>());
                }

                var pickup = droppedItem.GetComponent <ItemPickup>();
                if (pickup)
                {
                    pickup.ItemToAdd = item;
                }

                if (parentSlot)
                {
                    parentSlot.ItemHolder.SetItem(null);
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        private void On_ItemNotStackable(Slot slotUnderPointer, Slot initialSlot)
        {
            if (!initialSlot.AllowsItem(slotUnderPointer.CurrentItem))
            {
                PutItemBack(initialSlot);
                return;
            }

            // Swap the items because they are of different kinds / not stackable / reached maximum stack size.
            SavableItem temp = slotUnderPointer.CurrentItem;

            if (!initialSlot.HasItem)
            {
                slotUnderPointer.ItemHolder.SetItem(m_DraggedItem);
                initialSlot.ItemHolder.SetItem(temp);
            }
            else
            {
                // Add as much as possible to the item's stack.
                int added;
                initialSlot.Parent.TryAddItem(m_DraggedItem.ItemData, m_DraggedItem.CurrentInStack, out added);

                // Add the remained items too.
                int remainedToAdd = m_DraggedItem.CurrentInStack - added;
                if (remainedToAdd > 0)
                {
                    initialSlot.Parent.TryAddItem(m_DraggedItem.ItemData, remainedToAdd);
                }
            }
        }
Exemplo n.º 3
0
        private bool Try_ChangeEquippedItem(SavableItem item, bool instantly)
        {
            if (Player.EquippedItem.Get() == item)
            {
                return(true);
            }

            // Register the object for equipping.
            m_WaitingToEquip   = true;
            m_NextTimeCanEquip = Time.time;

            if (!instantly && m_EquippedObject != null)
            {
                m_NextTimeCanEquip += m_HolsterTime;
            }

            // Register the current equipped object for disabling.
            if (m_EquippedObject != null)
            {
                m_EquippedObject.On_Holster();

                m_WaitingToDisable   = true;
                m_NextTimeCanDisable = Time.time;

                if (!instantly)
                {
                    m_NextTimeCanDisable += m_HolsterTime;
                }
            }

            Player.EquippedItem.Set(item);

            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        public virtual void On_Draw(SavableItem correspondingItem)
        {
            IsEnabled         = true;
            CorrespondingItem = correspondingItem;
            LastDrawTime      = Time.time;
            m_Durability      = correspondingItem.GetPropertyValue("Durability");

            Draw.Send();
        }
Exemplo n.º 5
0
        private void Awake()
        {
            var database = InventoryController.Instance.Database;

            ItemData itemData;

            if (database && database.FindItemByName(m_DefaultItem, out itemData))
            {
                ItemToAdd = new SavableItem(itemData, m_DefaultAmount);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Removes the item if it exists in the inventory, if not, the method will return false.
        /// </summary>
        public bool TryRemoveItem(SavableItem item)
        {
            if (!enabled)
            {
                return(false);
            }

            for (int i = 0; i < m_AllCollections.Length; i++)
            {
                bool removed = m_AllCollections[i].TryRemoveItem(item);
                if (removed)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        public void SetItem(SavableItem item)
        {
            if (CurrentItem)
            {
                CurrentItem.PropertyChanged.RemoveListener(On_PropertyChanged);
                CurrentItem.StackChanged.RemoveListener(On_StackChanged);
            }

            CurrentItem = item;

            if (CurrentItem)
            {
                CurrentItem.PropertyChanged.AddListener(On_PropertyChanged);
                CurrentItem.StackChanged.AddListener(On_StackChanged);
            }

            Updated.Send(this);
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        private void On_Slot_BeginDrag(PointerEventData data, Slot slot, ItemContainer collection)
        {
            if (!slot.HasItem || InventoryController.Instance.IsClosed)
            {
                return;
            }

            m_Dragging = true;
            SavableItem itemUnderPointer = slot.CurrentItem;

            // Stack splitting.
            if (Input.GetKey(m_SplitKey) && itemUnderPointer.CurrentInStack > 1)
            {
                int initialAmount = itemUnderPointer.CurrentInStack;
                int half          = initialAmount / 2;
                itemUnderPointer.CurrentInStack = initialAmount - half;

                m_DraggedItem = new SavableItem(itemUnderPointer.ItemData, half, itemUnderPointer.CurrentPropertyValues);
                slot.Refresh();
            }
            else
            {
                slot.ItemHolder.SetItem(null);
                m_DraggedItem = itemUnderPointer;
            }

            m_DraggedItemRT = slot.GetDragTemplate(m_DraggedItem, m_DraggedItemAlpha);
            m_DraggedItemRT.SetParent(m_ParentCanvasRT, true);
            m_DraggedItemRT.localScale = Vector3.one * m_DraggedItemScale;

            Camera  cam = data.pressEventCamera;
            Vector3 worldPoint;

            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_ParentCanvasRT, (Vector3)data.position, cam, out worldPoint))
            {
                m_DragOffset = slot.transform.position - worldPoint;
            }

            //Debug.Break();
        }
Exemplo n.º 9
0
        public bool TryGenerate(out SavableItem runtimeItem)
        {
            runtimeItem = null;
            var      database = InventoryController.Instance.Database;
            ItemData itemData;

            if (m_Random)
            {
                if (database.FindItemById(UnityEngine.Random.Range(0, database.GetItemCount() - 1), out itemData))
                {
                    runtimeItem = new SavableItem(itemData, (int)(itemData.StackSize * 0.1f) + 1);
                    return(true);
                }
            }
            else if (database.FindItemByName(m_CustomName, out itemData))
            {
                runtimeItem = new SavableItem(itemData, m_StackSize);
                return(true);
            }

            return(false);
        }
Exemplo n.º 10
0
        public override void On_Draw(SavableItem correspondingItem)
        {
            base.On_Draw(correspondingItem);
            //m_AmmoProperty = correspondingItem.GetPropertyValue("Ammo Type");
            if (correspondingItem && correspondingItem.HasProperty("Ammo Type"))
            {
                m_AmmoProperty = correspondingItem.GetPropertyValue("Ammo Type");
            }


            if (correspondingItem && correspondingItem.HasProperty("Ammo Capacity"))
            {
                m_AmmoProperty = correspondingItem.GetPropertyValue("Ammo Capacity");
            }



            if (correspondingItem && correspondingItem.HasProperty("Ammo"))
            {
                m_AmmoProperty = correspondingItem.GetPropertyValue("Ammo");
            }
        }
Exemplo n.º 11
0
        public bool TryAddItem(ItemData itemData, int amount, out int added, List <ItemProperty.Value> customPropertyValues = null)
        {
            added = 0;

            if (HasItem && itemData.Id != CurrentItem.Id)
            {
                return(false);
            }

            if (!HasItem)
            {
                CurrentItem = new SavableItem(itemData, 1, customPropertyValues);
                CurrentItem.CurrentInStack = 0;
                CurrentItem.PropertyChanged.AddListener(On_PropertyChanged);
                CurrentItem.StackChanged.AddListener(On_StackChanged);
            }

            int oldValue       = CurrentItem.CurrentInStack;
            int surplus        = amount + oldValue - itemData.StackSize;
            int currentInStack = oldValue;

            if (surplus <= 0)
            {
                currentInStack += amount;
            }
            else
            {
                currentInStack = itemData.StackSize;
            }

            CurrentItem.CurrentInStack = currentInStack;
            added = currentInStack - oldValue;

            Updated.Send(this);

            return(added > 0);
        }
Exemplo n.º 12
0
        //private ItemProperty.Value m_AmmoProperty;


        public override void On_Draw(SavableItem correspondingItem)
        {
            base.On_Draw(correspondingItem);
            //m_AmmoProperty = correspondingItem.GetPropertyValue("Ammo Type");
        }
Exemplo n.º 13
0
 public override void On_Draw(SavableItem correspondingItem)
 {
     base.On_Draw(correspondingItem);
     StopAllCoroutines();
     StartCoroutine(C_ToggleTorch(true));
 }
Exemplo n.º 14
0
        /// <summary>
        ///
        /// </summary>
        private void On_Slot_EndDrag(PointerEventData data, Slot initialSlot, ItemContainer collection)
        {
            if (!m_Dragging)
            {
                return;
            }

            var slots = collection.Slots;

            GameObject objectUnderPointer = data.pointerCurrentRaycast.gameObject;
            Slot       slotUnderPointer   = null;

            if (objectUnderPointer)
            {
                slotUnderPointer = objectUnderPointer.GetComponent <Slot>();
            }

            // Is there a slot under our pointer?
            if (slotUnderPointer)
            {
                // See if the slot allows this type of item.
                if (slotUnderPointer.AllowsItem(m_DraggedItem))
                {
                    // If the slot is empty...
                    if (!slotUnderPointer.HasItem)
                    {
                        if (slotUnderPointer.ItemHolder)
                        {
                            slotUnderPointer.ItemHolder.SetItem(m_DraggedItem);
                        }
                        else
                        {
                            Debug.LogError("You tried to drop an item over a Slot which is not linked with any holder.", this);
                        }
                    }
                    // If the slot is not empty...
                    else
                    {
                        SavableItem itemUnderPointer = slotUnderPointer.CurrentItem;

                        // Can we stack the items?
                        bool canStackItems = (itemUnderPointer.Id == m_DraggedItem.Id && itemUnderPointer.ItemData.StackSize > 1 && itemUnderPointer.CurrentInStack < itemUnderPointer.ItemData.StackSize);
                        if (canStackItems)
                        {
                            OnItemsAreStackable(slotUnderPointer, initialSlot);
                        }
                        else
                        {
                            On_ItemNotStackable(slotUnderPointer, initialSlot);
                        }
                    }
                }
                else
                {
                    PutItemBack(initialSlot);
                }

                initialSlot.Refresh();
            }
            // If the player didn't drop it on a slot...
            else
            {
                if (PlayerDroppedItem != null)
                {
                    PlayerDroppedItem(collection, initialSlot, m_DraggedItem);
                }

                if (!InventoryController.Instance.Try_DropItem(m_DraggedItem))
                {
                    collection.TryAddItem(m_DraggedItem);
                }
            }

            Destroy(m_DraggedItemRT.gameObject);
            m_DraggedItem = null;
            m_Dragging    = false;

            //initialSlot.interactable = initialSlot.HasItem;
        }
Exemplo n.º 15
0
        //reload Ammo
        public bool LoadAmmo(Slot initialSlot, SavableItem itemUnderPointer)
        {
            bool canLoadAmmo = (
                //make sure that the item that you drag and the item in the slot are exixcte
                itemUnderPointer && m_DraggedItem &&
                //make sure that both items have the property ammo type , so you dont get null values
                m_DraggedItem.HasProperty("Ammo Type") && itemUnderPointer.HasProperty("Ammo Type") &&
                //make sure that the weapon and the ammo have the same Ammo type
                m_DraggedItem.GetPropertyValue("Ammo Type").String == itemUnderPointer.GetPropertyValue("Ammo Type").String&&
                //make sure that only the drager item is an Ammo and not the item already in the slot
                m_DraggedItem.HasProperty("Is Ammo") && m_DraggedItem.GetPropertyValue("Is Ammo").Bool&&
                !itemUnderPointer.HasProperty("Is Ammo") && itemUnderPointer.HasProperty("Ammo Capacity") && itemUnderPointer.HasProperty("Ammo "));

            if (canLoadAmmo)
            {
                //define out variables
                //pick up amm stock size
                int ammostack = m_DraggedItem.CurrentInStack;
                //current ammo
                m_AmmoAmount = itemUnderPointer.GetPropertyValue("Ammo");
                var ammo = m_AmmoAmount.Int;
                //capacity
                m_AmmoCapacity = itemUnderPointer.GetPropertyValue("Ammo Capacity");
                //make sure taht the gun is not already full loaded
                if (m_AmmoAmount.Int.Current < m_AmmoCapacity.Int.Current)
                {
                    //check if there ammo in object we pick up
                    if (ammostack > 0)
                    {
                        int needAmmo = m_AmmoCapacity.Int.Current - m_AmmoAmount.Int.Current;
                        //if you need more mmothan what is the enter stack we consule all the ammo
                        if (needAmmo >= ammostack)
                        {
                            //reload the gun
                            ammo.Current += ammostack;//has to be correct type
                            m_AmmoAmount.SetValue(ItemProperty.Type.Int, ammo);
                            GameController.LocalPlayer.Reload.Try(needAmmo);

                            //destroy Ammo
                            Destroy(m_DraggedItemRT.gameObject);
                            m_DraggedItem = null;
                            m_Dragging    = false;
                            initialSlot.Refresh();
                            return(true);
                        }
                        //else we will use all the ammo we can and return what is left
                        else
                        {
                            ammo.Current += needAmmo;//has to correct type
                            m_AmmoAmount.SetValue(ItemProperty.Type.Int, ammo);
                            int remainingAmmo = ammostack - needAmmo;
                            m_DraggedItem.CurrentInStack = remainingAmmo;
                            GameController.LocalPlayer.Reload.Try(needAmmo);

                            //put what it left in our ammo stack where we pick it up
                            PutItemBack(initialSlot);
                            initialSlot.Refresh();

                            //destroy the ammo(because we already put it back)
                            Destroy(m_DraggedItemRT.gameObject);
                            m_DraggedItem = null;
                            m_Dragging    = false;
                            return(true);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    //Gun is full /put the bullet back
                    PutItemBack(initialSlot);
                    initialSlot.Refresh();

                    //destroy the ammo(because we already put it back)
                    Destroy(m_DraggedItemRT.gameObject);
                    m_DraggedItem = null;
                    m_Dragging    = false;
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }