Esempio n. 1
0
 void Initialize()
 {
     transform.position     = _initialPosition;
     FoundGoal              = false;
     FoundObstacle          = false;
     PerformedActionsNumber = 0;
     ExploredPuts           = 0;
     Matrix = MatrixFromRaycast.CreateMatrixFromRayCast();
     this.GetComponent <TrailRenderer>().time = _totalAnimationTime;
 }
Esempio n. 2
0
 // Use this for initialization
 void Start()
 {
     _initialPosition = transform.position;
     Matrix           = MatrixFromRaycast.CreateMatrixFromRayCast();
     if (PlayerScript._trailColorIterator == null)
     {
         PlayerScript._trailColorIterator = _playerTrailColors().GetEnumerator();
     }
     PlayerScript._trailColorIterator.MoveNext();
     this.GetComponent <TrailRenderer>().time = _totalAnimationTime;
     this.GetComponent <TrailRenderer>().materials[0].SetColor("_TintColor", PlayerScript._trailColorIterator.Current);
 }
Esempio n. 3
0
    public IEnumerator AStar()
    {
        _isRunning = true;

        // Recovering the environment as a matrix
        var matrix = MatrixFromRaycast.CreateMatrixFromRayCast();

        /*
         * // Convert as boolean grid
         * // true => wall || explored
         * // false => unknow
         */
        bool[][] ExploredGrid = new bool[matrix.Length][];
        for (int i = 0; i < matrix.Length; i++)
        {
            ExploredGrid[i] = new bool[matrix[i].Length];
            for (int j = 0; j < matrix[i].Length; j++)
            {
                ExploredGrid[i][j] = (matrix[i][j] == LayerMask.NameToLayer("Obstacle")) ? true : false;
            }
        }

        //Label all the nodes with an infinite score
        int[][] Grid = new int[matrix.Length][];
        for (int i = 0; i < matrix.Length; i++)
        {
            Grid[i] = new int[matrix[i].Length];
            for (int j = 0; j < matrix[i].Length; j++)
            {
                Grid[i][j] = int.MaxValue;
            }
        }

        int[][] HeuristicGrid = new int[matrix.Length][];
        for (int i = 0; i < matrix.Length; i++)
        {
            HeuristicGrid[i] = new int[matrix[i].Length];
            for (int j = 0; j < matrix[i].Length; j++)
            {
                HeuristicGrid[i][j] = ManhattanScore(i, j);
            }
        }

        // get position
        var startPosX = PlayerScript.StartXPositionInMatrix;
        var startPosY = PlayerScript.StartYPositionInMatrix;
        var endPosX   = PlayerScript.GoalXPositionInMatrix;
        var endPosY   = PlayerScript.GoalYPositionInMatrix;

        // init start pos at 0
        Grid[startPosX][startPosY]         = 0;
        ExploredGrid[startPosX][startPosY] = true;

        //define move cost
        const int moveCost = 1;

        //define the current case
        var currentPos = new currentNode
        {
            XPos = startPosX,
            YPos = startPosY
        };

        var it = 0;

        while (true)
        {
            Instantiate(CubeCurrentNode, new Vector3(currentPos.XPos - 25, -0.9f, currentPos.YPos - 25), Quaternion.identity);
            if (currentPos.XPos == endPosX && currentPos.YPos == endPosY)
            {
                Debug.Log("Solution found in " + it + " iterations");
                //todo return the optimal path
                break;
            }
            MarkNeighbourNode(Grid, ExploredGrid, currentPos, moveCost);
            currentPos = MoveToNextNodeWithHeuristic(Grid, HeuristicGrid, ExploredGrid, currentPos);

            it++;
            yield return(null);
        }
        _isRunning = false;
        yield return(null);
    }