Ejemplo n.º 1
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);
    }
Ejemplo n.º 2
0
    private void HandleMouseHover()
    {
        Vector3Int hoveredPosition = navigator.WorldToCellPos(
            mainCamera.ScreenToWorldPoint(Input.mousePosition)
            );

        if (HoveredCell == null || HoveredCell != hoveredPosition)
        {
            HoveredCell = hoveredPosition;
        }
    }
Ejemplo n.º 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));
        }
    }