예제 #1
0
        /// <summary>The player uses the specified IPickUpable</summary>
        /// <param name="inventoryIndex">The index in inventory of the IPickUpable to use</param>
        public void UseInventoryItem(int inventoryIndex)
        {
            IPickUpable item = null;

            if (inventoryIndex < Inventory.Count)
            {
                item = Inventory[inventoryIndex];
            }
            if (item != null)
            {
                item.Use(this);
                if (item.Count <= 0)
                {
                    Inventory.Remove(item);
                }
            }
        }
예제 #2
0
    private void HandleRaycasting()
    {
        string     message = "";
        RaycastHit hit;

        if (Physics.Raycast(camTransform.position, camTransform.forward, out hit, maxInteractDist))
        {
            //check for interactable objects
            if (hit.collider.CompareTag("Interactable"))
            {
                IInteractable interactable = hit.collider.gameObject.GetComponent(typeof(IInteractable)) as IInteractable; //get interactable object

                //display interaction message
                message = interactable.InteractMessage;

                //let player interact with 'E'
                if (Input.GetKeyDown(KeyCode.E))
                {
                    interactable.Interact();
                }
            }

            //check for pick up items
            else if (hit.collider.CompareTag("PickUp"))
            {
                IPickUpable pickup = hit.collider.gameObject.GetComponent(typeof(IPickUpable)) as IPickUpable; //get pickup item
                message = pickup.PickUpMessage;

                if (Input.GetKeyDown(KeyCode.E))
                {
                    GameObject pickupObj = hit.collider.gameObject;
                    inv.AddItem(pickup, pickupObj);
                }
            }
        }

        //display message
        actionPrompt.text = message;
    }
예제 #3
0
        public void AddItem(IPickUpable item, GameObject itemObj)
        {
            if (item.Type == PickUpType.Battery)
            {
                ++numBatteries;
                Destroy(itemObj);
            }

            else if (item.Type == PickUpType.KeyItem)
            {
                KeyItem key = item as KeyItem;
                keyRing.Add(key.DoorID);
                Destroy(itemObj);
            }

            else
            {
                Debug.LogError("PlayerInventory does not have handling for this PickUpType!");
            }

            UpdateUI();
        }
예제 #4
0
        //Methods
        /// <summary>Adds an IPickUpable to the player's inventory</summary>
        /// <param name="itemToAdd">The IPickUpable to add</param>
        public void AddItem(IPickUpable itemToAdd)
        {
            bool exists      = false;
            int  insertIndex = 0;

            foreach (IPickUpable pickUp in Inventory)
            {
                if (pickUp.Name == itemToAdd.Name)
                {
                    exists = true;
                    pickUp.Count++;
                }
                if (string.Compare(itemToAdd.Name, pickUp.Name, true) > 0)
                {
                    insertIndex++;
                }
            }

            if (!exists)
            {
                Inventory.Insert(insertIndex, itemToAdd);
            }
        }
예제 #5
0
        private void DisplayInventory()
        {
            Console.SetCursorPosition(WorldWidth + 2, 0);
            Console.Write("Backpack contains: ");
            int i = 0;

            for (; i < player.Inventory.Count; i++)
            {
                Console.SetCursorPosition(WorldWidth + 2, i + 1);
                IPickUpable pickUp = player.Inventory[i];
                if (pickUp.Count == 1)
                {
                    Console.Write($"{i + 1}. {pickUp.Name}");
                }
                else if (pickUp.Count > 1)
                {
                    Console.Write($"{i + 1}. {pickUp.Name} x{pickUp.Count}");
                }
                ClearLine();
            }
            Console.SetCursorPosition(WorldWidth + 2, i + 1);
            ClearLine();
        }
예제 #6
0
 void PickUp(IPickUpable p)
 {
     curItem = p;
     p.GetPickedUp(this);
 }
예제 #7
0
 void Release(IPickUpable t)
 {
     t?.Release(this);
     curItem = null;
 }