Beispiel #1
0
    /// <summary>
    /// Move the player in the given direction
    ///
    /// Set the starting point, and target position
    /// Move in the while at the givent speed
    /// Set the target position to current position
    /// </summary>
    /// <param name="targetPos"></param>
    IEnumerator MoveAnimation(Vector2 targetPos)
    {
        Vector2 startPos = transform.position;
        float   timer    = 0;

        triangleCreator.TransformTriangle(MovementSpeed / 2);
        while (timer < MovementSpeed)
        {
            transform.position = Vector2.Lerp(startPos, targetPos, (timer / MovementSpeed));
            timer += Time.deltaTime;
            yield return(null);
        }
        triangleCreator.TransformSquare(MovementSpeed / 2);
        currentGridIndex   = targetGridIndex;
        transform.position = movementGrid.GetPosition((int)targetGridIndex.x, (int)targetGridIndex.y);
    }
Beispiel #2
0
    /// <summary>
    /// If it s the first move play triangle animation.
    /// Else search possible position on the grid
    ///  and get killed if no more movement are available
    /// </summary>
    void NextMove()
    {
        if (!hasEntered)
        {
            triangleCreator.TransformTriangle(MovementSpeed);
            hasEntered = true;
            return;
        }

        float nextPosX = currentGridIndex.x - direction.x;
        float nextPosY = currentGridIndex.y - direction.y;

        if (movementGrid.IndexExist((int)nextPosX, (int)nextPosY))
        {
            targetGridIndex = movementGrid.ClampIndexOnGrid((int)nextPosX, (int)nextPosY);
        }
        else
        {
            eManager.Killed(this);
            GameObject.Destroy(gameObject);
        }

        StartCoroutine(MoveAnimation(movementGrid.GetPosition(targetGridIndex.x, targetGridIndex.y)));
    }