示例#1
0
    public IEnumerator Play(Vector3Int cellStart, Vector3Int cellEnd, DamageTrajectory trajectory)
    {
        TilemapNavigator navigator  = TilemapNavigator.Instance;
        Vector3          startWorld = navigator.CellToWorldPos(cellStart);
        Vector3          endWorld   = navigator.CellToWorldPos(cellEnd);
        Vector3          midPoint   = trajectory == DamageTrajectory.Curve ?
                                      GetArcMiddlePoint(startWorld, endWorld) : GetMiddlePoint(startWorld, endWorld);

        float movementSpeed = trajectory == DamageTrajectory.Curve ? 6f : 20f;
        float distance      = Vector3.Distance(startWorld, endWorld);

        float duration    = distance * 1 / movementSpeed;
        float elapsedTime = 0f;

        projectile.gameObject.SetActive(true);
        while (elapsedTime < duration)
        {
            float   currentState = elapsedTime / duration;
            Vector3 m1           = Vector3.Lerp(startWorld, midPoint, currentState);
            Vector3 m2           = Vector3.Lerp(midPoint, endWorld, currentState);

            projectile.position = Vector3.Lerp(m1, m2, currentState);
            elapsedTime        += Time.deltaTime;
            yield return(null);
        }
        projectile.gameObject.SetActive(false);
    }
示例#2
0
    private void AlignToGrid()
    {
        TilemapNavigator navigator       = TilemapNavigator.Instance;
        Vector3          alignedPosition = navigator.CellToWorldPos(navigator.WorldToCellPos(transform.position));

        transform.position = new Vector3(alignedPosition.x, alignedPosition.y, UNIT_Z_POSITION);
    }
示例#3
0
    public IEnumerator Move(Vector3Int cellTargetPos, float movementSpeed = 3, bool skipTurning = false)
    {
        TilemapNavigator navigator = TilemapNavigator.Instance;

        Vector3 startingPos    = transform.position;
        Vector3 targetWorldPos = navigator.CellToWorldPos(cellTargetPos);
        Vector3 targetPos      = new Vector3(targetWorldPos.x, targetWorldPos.y, UNIT_Z_POSITION);

        float movementDuration = 1 / movementSpeed;

        UnitBlockManager.Instance.traversalProvider.selectedUnitCell = CellPosition;
        Path path = ABPath.Construct(CellPosition, cellTargetPos, null);

        path.traversalProvider = tilesetTraversalProvider;
        AstarPath.StartPath(path);

        yield return(StartCoroutine(path.WaitForPath()));

        if (!path.error)
        {
            tilesetTraversalProvider.ReleaseNode(CellPosition);
            List <Vector3> waypoints = path.vectorPath.ConvertAll(node => navigator.CellToWorldPos(navigator.WorldToCellPos(node)));
            navigator.GetTile(CellPosition).OnUnitLeave(this);

            for (int currentTarget = 1; currentTarget < waypoints.Count; currentTarget++)
            {
                Vector3 currentStart = currentTarget == 1 ? startingPos : waypoints[currentTarget - 1];
                if (!skipTurning)
                {
                    yield return(StartCoroutine(CheckTurningAnimation(currentStart, waypoints[currentTarget])));
                }

                float elapsedTime = 0f;
                while (elapsedTime < movementDuration)
                {
                    transform.position = Vector3.Lerp(currentStart, waypoints[currentTarget], (elapsedTime / movementDuration));
                    elapsedTime       += Time.deltaTime;
                    yield return(null);
                }
            }

            navigator.GetTile(CellPosition).OnUnitEnter(this);
            tilesetTraversalProvider.ReserveNode(cellTargetPos);
            transform.position = targetPos;
            yield return(AnimateFlip(Player.FacingLeft));
        }
    }