Пример #1
0
    public IEnumerator MoveCoroutine(IWalker walker)
    {
        // the walker's position and the target position

        Vector3 startPos = walker.walkerGameObject.transform.position;
        Vector3 endPos   = Utilities.GetCharacterPosOnTile(walker, walker.walkerTargetPos);

        float distance = Vector3.Distance(startPos, endPos);

        float tempSpeed = walker.walkerSpeed / distance;

        // interpolation
        float inter = 0;

        // -- ANIMATIONS -- //

        // declarations

        Animator  myAnimator    = walker.walkerGameObject.GetComponent <Animator>();
        Direction lastDirection = Direction.left;

        // coords - for the animation

        Coords startCoords = new Coords(walker.speakerPos.x, walker.speakerPos.y);
        Coords endCoords   = new Coords(walker.walkerTargetPos.x, walker.walkerTargetPos.y);

        // Walk down

        if (startCoords.y > endCoords.y)
        {
            myAnimator.PlayInFixedTime("Walk_front");
            lastDirection = Direction.down;
        }

        // Walk up

        if (startCoords.y < endCoords.y)
        {
            myAnimator.PlayInFixedTime("Walk_back");
            lastDirection = Direction.up;
        }

        // Walk left

        if (startCoords.x > endCoords.x)
        {
            myAnimator.PlayInFixedTime("Walk_left");
            lastDirection = Direction.left;
        }

        // Walk right

        if (startCoords.x < endCoords.x)
        {
            myAnimator.PlayInFixedTime("Walk_right");
            lastDirection = Direction.right;
        }

        // while loop - updating the character object position

        while (startPos != endPos)
        {
            inter += tempSpeed * Time.deltaTime;

            if (inter >= 1)
            {
                walker.walkerGameObject.transform.position = startPos = endPos;
                break;
            }
            else
            {
                walker.walkerGameObject.transform.position = Vector3.Lerp(startPos, endPos, inter);
            }

            // changing the sorting layer
            walker.walkerGameObject.GetComponentInChildren <SpriteRenderer>().sortingOrder = Mathf.FloorToInt(-(walker.walkerGameObject.transform.position.y * 10) + 6f);

            yield return(new WaitForFixedUpdate());
        }

        // After while loop is done, change the character tile

        walker.ChangePos(walker.walkerTargetPos);

        switch (lastDirection)
        {
        case Direction.left:

            myAnimator.PlayInFixedTime("Idle_left");

            break;

        case Direction.right:

            myAnimator.PlayInFixedTime("Idle_right");

            break;

        case Direction.down:

            myAnimator.PlayInFixedTime("Idle_front");

            break;

        case Direction.up:

            myAnimator.PlayInFixedTime("Idle_back");

            break;
        }

        // Check if I need to continue walking

        if (walker.walkerPath != null)
        {
            if (walker.walkerPath.Count > 0)
            {
                walker.walkerTargetPos = walker.walkerPath.Dequeue();
                MoveToTargetTile(walker, walker.walkerTargetPos);
            }
            else
            {
                EventsHandler.Invoke_Callback(EventsHandler.cb_characterFinishedPath);
            }
        }
    }
Пример #2
0
    public IEnumerator MoveCoroutine(IWalker walker)
    {
        Vector3 startPos = walker.walkerGameObject.transform.position;
        Vector3 endPos   = Utilities.GetCharacterPosOnTile(walker, walker.walkerTargetPos);

        float distance = Vector3.Distance(startPos, endPos);

        Debug.Log("distance " + distance);

        float tempSpeed = walker.walkerSpeed / distance;

        Debug.Log("tempSpeed " + tempSpeed);

        // interpolation
        float inter = 0;


        // Animations

        Animator  myAnimator    = walker.walkerGameObject.GetComponent <Animator>();
        Direction lastDirection = Direction.left;


        // ANIMATIONS


        // Walk left

        if (walker.speakerPos.x > walker.walkerTargetPos.x)
        {
            myAnimator.PlayInFixedTime("Walk_left");
            lastDirection = Direction.left;
        }


        // Walk right

        if (walker.speakerPos.x < walker.walkerTargetPos.x)
        {
            myAnimator.PlayInFixedTime("Walk_right");
            lastDirection = Direction.right;
        }


        // Walk down

        if (walker.speakerPos.y > walker.walkerTargetPos.y)
        {
            myAnimator.PlayInFixedTime("Walk_front");
            lastDirection = Direction.down;
        }


        // Walk up

        if (walker.speakerPos.y < walker.walkerTargetPos.y)
        {
            myAnimator.PlayInFixedTime("Walk_back");
            lastDirection = Direction.up;
        }



        // while loop - updating the character object position

        while (startPos != endPos)
        {
            inter += tempSpeed * Time.deltaTime;

            if (inter >= 1)
            {
                Debug.Log("I arrived " + walker.speakerName);

                walker.walkerGameObject.transform.position = startPos = endPos;
                break;
            }
            else
            {
                walker.walkerGameObject.transform.position = Vector3.Lerp(startPos, endPos, inter);
            }

            yield return(new WaitForFixedUpdate());
        }


        // After while loop is done, change the character tile

        walker.ChangePos(walker.walkerTargetPos);


        switch (lastDirection)
        {
        case Direction.left:

            myAnimator.PlayInFixedTime("Idle_left");

            break;


        case Direction.right:

            myAnimator.PlayInFixedTime("Idle_right");

            break;


        case Direction.down:

            myAnimator.PlayInFixedTime("Idle_front");

            break;


        case Direction.up:

            myAnimator.PlayInFixedTime("Idle_back");

            break;
        }



        // Check if I need to continue walking

        if (walker.walkerPath != null)
        {
            if (walker.walkerPath.Count > 0)
            {
                walker.walkerTargetPos = walker.walkerPath.Dequeue();
                MoveToTargetTile(walker, walker.walkerTargetPos);
            }
            else
            {
                EventsHandler.Invoke_cb_characterFinishedPath();
            }
        }
    }