Beispiel #1
0
    private void PlayerInteraction()
    {
        RaycastHit hit;

        if (Physics.SphereCast(CameraContainer.transform.position, Radius, CameraContainer.transform.forward, out hit, Distance, InteractionLayer, QueryTriggerInteraction.Collide))
        {
            IInteractable interactable = hit.transform.root.GetComponent <IInteractable>();

            if (interactable.CanInteract(this))
            {
                PlayerInteractable?.Invoke(interactable.GetUIMessage(this));
                if (Input.GetKeyDown(KeyCode.F))
                {
                    interactable.Interact(this);
                }
            }
            else
            {
                PlayerInteractable?.Invoke(null);
            }
        }
        else
        {
            PlayerInteractable?.Invoke(null);
        }
    }
Beispiel #2
0
    private void DetectNearbyInteractables()
    {
        IInteractable nearestInteractable = GetNearestInteractable();

        ResetHighlight();
        nearbyInteractables.Clear();

        Vector2 overlapPos = (Vector2)transform.position;

        Collider2D[] colliders = Physics2D.OverlapCircleAll(overlapPos, checkRadius);

        if (colliders == null)
        {
            return;
        }
        foreach (Collider2D collider in colliders)
        {
            IInteractable interactable = collider.gameObject.GetComponent <IInteractable>();
            if (interactable != null)
            {
                if (interactable.CanInteract(overlapPos))
                {
                    nearbyInteractables.Add(interactable);
                }
            }
        }

        nearbyInteractables.Sort((a, b) => CompareInteractableDistances(a, b));
        SetHighlightForNearestInteractable();
    }
Beispiel #3
0
 /// <summary>
 /// Can the ability be started?
 /// </summary>
 /// <returns>True if the ability can be started.</returns>
 public override bool CanStartAbility()
 {
     if (!m_Controller.Aiming && m_Interactable != null && m_Interactable.CanInteract())
     {
         return(true);
     }
     return(false);
 }
Beispiel #4
0
 void handleInteractionKey(IInteractable i)
 {
     if (i != null && i.CanInteract(this))
     {
         if (Input.GetKeyDown(InteractionKey))
         {
             i.Interact(this);
         }
     }
 }
Beispiel #5
0
 private void Update()
 {
     if (_currentInteractable != null)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             if (_currentInteractable.CanInteract())
             {
                 _currentInteractable.Interact();
             }
         }
     }
 }
Beispiel #6
0
 private void OnInteractableOverlap(IInteractable interactable, bool exit)
 {
     if (!exit)
     {
         if (interactable.CanInteract())
         {
             InteractionText.text = interactable.GetInteractableAction() + " '" + interactable.GetName() + "'";
             InteractionText.gameObject.SetActive(true);
         }
     }
     else
     {
         InteractionText.text = string.Empty;
         InteractionText.gameObject.SetActive(false);
     }
 }
