private void closeCurrentSelectionMenu()
    {
        if (aSelectionMenuIsOpen)
        {
            if (selectedWorldObject != null)
            {
                InteractableObjectScript unselectedWorldObject = selectedWorldObject;
                selectedWorldObject = null;
                GameObject.FindGameObjectWithTag("SelectedObjectMenu_UIText").GetComponent <SelectedObjectMenuScript>().closeMenu();

                // check if mouse is still over object
                RaycastHit mouseRayHit;
                Ray        mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(mouseRay, out mouseRayHit))
                {
                    InteractableObjectScript hitObjectInteractionScript = mouseRayHit.collider.GetComponent <InteractableObjectScript>();
                    if (hitObjectInteractionScript == unselectedWorldObject)
                    {
                        setHighlightedWorldObject(hitObjectInteractionScript);
                    }
                }
            }

            if (selectedInventoryItem != null)
            {
                selectedInventoryItem.closeItemMenu();
                selectedInventoryItem = null;
            }

            aSelectionMenuIsOpen = false;
        }
    }
    public void checkIfMouseHighlightingObject()
    {
        // resets mouse ray and sets highlighted object to null if an object is deactivated while mouse is hovering over it
        stopHighlightingWorldObject(highlightedWorldObject);
        RaycastHit mouseRayHit;
        Ray        mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(mouseRay, out mouseRayHit))
        {
            InteractableObjectScript hitObjectInteractionScript = mouseRayHit.collider.GetComponent <InteractableObjectScript>();
            setHighlightedWorldObject(hitObjectInteractionScript);
        }
    }
    public void objectClicked(InteractableObjectScript objectScript, Vector3 position)
    {
        // close menu if already open
        closeMenu();

        currentObjectScript                  = objectScript;
        GetComponent <Text>().enabled        = true;
        nameOfCurrentObject                  = objectScript.data.actorName;
        GetComponent <Text>().text           = nameOfCurrentObject;
        transform.position                   = position;
        currentPossibleInteractions          = objectScript.interactions;
        numberOfInteractionsForCurrentObject = objectScript.numberOfInteractions;

        for (int i = 0; i < objectScript.numberOfInteractions; i++)
        {
            interactionButtons[i].gameObject.SetActive(true);

            switch (objectScript.interactions[i])
            {
            case InteractableObjectScript.InteractionType.Examine:
                interactionButtons[i].GetComponent <Image>().sprite = examineButtonSprite; break;

            case InteractableObjectScript.InteractionType.GoTo:
                interactionButtons[i].GetComponent <Image>().sprite = goToButtonSprite; break;

            case InteractableObjectScript.InteractionType.PickUp:
                interactionButtons[i].GetComponent <Image>().sprite = pickUpButtonSprite; break;

            case InteractableObjectScript.InteractionType.TalkTo:
                interactionButtons[i].GetComponent <Image>().sprite = talkToButtonSprite; break;

            case InteractableObjectScript.InteractionType.Use:
                interactionButtons[i].GetComponent <Image>().sprite = useButtonSprite; break;

            case InteractableObjectScript.InteractionType.Separate:
                interactionButtons[i].GetComponent <Image>().sprite = separateButtonSprite; break;
            }

            if (objectScript.numberOfInteractions == 2)
            {
                interactionButtons[i].transform.position = new Vector2(position.x - (gapBetweenButtons / 2) + (i * gapBetweenButtons), position.y + 30);
            }

            else if (objectScript.numberOfInteractions == 3)
            {
                interactionButtons[i].transform.position = new Vector2(position.x - gapBetweenButtons + (i * gapBetweenButtons), position.y + 30);
            }
        }
    }
    public bool combineActors(ItemInteractionScript inventorySlotOfFirstItem, InteractableObjectScript worldObjectCombinedWith)
    {
        bool actorsNeedSwitching = false;
        // check if these actors can be combined
        int indexOfSecondActor = getIndexOfSecondActor(inventorySlotOfFirstItem.dataOfItemInSlot, worldObjectCombinedWith.data);

        if (indexOfSecondActor == -1)
        {
            // switch first actor and second actor and check again
            indexOfSecondActor = getIndexOfSecondActor(worldObjectCombinedWith.data, inventorySlotOfFirstItem.dataOfItemInSlot);
            if (indexOfSecondActor == -1)
            {
                return(false);
            }
            else
            { // switch the actors around
                actorsNeedSwitching = true;
            }
        }

        ActorData firstActorData;
        ActorData secondActorData;

        if (actorsNeedSwitching)
        {
            firstActorData  = worldObjectCombinedWith.data;
            secondActorData = inventorySlotOfFirstItem.dataOfItemInSlot;
        }
        else
        {
            firstActorData  = inventorySlotOfFirstItem.dataOfItemInSlot;
            secondActorData = worldObjectCombinedWith.data;
        }

        // remove actors if necessary
        if (firstActorData.actorsThisCanBeCombinedWith[indexOfSecondActor].outcomeForThisActor == ActorData.ActorOutcomeAfterCombination.Deactivate)
        {
            FindObjectOfType <InventoryScript>().removeItem(firstActorData);
        }

        if (firstActorData.actorsThisCanBeCombinedWith[indexOfSecondActor].outcomeForOtherActor == ActorData.ActorOutcomeAfterCombination.Deactivate)
        {
            currentScene.deactivateWorldObject(worldObjectCombinedWith.gameObject);
        }

        resolveComination(firstActorData, secondActorData, indexOfSecondActor);

        return(true);
    }
 public void stopHighlightingWorldObject(InteractableObjectScript worldObject)
 {
     if (highlightedWorldObject == worldObject)
     {
         highlightedWorldObject = null;
         if (!combiningInProgress)
         {
             UI_nameOfHighlightedObject.GetComponent <Text>().enabled = false;
         }
         else
         {
             UI_nameOfHighlightedObject.GetComponent <Text>().text = "Combine with ...";
         }
     }
 }
    public bool separateActor(InteractableObjectScript worldObjectToSeparate)
    {
        // splits previously combined world object into components

        ActorData objectToSeparateData = worldObjectToSeparate.data;

        if (!resolveSeparation(objectToSeparateData))
        {
            Debug.Log("separation failed");
            return(false); // return false if separation is not possible
        }

        currentScene.deactivateWorldObject(worldObjectToSeparate.gameObject);

        return(true);
    }
    public void worldObjectClickedOn(InteractableObjectScript worldObject)
    {
        if (combiningInProgress)
        {
            setQueuedAction(worldObject.gameObject, InteractableObjectScript.InteractionType.Combine);
            combiningInProgress = false;
            setHighlightedWorldObject(worldObject);
        }

        else
        {
            closeCurrentSelectionMenu();
            selectedWorldObject = worldObject;
            GameObject.FindGameObjectWithTag("SelectedObjectMenu_UIText").GetComponent <SelectedObjectMenuScript>().objectClicked(selectedWorldObject.GetComponent <InteractableObjectScript>(), new Vector3(Input.mousePosition.x, Input.mousePosition.y + 20, Input.mousePosition.z));
            aSelectionMenuIsOpen = true;
            if (worldObject == highlightedWorldObject)
            {
                stopHighlightingWorldObject(highlightedWorldObject);
            }
        }
    }
