/**
     * Note: This current implentation creates goofy situations with carryables.
     *       If the character is standing near an interactable, and a carryable
     *       if the carryable interaction executes first, the carryable will get
     *       picked up, and the other object will not be interacted with. If the
     *       interactable interaction executes first, that interaction will occur
     *       AND the carryable will be picked up.
     */
    public void Interact()
    {
        if (character.isAlive)
        {
            bool interacted = false;

            // Using index iteration because the interactable might be destroyed
            // through the interaction. Iterating in reverse in case an element
            // is removed.
            for (int i = interactables.Count - 1; i >= 0; i--)
            {
                InteractableBehavior interactable = interactables[i];
                interactable.Interact(gameObject, interaction);
                interacted = true;
            }

            // There is no way to know that an interaction really occurred, so
            // we just need to test if there are any interactables around.
            if (!interacted)
            {
                Carrier carrier = character.GetComponent <Carrier>();
                if (carrier != null && carrier.heldObject != null)
                {
                    carrier.Drop();
                }
            }
            character.movementState = Character.MovementState.IDLE;
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        InteractableBehavior interactable = other.gameObject.GetComponent <InteractableBehavior>();

        if (interactable != null && !interactables.Contains(interactable))
        {
            interactables.Add(interactable);
        }
    }
    private void OnTriggerExit2D(Collider2D other)
    {
        InteractableBehavior interactable = other.gameObject.GetComponent <InteractableBehavior>();

        if (interactable != null)
        {
            interactables.Remove(interactable);
        }
    }
 // Update is called once per frame
 void Update()
 {
     for (int i = interactables.Count - 1; i >= 0; i--)
     {
         InteractableBehavior interactable = interactables[i];
         if (!interactable)
         {
             interactables.RemoveAt(i);
         }
     }
 }
Example #5
0
    //turn this into an ienumerator so you cant call interaction button again until it returns an action
    //OUTSOURCE ALL INTERACTABLE LOGIC TO INTERACTABLE BEHAVIOR OOOOOH MY GOOOOOOOOOD
    //USE A CALLBACK FROM INTERACTABLE LOGIC TO TERMINATE THIS IENUMERATOR
    void ActionButton()
    {
        Debug.Log("action button was pressed");
        //this is the interaction handler for using keys and utilities
        RaycastHit hit;
        bool       interactable = false;

        //check that ur actually hitting something, then check if the item is interactable
        Debug.Log(gameObject.transform.rotation.eulerAngles.ToString());
        if (Physics.Raycast(gameObject.transform.position, gameObject.transform.forward, out hit, 5.0f))
        {
            if (hit.collider.tag == "Interactable")
            {
                interactable = true;
                Debug.Log("target is in fact interactable");
            }
            //just do this, to prevent stacking tons of ifs and a for loop.
        }
        //here we do stuff if the item is interactable.

        //LOOK AT THIS F*****G MONSTER NOOOOO WAY
        //SORT THIS BASTARD OF A SYSTEM OUT LATER I GOTTA GET IT DONE
        if (interactable)
        {
            InteractableBehavior benis = hit.collider.gameObject.GetComponent <InteractableBehavior>();
            Debug.Log(benis.key);
            if (benis.locked)
            {
                //ok make sure ur not trying to open an already opened door
                //ok make sure ur not checking the key for a door that duznt require one
                if (benis.key == "")
                {
                    Debug.Log("target was not locked with a key. opening");
                    benis.locked = false;
                }
                else
                {
                    //ok check if the items in ur inv and consoom it if it duz be
                    foreach (string e in inventory)
                    {
                        if (e == benis.key)
                        {
                            benis.locked = false;
                            inventory.Remove(e);
                            Debug.Log("target was locked with a key. " + e + "has been consoomed");
                            break;
                        }
                    }
                }
            }
        }
    }