예제 #1
0
    void Start()
    {
        keyLockRenderer.sprite = lockColor switch
        {
            KeyColors.Golden => golden,
            KeyColors.Blue => blue,
            KeyColors.Green => green,
            KeyColors.Red => red,
            KeyColors.Cyan => cyan,
            KeyColors.Brown => brown,
            _ => throw new ArgumentOutOfRangeException()
        };
        openDrawerSprite.SetActive(false);
    }

    void Awake()
    {
        source = GetComponent <AudioSource>();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        canInteract = true;

        if (LayerMask.LayerToName(other.gameObject.layer) == "Player")
        {
            inventary = other.gameObject.GetComponent <CharacterInventary>();
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        canInteract = false;
        inventary   = null;
    }

    void Update()
    {
        if (!Input.GetKeyDown(KeyCode.Q) && !Input.GetKeyDown(KeyCode.Space))
        {
            return;
        }

        if (!canInteract || isOpen)
        {
            return;
        }
        if (inventary == null || !inventary.HasKey(lockColor))
        {
            source.PlayOneShot(lockedSound);
            return;
        }


        IEnumerator wait()
        {
            source.PlayOneShot(unlockSound);
            yield return(new WaitUntil(() => !source.isPlaying));

            source.PlayOneShot(dropKeySound);
            openDrawerSprite.SetActive(true);

            Instantiate(keyItemPrefab, transform.position, Quaternion.identity).GetComponent <KeyData>()
            .SetKeyColor(keyDropColor);
            yield return(new WaitUntil(() => !source.isPlaying));

            keyLockRenderer.enabled = false;
            openDrawerSprite.SetActive(true);
        }

        isOpen = true;
        StartCoroutine(wait());
    }
}
예제 #2
0
        void Start()
        {
            keyLockRenderer.sprite = keyName switch
            {
                KeyColors.Golden => golden,
                KeyColors.Blue => blue,
                KeyColors.Green => green,
                KeyColors.Red => red,
                KeyColors.Cyan => cyan,
                KeyColors.Brown => brown,
                _ => throw new ArgumentOutOfRangeException()
            };

            if (needsKey)
            {
                LockDoorKey();
            }

            inventary = FindObjectOfType <CharacterInventary>();
        }

        void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.gameObject == door)
            {
                return;
            }
            hasSomeoneClose = true;


            if (LayerMask.LayerToName(collision.gameObject.layer) == "Enemy")
            {
                enemyPlayLockSound = true;
            }

            if (LayerMask.LayerToName(collision.gameObject.layer) == "Player")
            {
                canInteract = true;
            }
        }

        void OnTriggerStay2D(Collider2D other)
        {
            if (other.gameObject == door)
            {
                return;
            }
            hasSomeoneClose = true;
            if (LayerMask.LayerToName(other.gameObject.layer) == "Enemy")
            {
                if (CurrentState == State.Closed)
                {
                    other.gameObject.GetComponent <EnemyFsm>().ResetAfterCloseDoorCoolDown();
                }

                OpenDoor(enemyPlayLockSound);
                enemyPlayLockSound = false;
                if (CurrentState == State.Locked)
                {
                    ConfirmLockDoor();
                }
            }
        }

        void OnTriggerExit2D(Collider2D other)
        {
            if (other.gameObject == door)
            {
                return;
            }
            hasSomeoneClose = false;

            if (LayerMask.LayerToName(other.gameObject.layer) == "Player")
            {
                canInteract = false;
            }
            Invoke(nameof(AutoCloseDoor), timeToAutoClose);
        }

        bool IsDoorLocked() => CurrentState == State.Locked || CurrentState == State.ConfirmedLock;

        void AutoCloseDoor()
        {
            if (!hasSomeoneClose)
            {
                CloseDoor();
            }
        }

        void CloseDoor()
        {
            if (CurrentState == State.Closed || IsDoorLocked())
            {
                return;
            }
            AnimCloseDoor();

            IEnumerator wait()
            {
                yield return(new WaitForSeconds(0.2f));

                audioSource.PlayOneShot(close);
            }

            StartCoroutine(wait());
            CurrentState = State.Closed;
        }

        void OpenDoor(bool playLockedSound = true)
        {
            if (CurrentState == State.Open)
            {
                return;
            }

            if (IsDoorLocked())
            {
                if (needsKey && !inventary.HasKey(keyName) && canInteract)
                {
                    ThoughtBubble.Instance.ShowThought(dontHaveKeyThought);
                }

                if (playLockedSound)
                {
                    audioSource.PlayOneShot(locked);
                }
                return;
            }

            AnimOpenDoor();
            audioSource.PlayOneShot(open);
            door.gameObject.layer = playerObstableLayer;
            CurrentState          = State.Open;
        }

        void LockDoor()
        {
            if (CurrentState == State.Locked)
            {
                return;
            }


            if (!inventary.HasLocks())
            {
                ThoughtBubble.Instance.ShowThought(dontHaveLocksThought);

                if (!audioSource.isPlaying)
                {
                    audioSource.PlayOneShot(needLockPad);
                }

                return;
            }

            lockSimbol.SetActive(true);
            inventary.UseLock();
            hasLockIn = true;
            audioSource.PlayOneShot(lockDoor);

            AnimCloseDoor();
            CurrentState = State.Locked;
        }

        void LockDoorKey()
        {
            if (CurrentState == State.Locked)
            {
                return;
            }
            keyLockRenderer.enabled = true;
            AnimCloseDoor();
            hasLockIn    = false;
            CurrentState = State.Locked;
            ConfirmLockDoor();
        }

        void ConfirmLockDoor()
        {
            if (CurrentState == State.ConfirmedLock)
            {
                return;
            }
            door.gameObject.layer = obstableLayer;
            confirmLockedWhen     = Time.time;
            UpdatePath();
            CurrentState = State.ConfirmedLock;
        }

        void UnconfirmLockDoor()
        {
            if (CurrentState != State.ConfirmedLock)
            {
                return;
            }
            door.gameObject.layer = playerObstableLayer;
            confirmLockedWhen     = 0;
            UpdatePath();
            CurrentState = State.Locked;
        }

        void UnlockDoor()
        {
            if (!IsDoorLocked())
            {
                return;
            }

            if (needsKey)
            {
                if (!inventary.HasKey(keyName))
                {
                    ThoughtBubble.Instance.ShowThought(dontHaveKeyThought);
                    audioSource.PlayOneShot(locked);
                    return;
                }

                inventary.AddLock();
                needsKey = false;
            }

            AnimCloseDoor();
            if (hasLockIn)
            {
                inventary.AddLock();
            }
            lockSimbol.SetActive(false);
            keyLockRenderer.enabled = false;
            door.gameObject.layer   = playerObstableLayer;
            audioSource.PlayOneShot(lockDoor);
            UpdatePath();
            CurrentState = State.Closed;
        }

        void UpdatePath() => //AstarPath.active.Scan();
        AstarPath.active.UpdateGraphs(GetComponent <BoxCollider2D>().bounds);

        void Update()
        {
            HandleInput();

            if (CurrentState == State.ConfirmedLock &&
                confirmLockedWhen > 0 &&
                (Time.time - confirmLockedWhen) > timeToUncorfimLock)
            {
                UnconfirmLockDoor();
            }


            audioSource.volume = Vector2.Distance(
                transform.position,
                inventary.transform.position) switch
            {
                var dist when dist <= 15 => 1f,
                var dist when dist <= 30 => .6f,
                var dist when dist <= 50 => .4f,
                              _ => .2f,
            };
        }

        void HandleInput()
        {
            if (!canInteract)
            {
                return;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (CurrentState != State.Open)
                {
                    OpenDoor();
                }
                else
                {
                    CloseDoor();
                }
            }

            if (Input.GetKeyDown(KeyCode.Q))
            {
                if (CurrentState == State.Closed)
                {
                    LockDoor();
                }
                else if (CurrentState == State.Open)
                {
                    ThoughtBubble.Instance.ShowThought("I can lock only closed doors");
                }
                else if (IsDoorLocked())
                {
                    UnlockDoor();
                }
            }
        }