Exemplo n.º 1
0
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.magenta;
        var pos = transform.position;

        pos += BlockPuzzleManager.DirectionToVector(mPlayerDirection) * 2f;
        Gizmos.DrawSphere(pos, .5f);
    }
Exemplo n.º 2
0
    protected virtual void Update()
    {
        mDir = BlockDirection.Null;

        if (Input.GetButtonDown("Interact"))
        {
            var player = FindObjectOfType <Player>();
            if (player == null)
            {
                return;
            }

            if (mPlayerAttached)
            {
                mQueueDettach = true;
            }
            else
            {
                mPlayerDirection = GetPlayerDirection();
                if ((mPlayerDirection == BlockDirection.Left || mPlayerDirection == BlockDirection.Right) && Mathf.Abs(transform.position.x - player.transform.position.x) <= InteractDistance)
                {
                    mPlayerAttached = true;
                }
                if ((mPlayerDirection == BlockDirection.Backward || mPlayerDirection == BlockDirection.Forward) && Mathf.Abs(transform.position.z - player.transform.position.z) <= InteractDistance)
                {
                    mPlayerAttached = true;
                }

                if (mPlayerAttached)
                {
                    player.SetIgnoreInput(true);
                }
            }
        }

        if (mPlayerAttached && !mQueueDettach)
        {
            var xInput = Input.GetAxis("Horizontal");
            var yInput = Input.GetAxis("Vertical");

            if (mPlayerDirection == BlockDirection.Left || mPlayerDirection == BlockDirection.Right)
            {
                if (yInput > 0.1f)
                {
                    mDir = BlockDirection.Right;
                }
                if (yInput < -0.1f)
                {
                    mDir = BlockDirection.Left;
                }
            }

            else if (mPlayerDirection == BlockDirection.Backward || mPlayerDirection == BlockDirection.Forward)
            {
                if (xInput < -0.1f)
                {
                    mDir = BlockDirection.Forward;
                }
                if (xInput > 0.1f)
                {
                    mDir = BlockDirection.Backward;
                }
            }

            if (mDir != BlockDirection.Null)
            {
                BlockPuzzleTileInfo tile;
                if (BlockGrid.BlockMoveIsValid(transform.position, mDir, out tile))
                {
                    tile.Pos.y = transform.position.y;
                    mTargetPos = tile.Pos;
                }
            }
        }

        MoveToTargetPos();

        if (mPlayerAttached && FindObjectOfType <Player>())
        {
            var player = FindObjectOfType <Player>();

            // Dettaching tia if either Tia or the block start to fall away from the other.
            if (GetComponent <Rigidbody>())
            {
                if (Mathf.Abs(GetComponent <Rigidbody>().velocity.y) > 0.25f)
                {
                    mQueueDettach = true;
                }
            }
            if (Mathf.Abs(player.GetComponent <Rigidbody>().velocity.y) > 0.25f)
            {
                mQueueDettach = true;
            }

            // Update player anim.
            float animSpeed = mCurrentVelocity.magnitude * Mathf.Sign(Vector3.Dot(transform.position - player.transform.position, mCurrentVelocity));
            player.SetPushPullSpeed(animSpeed);

            // Update the player position.
            var tiaPushPos = transform.position;
            tiaPushPos.y = player.transform.position.y;
            tiaPushPos  += BlockPuzzleManager.DirectionToVector(mPlayerDirection) * 2f;
            player.transform.position = Vector3.Slerp(player.transform.position, tiaPushPos, Time.deltaTime * 5f);

            // Updating the player rotation.
            player.transform.parent = transform;
            var tiaLookPos = transform.position;
            tiaLookPos.y = player.transform.position.y;
            player.transform.LookAt(tiaLookPos);

            // Dettaching the player if required.
            if (mCurrentVelocity.magnitude <= 0f)
            {
                if (mQueueDettach)
                {
                    mPlayerAttached         = false;
                    player.transform.parent = null;
                    player.SetIgnoreInput(false);
                    mQueueDettach = false;
                }
            }
        }

        if (mCurrentVelocity.magnitude <= 0f && mQueueDestroy)
        {
            Destroy(this);
        }
    }