Exemplo n.º 1
0
    public void Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos)
    {
        Init();
        this.targetShip = targetShip;

        if (globalStartPos == globalEndPos)
        {
            StartCoroutine("DelaySearch");
            return;
        }

        nextIndex = 1;

        CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos);
        CellTemplate endCell   = targetShip.GetCellByGlobalPos(globalEndPos);

        // Invalid path supplied
        if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0)
        {
            return;
        }

        ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos);
        ShipPiece endPiece   = targetShip.GetPieceByGlobalCellPos(globalEndPos);


        NavGrid startGrid = new NavGrid(startPiece);

        startGrid.Generate();

        NavGrid goalGrid = startGrid;

        if (startPiece != endPiece)
        {
            goalGrid = new NavGrid(endPiece);
            goalGrid.Generate();
        }

        AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos);

        int newTTmp = 0;

        curPath = aStarAlgorithm.AStarSearch(ref newTTmp);

        wiggleTimer = Random.Range(0, 6.28318f);
    }
    // Sent a navigation command from a selection/target script - attempt pathing
    public bool TryForceNavToPos(Vector2Int cellPos)
    {
        // Click target is at the very least valid
        if (targetShip.GetCellByGlobalPos(cellPos) != null)
        {
            // TMP navigate within the current ship - do a ship connectivity test later and logic to move between ships/buildings/space
            if (Navigate(
                    targetShip,
                    new Vector2Int(Mathf.RoundToInt(transform.localPosition.x / 3.2f), Mathf.RoundToInt(transform.localPosition.y / 3.2f)),
                    cellPos))
            {
                return(true);
            }
        }

        return(false);
    }
    public bool Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos)
    {
        if (globalStartPos == globalEndPos)
        {
            return(false);
        }

        NavCell[] overrideStart = null;

        // Cur, prev and T evaluation
        if (curPath != null)
        {
            if (nextIndex < curPath.Length)
            {
                overrideStart = new NavCell[2];

                // Store C0 and C1
                overrideStart[0] = curPath[nextIndex - 1];
                overrideStart[1] = curPath[nextIndex];
            }
        }

        nextIndex = 1;

        CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos);
        CellTemplate endCell   = targetShip.GetCellByGlobalPos(globalEndPos);

        // Invalid path supplied
        if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0)
        {
            return(false);
        }

        ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos);
        ShipPiece endPiece   = targetShip.GetPieceByGlobalCellPos(globalEndPos);


        NavGrid startGrid = new NavGrid(startPiece);

        startGrid.Generate();

        NavGrid goalGrid = startGrid;

        if (startPiece != endPiece)
        {
            goalGrid = new NavGrid(endPiece);
            goalGrid.Generate();
        }

        AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos);

        int newTState = -1;

        curPath = aStarAlgorithm.AStarSearch(ref newTState, overrideStart);

        // Either reset, invert or leave move timer
        switch (newTState)
        {
        case -1:
            moveTimer = 0;
            break;

        case 1:
            moveTimer = 1 - moveTimer;
            break;
        }

        pathTracer.SetPositions(GetNavArray());

        return(curPath != null);
    }