示例#1
0
        public void AddDirection(GridPathNode node, int movementCost)
        {
            GridMovement gridMovement = new GridMovement(node, movementCost);

            directions[directionCount] = gridMovement;
            directionCount++;
        }
        private int GetLowest(NativeList <int> open)
        {
            GridPathNode lowest = nodes[open[0]];

            for (int i = 0; i < open.Length; i++)
            {
                if (nodes[open[i]].sum < lowest.sum)
                {
                    lowest = nodes[open[i]];
                }
            }
            return(lowest.index);
        }
		public void AddDirection(GridPathNode node, int movementCost)
		{
			GridMovement gridMovement = new GridMovement(node, movementCost);
			directions[directionCount] = gridMovement;
			directionCount++;
		}
			public GridMovement(GridPathNode node, int movementCost)
			{
				this.node = node;
				this.movementCost = movementCost;
			}
示例#5
0
 public GridMovement(GridPathNode node, int movementCost)
 {
     this.node         = node;
     this.movementCost = movementCost;
 }
        public void Execute()
        {
            NativeArray <int2> neighbors = new NativeArray <int2>(8, Allocator.Temp);

            neighbors[0] = new int2(-1, 0);
            neighbors[1] = new int2(+1, 0);
            neighbors[2] = new int2(0, +1);
            neighbors[3] = new int2(0, -1);
            neighbors[4] = new int2(-1, -1);
            neighbors[5] = new int2(-1, +1);
            neighbors[6] = new int2(+1, +1);
            neighbors[7] = new int2(+1, -1);

            for (int i = 0; i < nodes.Length; i++)
            {
                nodes[i].Init(CalcDistanceCost(nodes[i].position, end), gridSize.x);
            }

            int endNodeIndex   = CalcIndex(end);
            int startNodeIndex = CalcIndex(start);

            nodes[startNodeIndex].SetCost(0);

            NativeList <int> open = new NativeList <int>(Allocator.Temp)
            {
                startNodeIndex
            };

            int it = 0;

            while (open.Length > 0 && it < 50)
            {
                it++;
                int current           = GetLowest(open);
                GridPathNode currentN = nodes[current];
                if (current == endNodeIndex)
                {
                    //Debug.Log("Found Path");
                    break;
                }
                for (int i = 0; i < open.Length; i++)
                {
                    if (open[i] == current)
                    {
                        open.RemoveAtSwapBack(i);
                        break;
                    }
                }
                nodes[current].Close();

                for (int i = 0; i < neighbors.Length; i++)
                {
                    int2 neighbor         = neighbors[i];
                    int2 neighborposition = new int2(currentN.position.x + neighbor.x, currentN.position.y + neighbor.y);

                    if (!ValidPosition(neighborposition))
                    {
                        continue;
                    }
                    int neighborIndex = CalcIndex(neighborposition);
                    if (nodes[neighborIndex].closed || !nodes[neighborIndex].walkable)
                    {
                        continue;
                    }

                    int newCost = currentN.cost + CalcDistanceCost(currentN.position, neighborposition);
                    if (newCost < nodes[neighborIndex].cost)
                    {
                        nodes[neighborIndex].Update(current, newCost);
                    }

                    if (!open.Contains(neighborIndex))
                    {
                        open.Add(neighborIndex);
                    }
                }
            }
            //Debug.Log("Didn't find a path");
            neighbors.Dispose();
            open.Dispose();
        }