Beispiel #7
0
    void Interacting()
    {
        hitColliders = Physics.OverlapSphere(transform.position, 1f);

        if (hitColliders != null)
        {
            foreach (Collider c in hitColliders)
            {
                IInteractable i = c.GetComponent <IInteractable>();
                if (i != null && i.CanInteract())
                {
                    i.Interact(gameObject);
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && !SteamPunkConsole.IsConsoleOpen &&
            !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Vector3 targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            // if clicked on a thing
            // do, interact sequence

            Collider2D hitCollider = Physics2D.OverlapPoint(targetPosition, LayerMask.GetMask("Interactable"));
            if (hitCollider)
            {
                // determine where to walk to interact. (interaction distance stored in target?)
                // update the walk sequence (abort it if its already running) this is the only time we need to 'cancel' a sequence.
                // when we've arrived, call 'DoInteract' on target.
                interactionTarget = hitCollider.GetComponent <IInteractable>();
                if (interactionTarget != null)
                {
                    if (interactionTarget.CanInteract(controlledActor))
                    {
                        if (sequenceInstance != null)
                        {
                            GameInstance.Singleton.SequenceManager.AbortSequence(sequenceInstance);
                        }

                        walkSequence.actionStates[0].Set("Position Target", (Vector2)interactionTarget.GetInteractionPosition(controlledActor));
                        sequenceInstance = GameInstance.Singleton.SequenceManager.ActivateSequence(walkSequence);
                    }
                }
            }
            else
            {
                interactionTarget = null;
                targetPosition.z  = 0;

                walkSequence.actionStates[0].Set("Position Target", (Vector2)targetPosition);
                if (sequenceInstance != null)
                {
                    GameInstance.Singleton.SequenceManager.AbortSequence(sequenceInstance);
                }

                sequenceInstance = GameInstance.Singleton.SequenceManager.ActivateSequence(walkSequence);
            }
        }
    }
Beispiel #9
0
    void CheckForInteractables()
    {
        availableInteractables.Clear();

        Collider[] interactables = Physics.OverlapSphere(
            transform.position,
            InteractRange,
            1 << 9);
        foreach (Collider col in interactables)
        {
            IInteractable interactable = col.GetComponent <IInteractable>();
            if (interactable.CanInteract())
            {
                availableInteractables.Add(interactable);
            }
        }
    }
Beispiel #10
0
    public IInteractable GetInteractableById(string id)
    {
        Vector2 overlapPos = (Vector2)transform.position;

        Collider2D[] colliders = Physics2D.OverlapCircleAll(overlapPos, range);

        foreach (Collider2D collider in colliders)
        {
            IInteractable interactable = collider.gameObject.GetComponent <IInteractable>();
            if (interactable != null)
            {
                if (interactable.CanInteract(enemy.transform.position) && interactable.GetID() == id)
                {
                    return(interactable);
                }
            }
        }
        return(null);
    }
    private void OnTriggerEnter(Collider other)
    {
        if (IsInteracting)
        {
            return;
        }
        IInteractable interactable = other.GetComponent <IInteractable>();

        if (interactable == null)
        {
            return;
        }
        if (currentInteractable == interactable)
        {
            return;
        }
        if (!interactable.CanInteract())
        {
            return;
        }

        string notificationText = interactable.InteractionNotification();

        if (notificationText == null)
        {
            currentInteractable = null;
            IsInteracting       = true;
            interactable.Interact(() => {
                IsInteracting = false;
            });
        }
        else
        {
            NotificationScreen.Instance.Show(other.transform.position, notificationText);
            currentInteractable = interactable;
        }
    }
Beispiel #12
0
 void UpdatePickAxe(IInteractable i)
 {
     if (i != null && i.CanInteract(this))
     {
         if (i is ResourceObject)
         {
             if (!PickCooldown.IsRunning)
             {
                 if (BattlePlayerInput.instance.GetAxis("Fire1") > 0)
                 {
                     StartCoroutine(PickCooldown.Run());
                     i.Interact(this);
                 }
             }
         }
         else
         {
             if (Input.GetKeyDown(InteractionKey))
             {
                 i.Interact(this);
             }
         }
     }
 }
Beispiel #13
0
        public override void Interact()
        {
            TriggerController selfTriggerController  = gameObject.GetChild("InteractiveCube").GetComponent <TriggerController>();
            TriggerController otherTriggerController = selfTriggerController.GetFirstKeepingTriggerController();

            if (selfTriggerController != null && otherTriggerController != null && otherTriggerController.ITriggerDelegate != null)
            {
                GameObject interactObject = otherTriggerController.ITriggerDelegate.GetGameObject();

                if (interactObject.tag == "NPC")
                {
                    RolePromptController rolePromptController = GetComponentInChildren <RolePromptController>();
                    rolePromptController.killtalk();
                    OnInteractAction(interactObject);
                    AnimManager.Instance.PrintBubble("[color=#FFFFFF,#000000]fgtdfg[/color]", interactObject);
                }
                else if (interactObject.tag == "Ladder")
                {
                    LadderController ladderController = interactObject.GetComponent <LadderController>();
                    BoxCollider      boxCollider      = otherTriggerController.gameObject.GetComponent <BoxCollider>();
                    Bounds           bou = boxCollider.bounds;
                    gameObject.layer = LayerMask.NameToLayer("Default");
                    if ((interactObject.transform.position.y + bou.center.y + ((bou.size.y / 2) - 1.6f)) < transform.position.y)
                    {
                        switch (ladderController.laddertype)
                        {
                        case laddertype.Left:
                            transform.position = new Vector3(interactObject.transform.position.x + CharacterController.radius, transform.position.y - 1.6f, interactObject.transform.position.z);
                            break;

                        case laddertype.Right:
                            transform.position = new Vector3(interactObject.transform.position.x - CharacterController.radius, transform.position.y - 1.6f, interactObject.transform.position.z);
                            break;

                        case laddertype.Center:
                            transform.position = new Vector3(interactObject.transform.position.x, transform.position.y - 1.6f, interactObject.transform.position.z - 0.1f);
                            break;
                        }
                    }
                    else
                    {
                        switch (ladderController.laddertype)
                        {
                        case laddertype.Left:
                            transform.position = new Vector3(interactObject.transform.position.x + CharacterController.radius, transform.position.y, interactObject.transform.position.z);
                            break;

                        case laddertype.Right:
                            transform.position = new Vector3(interactObject.transform.position.x - CharacterController.radius, transform.position.y, interactObject.transform.position.z);
                            break;

                        case laddertype.Center:
                            transform.position = new Vector3(interactObject.transform.position.x, transform.position.y, interactObject.transform.position.z - 0.1f);
                            break;
                        }
                    }
                    if (animatorParams.ContainsKey("ladder_type"))
                    {
                        _animator.SetInteger("ladder_type", (int)ladderController.laddertype);
                    }
                    if (animatorParams.ContainsKey("start_climb_ladder"))
                    {
                        _animator.SetBool("start_climb_ladder", true);
                    }
                }
                else if (interactObject.GetComponent <IInteractable>() != null)
                {
                    IInteractable interactable = interactObject.GetComponent <IInteractable>();
                    if (interactable.CanInteract())
                    {
                        interactable.Interact();
                    }
                }
                else if (interactObject.GetComponent <PortalController>())
                {
                    PortalController portalController = interactObject.GetComponent <PortalController>();
                    if (portalController)
                    {
                        this.OpenPortal(portalController);
                    }
                }
                else if (interactObject.GetComponent <FenceDoorController>())
                {
                    FenceDoorController fenceDoorController = interactObject.GetComponent <FenceDoorController>();
                    if (fenceDoorController)
                    {
                        fenceDoorController.OpenPortal();
                    }
                }
                else
                {
                    if (interactObject != null && interactObject.Equals(null) == false)
                    {
                        DropItemController dropItemController = interactObject.GetComponent <DropItemController>();
                        if (dropItemController != null)
                        {
                            var itemData = ItemManager.Instance.getItemDataById <ItemData>(dropItemController.ItemId);
                            GameManager.Instance.PickupTips("Textures/Icon/" + itemData.icon, itemData.name, itemData.count.ToString());
                            switch (itemData.itemType)
                            {
                            case ItemType.Consumable:
                                Debug.Log("捡到消耗品 id = " + itemData.id);

                                Destroy(interactObject);
                                ConsumableData consumableData = ItemManager.Instance.getItemDataById <ConsumableData>(itemData.id);
                                if (consumableData != null)
                                {
                                    switch (consumableData.consumableUseType)
                                    {
                                    case ConsumableUseType.deposit:
                                        RoleData.AddConsumableItem(itemData.id, itemData.count);
                                        PickupConsumable();
                                        break;

                                    case ConsumableUseType.ImmediateUse:
                                        BuffController.AddBuff(consumableData.buffId);
                                        break;
                                    }
                                }
                                break;

                            case ItemType.Other:
                                RoleData.OtherItemList.Add(itemData);
                                dropItemController.RemoveSelf();
                                break;

                            case ItemType.Equip:
                                var weaponData = ItemManager.Instance.getItemDataById <WeaponData>(dropItemController.ItemId);
                                if (weaponData != null)
                                {
                                    this.JuageEquipPossessType(weaponData);
                                }
                                else
                                {
                                    var equipData = ItemManager.Instance.getItemDataById <EquipData>(itemData.id);
                                    RoleData.EquipData.UseEquip(equipData, interactObject.transform.localPosition + new Vector3(Random.Range(-0.5f, 0.5f), Random.Range(1.0f, 1.5f), Random.Range(-0.5f, 0.5f)));
                                }
                                Destroy(interactObject);
                                break;
                            }
                        }
                        else
                        {
                            var interactive = interactObject.GetComponent <ITriggerDelegate>();
                            if (interactive != null)
                            {
                                Event evt = new Event();
                                evt.Type = EventType.Interact;
                                evt.Data = GetGameObject();
                                interactive.OnEvent(evt);
                            }
                        }
                    }
                }
            }
        }
Beispiel #14
0
    void PhysicsCheck()
    {
        isOnGround        = false;
        isAboveLadder     = false;
        isInFrontOfLadder = false;
        isInFrontOfGround = false;
        transform.parent  = null;

        // Cast rays for the left and right foot
        float        directedFootOffset = footOffset * direction;
        RaycastHit2D leftCheck          = Raycast(new Vector2(-footDistance + directedFootOffset, 0f), Vector2.down, groundDistance);
        RaycastHit2D rightCheck         = Raycast(new Vector2(footDistance + directedFootOffset, 0f), Vector2.down, groundDistance);
        RaycastHit2D middleCheck        = Raycast(new Vector2(directedFootOffset, 0f), Vector2.down, groundDistance);

        // If either ray hit the ground, the player is on the ground
        if (leftCheck || middleCheck || rightCheck)
        {
            if (leftCheck.collider?.tag == movingPlatformTag)
            {
                transform.parent = leftCheck.transform;
            }
            if (rightCheck.collider?.tag == movingPlatformTag)
            {
                transform.parent = rightCheck.transform;
            }
            if (middleCheck.collider?.tag == movingPlatformTag)
            {
                transform.parent = middleCheck.transform;
            }
            isOnGround = true;
        }

        // Cast an horizontal ray for ladder check
        RaycastHit2D ladderCheck = Raycast(new Vector2(footDistance + directedFootOffset, -groundDistance), Vector2.left, footDistance * 2, ladderLayer);

        if (ladderCheck)
        {
            isAboveLadder = true;
        }

        // Check if is colliding with a ladder
        isInFrontOfLadder = boxCollider.IsTouchingLayers(ladderLayer);

        RaycastHit2D[] groundFrontCheck = BoxCastAll(new Vector2(transform.position.x, transform.position.y) + boxCollider.offset, boxCollider.size, groundLayer);
        if (groundFrontCheck.Length > 0)
        {
            isInFrontOfGround = true;
        }

        // Check for interactables in range and get the closest
        RaycastHit2D[] interactableHits = BoxCastAll(new Vector2(transform.position.x, transform.position.y) + boxCollider.offset, boxCollider.size, interactableLayer);
        if (interactableHits.Length > 0)
        {
            List <GameObject> interactables = new List <GameObject>();
            foreach (var hit in interactableHits)
            {
                IInteractable interactableHit = hit.collider.GetComponent <IInteractable>();
                if (interactableHit != null && interactableHit.CanInteract(gameObject))
                {
                    interactables.Add(hit.collider.gameObject);
                }
            }
            interactable = GetClosestGameObject(interactables.ToArray())?.GetComponent <IInteractable>();
        }
        else
        {
            interactable = null;
        }
    }