void UI_InteractHover() { //Make sure player is hovering over an item GameObject itemInView = playerInteract.GetItemInView(); if (itemInView) { textInteractable.text = ""; //Update interact UI text Interactable interactable = itemInView.GetComponent <Interactable>(); Pickupable pickupable = itemInView.GetComponent <Pickupable>(); if (pickupable) { textInteractable.text = "Press E to pickup " + pickupable.item.name; } else if (interactable && GetItemInViewName() != "") { string interactName = GetItemInViewName(); Pickupable currentlyEquipped = playerInteract.GetComponent <EquipSystem>().currentEquippedItem; if (currentlyEquipped) { IItemUsable itemUsable = interactable as IItemUsable; if ((itemUsable != null && !itemUsable.IsCorrect()) && currentlyEquipped) //Display 'Use [item] on [itemhover]' { textInteractable.text = "Use " + currentlyEquipped.item.name + " on " + interactName; } } else { textInteractable.text = interactName; } } else { isVisible = false; StartCoroutine(DelayedFadeOut()); } //Make text visible if it is not already if (!isVisible && textInteractable.text != "") { //Make text visible if it is not already if (!isVisible) { isVisible = true; interactAnimator.SetBool("isVisible", isVisible); } } } else if (isVisible) { //Delay fade out to make sure text does not blink while quickly switching targets isVisible = false; StartCoroutine(DelayedFadeOut()); } }
/// <summary> /// Functionality of using an item on a IItemUsable component /// </summary> /// <returns>Whether the item could be used.</returns> public virtual bool Use(GameObject user, Interactable itemInView) { if (itemInView) { IItemUsable itemUsable = itemInView.GetComponent <IItemUsable>(); if (itemUsable != null) { return(itemUsable.OnItemUse(user, item)); } } return(false); }
void Update() { if (IsHolding) { // Don't care where we are, right click returns item to inventory if (Input.GetMouseButtonDown(1)) { StopHolding(); return; } else { // No effect - item cannot be used bool isHighlight = false; GetComponent <SpriteRenderer>().color = Color.white; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D[] objects = Physics2D.RaycastAll(ray.origin, ray.direction); foreach (var obj in objects) { IItemUsable u = obj.collider.gameObject.GetComponent <IItemUsable>(); if (u != null) { if (u.CanUseOnSelf(CurrentItemId)) { // Effect to show that the item can be used isHighlight = true; GetComponent <SpriteRenderer>().color = Color.yellow; if (Input.GetMouseButton(0)) { // Stop holding before usage to return item to inventory and then remove it via action // sometimes use on self invokes coroutine move closer - then we would be fine - item returns and is later removed // but other times there is no coroutine involved, item is in hand so action cannot remove it from inventory // and StopHolding just returns it to inventory for no reason int itemId = CurrentItemId; StopHolding(); u.UseOnSelf(itemId); return; } } } } ToggleHighlight(isHighlight); } MoveToCursor(); } }
protected override string GetInteractableMessage(Interactable interactable) { string returnString = base.GetInteractableMessage(interactable); IItemUsable usableInteractable = interactable as IItemUsable; bool canUseItem = usableInteractable != null && usableInteractable.CanUse(); if (owningPlayer && canUseItem) { var equip = owningPlayer.GetComponent <EquipManager>(); if (equip && equip.GetEquippedItem()) { return("Use " + equip.GetEquippedItem().GetName() + " on " + interactable.GetName()); } } return(returnString); }