Пример #1
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (GameState.GamePaused)
            {
                PauseMenu.SetActive(false);
                GameState.GamePaused = false;
                return;
            }
            else
            {
                GameState.GamePaused = true;
                PauseMenu.SetActive(true);
                return;
            }
        }

        if (GameState.GamePaused)
        {
            return;
        }

        if (movePending != null)
        {
            return;
        }
        if (Weapon != null && Weapon.GetComponent <SwordBehaviour>().IsAttacking)
        {
            return;
        }

        Vector3 newPos = Vector3.zero;

        if (Input.GetKeyDown(KeyCode.Space) && Weapon != null)
        {
            Weapon.GetComponent <SwordBehaviour>().StartAttack(currentDirection);

            switch (currentDirection)
            {
            case Direction.Up:
                newPos = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
                break;

            case Direction.Left:
                newPos = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
                break;

            case Direction.Down:
                newPos = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
                break;

            case Direction.Right:
                newPos = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
                break;

            default:
                newPos = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
                break;
            }
            GameWorld.CanEnterTile(newPos.x, newPos.y, true);
        }

        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (currentDirection != Direction.Up)
            {
                currentDirection = Direction.Up;
                Weapon?.GetComponent <SwordBehaviour>().SetSwordDirection(currentDirection, transform.position);
                spriteRenderer.sprite = GetNextSprite(Direction.Up);
            }
        }
        else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (currentDirection != Direction.Left)
            {
                currentDirection = Direction.Left;
                Weapon?.GetComponent <SwordBehaviour>().SetSwordDirection(currentDirection, transform.position);
                spriteRenderer.sprite = GetNextSprite(Direction.Left);
            }
        }
        else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (currentDirection != Direction.Down)
            {
                currentDirection = Direction.Down;
                Weapon?.GetComponent <SwordBehaviour>().SetSwordDirection(currentDirection, transform.position);
                spriteRenderer.sprite = GetNextSprite(Direction.Down);
            }
        }
        else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (currentDirection != Direction.Right)
            {
                currentDirection = Direction.Right;
                Weapon?.GetComponent <SwordBehaviour>().SetSwordDirection(currentDirection, transform.position);
                spriteRenderer.sprite = GetNextSprite(Direction.Right);
            }
        }

        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            if (moveDelay > 0)
            {
                moveDelay -= (1 * Time.deltaTime); return;
            }

            newPos = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);

            isMoving = true;

            if (GameWorld.CanEnterTile(newPos.x, newPos.y))
            {
                var newRoomName = GameWorld.GetRoomAtPosition(newPos).Name;

                if (!newRoomName.Equals(currentRoomName))
                {
                    Camera.GetComponent <CameraController>().MoveCamera(Direction.Up);
                    movePending     = Direction.Up;
                    currentRoomName = newRoomName;
                    return;
                }

                transform.position = newPos;
            }

            spriteRenderer.sprite = GetNextSprite(Direction.Up);
            isMoving  = false;
            moveDelay = MovementSpeed;
        }
        else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            if (moveDelay > 0)
            {
                moveDelay -= (1 * Time.deltaTime); return;
            }

            newPos = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);

            isMoving = true;

            if (GameWorld.CanEnterTile(newPos.x, newPos.y))
            {
                var newRoomName = GameWorld.GetRoomAtPosition(newPos).Name;

                if (!newRoomName.Equals(currentRoomName))
                {
                    Camera.GetComponent <CameraController>().MoveCamera(Direction.Left);
                    movePending     = Direction.Left;
                    currentRoomName = newRoomName;
                    return;
                }

                transform.position = newPos;
            }

            spriteRenderer.sprite = GetNextSprite(Direction.Left);
            isMoving  = false;
            moveDelay = MovementSpeed;
        }
        else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            if (moveDelay > 0)
            {
                moveDelay -= (1 * Time.deltaTime); return;
            }

            newPos = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);

            isMoving = true;

            if (GameWorld.CanEnterTile(newPos.x, newPos.y))
            {
                var newRoomName = GameWorld.GetRoomAtPosition(newPos).Name;

                if (!newRoomName.Equals(currentRoomName))
                {
                    Camera.GetComponent <CameraController>().MoveCamera(Direction.Down);
                    movePending     = Direction.Down;
                    currentRoomName = newRoomName;
                    return;
                }

                transform.position = newPos;
            }

            spriteRenderer.sprite = GetNextSprite(Direction.Down);
            isMoving  = false;
            moveDelay = MovementSpeed;
        }
        else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            if (moveDelay > 0)
            {
                moveDelay -= (1 * Time.deltaTime); return;
            }

            newPos = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);

            isMoving = true;

            if (GameWorld.CanEnterTile(newPos.x, newPos.y))
            {
                var newRoomName = GameWorld.GetRoomAtPosition(newPos).Name;

                if (!newRoomName.Equals(currentRoomName))
                {
                    Camera.GetComponent <CameraController>().MoveCamera(Direction.Right);
                    movePending     = Direction.Right;
                    currentRoomName = newRoomName;
                    return;
                }

                transform.position = newPos;
            }

            spriteRenderer.sprite = GetNextSprite(Direction.Right);
            isMoving  = false;
            moveDelay = MovementSpeed;
        }

        if (Input.GetKeyUp(KeyCode.W) ||
            Input.GetKeyUp(KeyCode.UpArrow) ||
            Input.GetKeyUp(KeyCode.A) ||
            Input.GetKeyUp(KeyCode.LeftArrow) ||
            Input.GetKeyUp(KeyCode.S) ||
            Input.GetKeyUp(KeyCode.DownArrow) ||
            Input.GetKeyUp(KeyCode.D) ||
            Input.GetKeyUp(KeyCode.RightArrow))
        {
            moveDelay = MovementSpeed;
        }

        if (Application.isEditor)
        {
            // Debug Keys
            if (Input.GetKeyDown(KeyCode.R))
            {
                newPos = new Vector3(25, 2, transform.position.z);
                var newCameraPos = new Vector3(25.5f, 5.5f, Camera.transform.position.z);
                Teleport(newPos, newCameraPos);
            }
            else if (Input.GetKeyDown(KeyCode.N))
            {
                AddKey(false);
            }
            else if (Input.GetKeyDown(KeyCode.M))
            {
                AddKey(true);
            }
        }
    }