コード例 #1
0
    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is FieldGridCoordinate))
        {
            return(false);
        }
        FieldGridCoordinate tmp = (FieldGridCoordinate)obj;

        return(tmp.X == X && tmp.Z == Z);
    }
コード例 #2
0
 public List <FieldGridCoordinate> RecalculatePath(FieldGridCoordinate startField)
 {
     return(grid.CalculatePath(startField));
 }
コード例 #3
0
ファイル: Field.cs プロジェクト: floqan/Tower-Defence-2.0
 public Field(int x, int z, float size, Vector3 offset)
 {
     GridCoordinate = new FieldGridCoordinate(x, z);
     this.size      = size;
     this.offset    = offset;
 }
コード例 #4
0
    public List <FieldGridCoordinate> CalculatePath(FieldGridCoordinate startField)
    {
        List <Node> nodesToCheck = new List <Node>();
        List <Node> checkedNodes = new List <Node>();

        nodesToCheck.Add(new Node(startField.X, startField.Z)
        {
            Cost = 0
        });

        //InitGraph(nodesToCheck);
        while (nodesToCheck.Count > 0)
        {
            int  minCostNode = IndexOfMin(nodesToCheck);
            Node tmp         = new Node(0, 0);
            tmp = nodesToCheck[minCostNode];
            nodesToCheck.Remove(tmp);
            checkedNodes.Add(tmp);
            if (Grid[tmp.coordinate.X, tmp.coordinate.Z].IsGoal)
            {
                return(CreatePath(tmp));
            }
            else
            {
                List <Node> neighbors = new List <Node>();
                Node        tmpNeighbor;
                // Get left neighbor
                if (tmp.coordinate.X > 0)
                {
                    tmpNeighbor = new Node(tmp.coordinate.X - 1, tmp.coordinate.Z);
                    if (!Grid[tmpNeighbor.coordinate.X, tmpNeighbor.coordinate.Z].IsEnvironment && !checkedNodes.Any(node => node.coordinate.Equals(tmpNeighbor.coordinate)))
                    {
                        neighbors.Add(tmpNeighbor);
                    }
                }
                // Get right neighbor
                if (tmp.coordinate.X < x - 1)
                {
                    tmpNeighbor = new Node(tmp.coordinate.X + 1, tmp.coordinate.Z);
                    if (!Grid[tmpNeighbor.coordinate.X, tmpNeighbor.coordinate.Z].IsEnvironment && !checkedNodes.Any(node => node.coordinate.Equals(tmpNeighbor.coordinate)))
                    {
                        neighbors.Add(tmpNeighbor);
                    }
                }
                // Get lower neighbor
                if (tmp.coordinate.Z > 0)
                {
                    tmpNeighbor = new Node(tmp.coordinate.X, tmp.coordinate.Z - 1);
                    if (!Grid[tmpNeighbor.coordinate.X, tmpNeighbor.coordinate.Z].IsEnvironment && !checkedNodes.Any(node => node.coordinate.Equals(tmpNeighbor.coordinate)))
                    {
                        neighbors.Add(tmpNeighbor);
                    }
                }
                // Get upper neighbor
                if (tmp.coordinate.Z < z - 1)
                {
                    tmpNeighbor = new Node(tmp.coordinate.X, tmp.coordinate.Z + 1);
                    if (!Grid[tmpNeighbor.coordinate.X, tmpNeighbor.coordinate.Z].IsEnvironment && !checkedNodes.Any(node => node.coordinate.Equals(tmpNeighbor.coordinate)))
                    {
                        neighbors.Add(tmpNeighbor);
                    }
                }

                foreach (Node neighbor in neighbors)
                {
                    int pathCost = tmp.Cost + Grid[neighbor.coordinate.X, neighbor.coordinate.Z].GetPathCost();
                    if (nodesToCheck.Any(node => node.coordinate.Equals(neighbor.coordinate)))
                    {
                        tmpNeighbor = nodesToCheck.Find(n => n.coordinate.Equals(neighbor.coordinate));
                        if (tmpNeighbor.Cost > pathCost)
                        {
                            tmpNeighbor.Cost     = pathCost;
                            tmpNeighbor.Previous = tmp;
                        }
                    }
                    else
                    {
                        neighbor.Previous = tmp;
                        neighbor.Cost     = pathCost;
                        nodesToCheck.Add(neighbor);
                    }
                }
            }
        }
        Debug.LogError("Couldn'f find a valid path from [" + startField.X + "," + startField.Z + "]");
        return(null);
    }
コード例 #5
0
 public Node(int x, int z)
 {
     coordinate = new FieldGridCoordinate(x, z);
 }
コード例 #6
0
 private IEnumerator RecalculatePath(FieldGridCoordinate coordinate)
 {
     Path            = GameManager.instance.RecalculatePath(nextGoal.GridCoordinate);
     pathCalculating = true;
     yield return(null);
 }