Example #8
0
    void updateTargets_Interactable(List <string> targetList)
    {
        /*
         * foreach(InteractableObjectScript i in targetInteractable)
         * {
         *  i.setOutline(0f, Color.white);
         * }
         * targetInteractable = new List<InteractableObjectScript>();
         */
        if (interactTarget != null)
        {
            interactTarget.setOutline(0f, Color.white);
        }
        interactTarget = null;
        Vector2      dis = (targetPosition - (Vector2)transform.position);
        RaycastHit2D hit = Physics2D.Raycast(transform.position, dis.normalized, dis.magnitude, layerMask_interactable);

        if (hit)
        {
            interactTarget = hit.collider.GetComponent <InteractableObjectScript>();
            interactTarget.setOutline(1f, Color.white);
        }

        /*
         * foreach (RaycastHit2D h in hits)
         * {
         *  if (targetList.Contains(h.collider.tag))
         *  {
         *      targetInteractable.Add(h.collider.GetComponent<InteractableObjectScript>());
         *  }
         * }
         * foreach (InteractableObjectScript i in targetInteractable)
         * {
         *  i.setOutline(1f, Color.white);
         * }
         */
    }
    //Checks for colliders within a certain distance
    //currently returns the array in a completely random order, this should be changed to ascending distance order so the closest object will be the interactable one. (shortest to longest)
    //so when we hit an interactable object we can just exit the foreach  loop as we dont need to check further ones.
    void CheckForInteractions()
    {
        //Grabs all colliders within 5 units
        Collider[] hitColliders = Physics.OverlapSphere(transform.position, 5.0f);

        //iterator to test if we need to clear the interactableobject
        int i = 0;


        foreach (Collider collider in hitColliders)
        {
            //Grabs the script from the colliders
            InteractableObjectScript script = collider.GetComponent <InteractableObjectScript>();

            //if there is no script
            if (script == null)
            {
                //increment iterator by one
                i++;

                //if i is not equal to the length than continue
                if (i != hitColliders.Length)
                {
                    continue;
                }

                //else if it is equal to the length clear the interactableobject
                InteractableObject = null;

                continue;
            }

            //if there is a script this is the new interactable object
            InteractableObject = collider;
        }
    }
    public void setHighlightedWorldObject(InteractableObjectScript worldObject)
    {
        // don't highlight if object is already selected
        if (worldObject == selectedWorldObject)
        {
            return;
        }

        // if the mouse is over a UI element, they shouldn't be able to highlight objects beneath UI
        if (mouseOverUI)
        {
            return;
        }

        // clear current highlighted object
        if (highlightedWorldObject != null)
        {
            stopHighlightingWorldObject(highlightedWorldObject);
        }

        // set this object as highlighted object
        if (worldObject != null)
        {
            highlightedWorldObject = worldObject;
            UI_nameOfHighlightedObject.GetComponent <Text>().enabled = true;
            if (combiningInProgress)
            {
                UI_nameOfHighlightedObject.GetComponent <Text>().text = "Combine with " + highlightedWorldObject.data.actorName;
            }
            else
            {
                UI_nameOfHighlightedObject.GetComponent <Text>().text = highlightedWorldObject.data.actorName;
            }
            UI_nameOfHighlightedObject.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y + 20, Input.mousePosition.z);
        }
    }
 // Start is called before the first frame update
 void Start()
 {
     objectScript = GetComponentInParent <InteractableObjectScript>();
     player       = GameObject.FindGameObjectWithTag("Player");
 }