private void GenerateEdgesByRoom(Room room)
    {
        if (room.IsOutsideRoom())
        {
            // We don't want to loop over all the tiles in the outside room as this would be expensive
            return;
        }

        Path_Node <Room>         node  = nodes[room];
        List <Path_Edge <Room> > edges = new List <Path_Edge <Room> >();

        Dictionary <Tile, Room> neighbours = room.GetNeighbours();

        foreach (Tile tile in neighbours.Keys)
        {
            // We have found a different room to ourselves add an edge from us to them
            Path_Edge <Room> edge = new Path_Edge <Room>();
            edge.cost = 1;
            edge.tile = tile;
            edge.node = nodes[neighbours[tile]];
            edges.Add(edge);
        }

        node.edges = edges.ToArray();
    }
    private void GenerateEdgesByTile(Tile t)
    {
        if (t == null)
        {
            return;
        }

        Path_Node <Tile>         n     = nodes[t];
        List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

        // Get a list of neighbours for the tile
        Tile[] neighbours = t.GetNeighbours(true, true);

        // NOTE: Some of the array spots could be null.
        // If neighbour is walkable, create an edge to the relevant node.
        for (int i = 0; i < neighbours.Length; i++)
        {
            if (neighbours[i] != null && neighbours[i].PathfindingCost > 0 && t.IsClippingCorner(neighbours[i]) == false)
            {
                // This neighbour exists, is walkable, and doesn't requiring clipping a corner --> so create an edge.
                Path_Edge <Tile> e = new Path_Edge <Tile>();
                e.cost = neighbours[i].PathfindingCost;
                e.node = nodes[neighbours[i]];

                // Add the edge to our temporary (and growable!) list
                edges.Add(e);
            }
        }

        n.edges = edges.ToArray();
    }
    private void GenerateEdgesOutside()
    {
        List <Path_Edge <Room> > outsideEdges = new List <Path_Edge <Room> >();
        Room outsideRoom = World.Current.GetOutsideRoom();

        foreach (Room room in nodes.Keys)
        {
            if (room.IsOutsideRoom())
            {
                continue;
            }

            Path_Edge <Room>[] edges = nodes[room].edges;
            foreach (Path_Edge <Room> edge in edges)
            {
                if (edge.node.data.IsOutsideRoom())
                {
                    // Edge connects to the outside room
                    Path_Edge <Room> outsideEdge = new Path_Edge <Room>();

                    // The cost of going outside should be high as to avoid needlessly leaving the base
                    outsideEdge.tile = edge.tile;
                    outsideEdge.cost = 10f;
                    outsideEdge.node = nodes[room];
                    outsideEdges.Add(outsideEdge);
                }
            }
        }

        nodes[outsideRoom].edges = outsideEdges.ToArray();
    }
示例#4
0
    private void CreateEdgesForNode(Block block)
    {
        Path_Node <Block> n = nodes[block];

        List <Path_Edge <Block> > edges = new List <Path_Edge <Block> >();

        // Get a list of neighbors
        // if the neighbor is walkable, create an edge
        // if the block is a ramp, then blocks that are
        // either at the ramp's height or one up from there
        // are walkable neighbors
        // TODO: Implement the part about ramps working

        for (int i = 0; i < 4; i++)
        {
            SquareDirection dir    = SquareDirectionExtensions.ReturnOrthogonal(i);
            Block           nBlock = block.GetNeighbor(dir);
            if (nBlock != null)
            {
                Path_Edge <Block> e = new Path_Edge <Block>();
                e.cost = nBlock.MoveCost;
                e.node = nodes[nBlock];
                edges.Add(e);
            }
        }
        n.edges = edges.ToArray();
    }
示例#5
0
    public Path_TileGraph(World world)
    {
        // Loop through all tiles of the world
        // For each tile, create a node
        //  Do we create nodes for non-floor tiles?  NO!
        //  Do we create nodes for tiles that are completely unwalkable (i.e. walls)?  NO!

        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if(t.movementCost > 0) {	// Tiles with a move cost of 0 are unwalkable
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

        // Now loop through all nodes again
        // Create edges for neighbours

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // Get a list of neighbours for the tile
            Tile[] neighbours = t.GetNeighbours(false);  // NOTE: Some of the array spots could be null.

            // If neighbour is walkable, create an edge to the relevant node.
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].MovementCost > 0 && IsClippingCorner(t, neighbours[i]) == false)
                {
                    // This neighbour exists, is walkable, and doesn't requiring clipping a corner --> so create an edge.

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].MovementCost;
                    e.node = nodes[neighbours[i]];

                    // Add the edge to our temporary (and growable!) list
                    edges.Add(e);

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }
    }
示例#6
0
    public Path_TileGraph(World world)
    {
        Debug.Log("Path_TileGraph Constructor");
        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        // loop through all tiles and create nodes;
        // do we create nodes for non walkable tiles? NO!
        for (int x = 0; x < world.width; x++)
        {
            for (int y = 0; y < world.height; y++)
            {
                Tile t = world.GetTileAt(x, y);
                //if(t.MoveCost > 0 && t.Type != TileType.Mountain && t.Type != TileType.Water && t.building == null)
                //{
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

        // loop through all nodes again,
        // create edges for neighbors
        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile>         n     = nodes[t];
            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // get a list of neighbors for the tile
            Tile[] neighbors = t.GetNeighbours(true); // NOTE some of the array may be null.

            // if neighbor is walkable, create an edge to relevant node
            for (int i = 0; i < neighbors.Length; i++)
            {
                if (neighbors[i] == null) // Breakout of loop if at the edge of the map
                {
                    continue;
                }
                //if( IsClippingCorner ( t, neighbors[i]))
                //{
                //    continue;
                //}
                if (neighbors[i] != null && neighbors[i].MoveCost > 0)
                {
                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbors[i].MoveCost;
                    e.node = nodes[neighbors[i]];

                    // Add the edges to our temporary (and growable!) list
                    edges.Add(e);
                }
            }
            n.edges = edges.ToArray();
        }
        Debug.Log(" Created " + nodes.Count + " nodes");
    }
示例#7
0
    public Path_TileGraph(World world)
    {
        nodes = new Dictionary <Vector3Int, Path_Node <Vector3Int> > ();
        //Loop through all tiles of the world, build a node for each tile
        //Not creating nodes for tiles that are NON walkable(i.e walls, water, windows, etc)
        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Vector3Int tilePos = new Vector3Int(x, y, 0);
                //Creates nodes FOR EVERY tile on the map

                Path_Node <Vector3Int> node = new Path_Node <Vector3Int> ();
                node.data = tilePos;
                nodes.Add(tilePos, node);
            }
        }
        int edgesCount = 0;

        //Now loop through all the nodes, and create the edges
        foreach (Vector3Int t in nodes.Keys)
        {
            //Get a list of neighbors of the tile, if neighbor is walkable create an edge to the relevant node
            Path_Node <Vector3Int>         node  = nodes[t];
            List <Path_Edge <Vector3Int> > edges = new List <Path_Edge <Vector3Int> > ();
            Vector3Int[] neighbors = world.GetNeighbors(t, true);
            for (int i = 0; i < neighbors.Length; i++)
            {
                float movementSpeed = 1f;
                //Get movement Speed based on the tile on tilemap Walkable layer
                if (WorldController.Instance.World.foundationGameMap.ContainsKey(neighbors[i]) == true)
                {
                    movementSpeed = WorldController.Instance.World.foundationGameMap[neighbors[i]].movementCost;
                }
                if (WorldController.Instance.World.foundationGameMap.ContainsKey(neighbors[i]) == false ||
                    (WorldController.Instance.World.foundationGameMap.ContainsKey(neighbors[i]) == true && movementSpeed > 0))
                {
                    if (isClippingCorner(nodes[t].data, neighbors[i]))
                    {
                        continue;
                    }
                    Path_Edge <Vector3Int> e = new Path_Edge <Vector3Int> ();
                    e.cost = movementSpeed;
                    if (nodes.ContainsKey(neighbors[i]) == true)
                    {
                        e.node = nodes[neighbors[i]];
                        edges.Add(e);
                        edgesCount++;
                    }
                }
            }
            node.edges = edges.ToArray();
        }
    }
示例#8
0
    public Path_TileGraph(Area area)
    {
        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        int edgeCount = 0;

        // Use the World Grid to create Nodes from Tile data
        // NODES will contain ONLY tiles that are WALKABLE
        for (int x = 0; x < area.Width; x++)
        {
            for (int y = 0; y < area.Height; y++)
            {
                Tile t = area.GetTile(x, y);
                if (t != null && t.MovementCost > 0)
                {
                    Path_Node <Tile> n = new Path_Node <Tile>();
                    n.data = t;
                    nodes.Add(t, n);
                }
            }
        }

        //Debug.Log("Path_TileGraph -- created " + nodes.Count + " Nodes.");

        // Now loop through nodes to create Edges between them
        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            Tile[] neighbors = t.GetNeighbors(true);

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            for (int i = 0; i < neighbors.Length; i++)
            {
                if (neighbors[i] != null && neighbors[i].MovementCost > 0)
                {
                    // Is walkable
                    Path_Edge <Tile> edge = new Path_Edge <Tile>();
                    edge.cost = neighbors[i].MovementCost;
                    edge.node = nodes[neighbors[i]];
                    edges.Add(edge);

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }

        //Debug.Log("Path_TileGraph -- created " + edgeCount + " Edges.");
    }
示例#9
0
    Dictionary <Tile, Path_Node <Tile> > nodes; // Maps from tiles to nodes

    // Public constructor
    // This class creates a tile compatible graph of the world, each tile is a graph, each walkable neighbour from a tile is linked via an edge
    public Tile_Graph(World world)
    {
        Debug.Log("Tile Graph initialised");
        nodes = new Dictionary <Tile, Path_Node <Tile> >();
        // Loop through all tiles of the world, for each tile that is wallkable create a node, create a node
        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y); // Fetch the tile at the looped x and y - values
                if (t.MovementCost > 0)         // if the tiles are walkable
                {
                    // Create a node
                    Path_Node <Tile> n = new Path_Node <Tile>();
                    n.data = t;
                    nodes.Add(t, n); // Dictionary will now have a corresponding node for any tile in the world that is walkable
                }
            }
        }

        Debug.Log("Created" + nodes.Count + "Nodes");


        int EdgeCount = 0;

        foreach (Tile t in nodes.Keys)                                       // For each tile (Key) in the dictionary
        {
            Path_Node <Tile> n = nodes[t];                                   // Get the corresponding node

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >(); // List of edges that come out of a node
            // Get a list of neighbours
            Tile[] neighbours = t.getNeighbours(true);                       // Note: Some tiles in this array could be null

            // loop through the array of neighbouring tiles for each walkable tile
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].MovementCost > 0)
                {
                    // The nighbouring tile exists and is walkable so create an edge for that neighbour tile
                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost      = neighbours[i].MovementCost; // feeds the neighbouring tile to the edge constructor
                    e.path_Node = nodes[neighbours[i]];       // the node fed to the path_edge is the neighbouring tile that is being processed
                    // Returns the node for the neighbouring tile ? //
                    edges.Add(e);                             // Add the edge to temp and throwable list
                    EdgeCount++;
                }
            }

            n.edges = edges.ToArray(); // Casts the List of Edges to an array that is stored in the node // so that the node is aware of how many edges it has
        }
        Debug.Log(EdgeCount + " Edges created");
    }
示例#10
0
    public Path_TileGraph(World world)
    {
        // Create all nodes via tiles.

        this.nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                // Can actually move through the tile...
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                this.nodes.Add(t, n);
            }
        }



        // Create edges.

        foreach (Tile t in this.nodes.Keys)
        {
            Path_Node <Tile> n = this.nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            Tile[] neighbours = t.GetNeighbours(true); // Can walk diagonal.

            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].movementCost > 0)
                {
                    // Ensure no corners can be clipped.
                    if (this.IsClippingCorner(t, neighbours[i]))
                    {
                        continue;
                    }

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].movementCost;
                    e.node = this.nodes[neighbours[i]];
                    edges.Add(e);
                }
            }

            n.edges = edges.ToArray();
        }
    }
    // Initializes a new instance of the <see cref="Path_TileGraph"/> class.
    public Path_TileGraph(World world)
    {
        nodes = new Dictionary <Tile, Path_Node <Tile> > ();
        //loop the tiles - ncrate a node
        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if (t.movementCost > 0) {
                Path_Node <Tile> n = new Path_Node <Tile> ();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

        //loop the tiles - create edges
        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes [t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> > ();
            //get list of neighbours
            Tile [] neighbours = t.GetNeighbours(true);             // check if null

            for (int i = 0; i < neighbours.Length; i++)
            {
                Tile nbTile = neighbours [i];
                if (nbTile != null && nbTile.movementCost > 0)                    // is walkable SO CREATE EDGE

                {
                    if (isClippingCorner(t, nbTile))
                    {
                        continue;
                    }

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.Cost = nbTile.movementCost;
                    e.Node = nodes [nbTile];
                    //ADD THE EDGE TO TEMP LIST
                    edges.Add(e);
                }
            }

            n.Edges = edges.ToArray();
        }
    }
示例#12
0
    public Path_TileGraph(World world)
    {
        nodes = new Dictionary <Tile, Path_Node <Tile> > ();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile tile = world.getTileAt(x, y);
                //movementCost==0 -> impassable
                //if (tile.movementCost > 0) {
                Path_Node <Tile> node = new Path_Node <Tile> ();
                node.data = tile;
                nodes.Add(tile, node);
                //}
            }
        }

        //Debug.Log ("Path_TileGraph -- Created " + nodes.Count + " nodes");
        int edgesCount = 0;

        foreach (Tile tile in nodes.Keys)
        {
            Path_Node <Tile>         node  = nodes [tile];
            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> > ();       //Heavy cumputation?
            Tile[] neighbours = tile.getNeighboors(true);

            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours [i] != null && neighbours [i].movementCost > 0)
                {
                    if (isClippingCorner(tile, neighbours [i]) == true)
                    {
                        continue;
                    }
                    Path_Edge <Tile> edge = new Path_Edge <Tile> ();
                    edge.cost = neighbours [i].movementCost;
                    edge.node = nodes [neighbours [i]];
                    edges.Add(edge);
                    edgesCount++;
                }
            }

            node.egdes = edges.ToArray();
        }

        //Debug.Log ("Path_TileGraph -- Created " + edgesCount + " edges");
    }
示例#13
0
    public Path_TileGraph(World world)
    {
        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        //loop all tiles , create a node for each one
        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                if (t.MovementCost > 0)   // if unwalkable
                {
                    Path_Node <Tile> n = new Path_Node <Tile>();
                    n.data = t;
                    nodes.Add(t, n);
                }
            }
        }

        //create edges for each
        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();
            //Get neighbors
            Tile[] neighbours = t.GetNeighbours(true); //? some array spots could be null

            //if walkable create edge
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].MovementCost > 0)
                {
                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].MovementCost;
                    e.node = nodes[neighbours[i]];

                    edges.Add(e);
                }
            }

            n.edges = edges.ToArray();
        }

        Debug.Log("Pathfinding: " + nodes.Count + " Nodes Created");
    }
    public Path_TileGraph(World world)
    {
        _nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile tile = world.GetTileAt(x, y);
                //if(tile.MovementCost > 0)
                //{
                Path_Node <Tile> node = new Path_Node <Tile>();
                node.Data = tile;
                _nodes.Add(tile, node);
                //}
            }
        }

        foreach (Tile tile in _nodes.Keys)
        {
            Path_Node <Tile> node = _nodes[tile];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            Tile[] neighbours = tile.GetNeighbours(true);

            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].MovementCost > 0)
                {
                    if (CheckIfNeighbourIsUnwalkable(i, neighbours))
                    {
                        continue;
                    }

                    Path_Edge <Tile> edge = new Path_Edge <Tile>();
                    edge.Cost = neighbours[i].MovementCost;
                    edge.Node = _nodes[neighbours[i]];
                    edges.Add(edge);
                }
            }

            node.Edges = edges.ToArray();
        }
    }
示例#15
0
    public Path_TileGraph(World world, bool debug = false)
    {
        nodes = new Dictionary <Tile, Path_Node <Tile> >();
        //Loop thorugh all tiles of the world
        //For each tile, create a node
        //Do we create nodes for non.floor tiles? No
        //Do we create nodes for tiles that are completely unwalkable (i.e. walls)? No

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if (t.MovementCost > 0) { //Tiles with a move cost of 0 are unwalkable
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                if (debug)
                {
                    Debug.DrawLine(new Vector3(x + 0.4f, y + 0.25f, 0), new Vector3(x + 0.6f, y + 0.75f, 0), Color.red, 5f);
                }
                //}
            }
        }

        //Now loop through all tiles again
        //Create edges for neighbours

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            //Get a list of neighbours for the tile
            ReadOnlyCollection <Tile> neighbours = t.GetNeighbours(true);            //NOTE: Some of the array spots could be null

            //If a neighbour is walkable, create an edge to the relevant node.
            for (int i = 0; i < neighbours.Count; i++)
            {
                if (neighbours[i] != null && neighbours[i].MovementCost > 0)
                {
                    //This neighbours exists and is walkable, so create an edge.

                    //But first, make sure we aren't clipping a diagonal or trying to squeeze inappropriately
                    if (IsClippingCorner(neighbours, i))
                    {
                        continue;
                    }


                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].MovementCost * (i < 4 ? 1 : 1.41421356237f);
                    e.node = nodes[neighbours[i]];
                    edges.Add(e);
                    if (debug && t.MovementCost > 0)
                    {
                        Debug.DrawLine(new Vector3(t.X + 0.5f, t.Y + 0.5f, 0), new Vector3(e.node.data.X + 0.5f, e.node.data.Y + 0.5f, 0), Color.green, 10f);
                    }
                }
            }



            n.edges = edges.ToArray();
        }
    }
示例#16
0
    public Path_TileGraph(World world)
    {
        Debug.Log("Path_TileGraph");
        //assumptions:
        //create a node for each floor tile
        //we don't create nodes for tiles that are unwalkable (like walls)

        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if (t.movementCost > 0) //tiles with movement of 0 is unwalkable
                //{
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

        Debug.Log($"Path_TileGraph: Created {nodes.Count} nodes");

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            Tile[] neighbours = t.GetNeighbours(true); //SOME ARRAY SPOTS COULD BE NULL

            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].movementCost > 0)
                {
                    //neighbour tile is walkable, so create edge

                    //first we must make sure we are not clipping a diagonal inappropriately
                    if (IsClippingCorner(t, neighbours[i]))
                    {
                        continue;
                    }

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].movementCost;
                    e.node = nodes[neighbours[i]];

                    //add edge to list
                    edges.Add(e);

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }

        Debug.Log($"Path_TileGraph: Created {edgeCount} edges");
    }
示例#17
0
    // Get World data and if they are limited tile
    public Path_TileGraph(int startWidth, int endWidth, int startHeight, int endHeight)
    {
        // Loop through all tiles of the world
        // For each tile, create a node
        //  Do we create nodes for non-floor tiles?  NO!
        //  Do we create nodes for tiles that are completely unwalkable (i.e. walls)?  NO!

        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        // Search space from start -> end  width & height
        for (int x = startWidth; x <= endWidth; x++)
        {
            for (int y = startHeight; y <= endHeight; y++)
            {
                Tile t = WorldController.Instance.World.GetTileAt(x, y);

                if (t.movementCost > 0)                             // Tiles with a move cost of 0 are unwalkable
                {
                    Path_Node <Tile> n = new Path_Node <Tile>();
                    n.data = t;
                    nodes.Add(t, n);
                    //Debug.Log("Nodes have : " + t.X + "," + t.Z);
                }
            }
        }

        Debug.Log("Path_TileGraph: Created " + nodes.Count + " nodes.");


        // Now loop through all nodes again
        // Create edges for neighbours

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // Get a list of neighbours for the tile
            Tile[] neighbours = t.GetNeighbours(true);                  // NOTE: Some of the array spots could be null.

            // If neighbour is walkable, create an edge to the relevant node.
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].movementCost > 0
                    // Check for little tile graph not get out of bound neighbour tile
                    && nodes.ContainsKey(neighbours[i])
                    //&& IsClippingCorner( t, neighbours[i] ) == false
                    )
                {
                    // This neighbour exists, is walkable, and doesn't requiring clipping a corner --> so create an edge.
                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].movementCost;
                    e.node = nodes[neighbours[i]];

                    // Add the edge to our temporary (and growable!) list
                    edges.Add(e);

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }

        Debug.Log("Path_TileGraph: Created " + edgeCount + " edges.");
    }
示例#18
0
    public Dictionary <Tile, Path_Node <Tile> > nodes; //Define a dictonary - a link between tiles and nodes

    public Path_TileGraph(World world)
    {
        Debug.Log("Path_TileGraph");

        // Loop through all tilse of the world
        // For each tile, create a node
        // Do we create nodes for non-floor tiles? NO!
        // Do we create nodes for tiles that are completely unwalkable? (i.e. walls?) NO!

        nodes = new Dictionary <Tile, Path_Node <Tile> >(); //Instanciate a dictonary - a link between tiles and nodes

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if(t.MovementCost > 0) { // Tiles with a move cost of 0 are unwalkable
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);

                //}
            }
        }

        Debug.Log("Path_TileGraph: Created " + nodes.Count + " nodes.");

        // Now loop through all nodes again
        // Create edges for neighbours

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)                           // Go through whole dictonary

        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // Get a list of neighbours for the tile
            Tile[] neighbours = t.GetNeighbours(true); // NOTE: Some of the array spots could be null


            // If neighbour is walkable, create an edge to the relevent node
            for (int i = 0; i < neighbours.Length; i++)                      // for each neighbour...
            {
                if (neighbours[i] != null && neighbours[i].MovementCost > 0) // This neighbour exists and is empty (air)

                // Now check if there is a block in the right place to stand on
                // If the neighbour tile is not standable, we skip to the next neighbour without building an edge
                {
                    if (neighbours[i].IsStandable() == false)
                    {
                        continue;
                    }


                    // But first make sure we are not clipping a diagonal or trying to squeeze inappropiately
                    if (IsClippingCorner(t, neighbours[i]))
                    {
                        continue;       // Skip to the next neighbour without building an edge
                    }

                    Path_Edge <Tile> e = new Path_Edge <Tile>(); // Create a new edge
                    e.cost = neighbours[i].MovementCost;         // Set the movement cost
                    e.node = nodes[neighbours[i]];               // Set the node

                    edges.Add(e);                                // Add this new edge to our temporary (and growable) list of edges

                    edgeCount++;                                 // Debug counter
                }
            }

            n.edges = edges.ToArray();                          // Give the node the list of edges
        }

        Debug.Log("Path_TileGraph: Created " + edgeCount + " edges.");
    }
示例#19
0
    public Path_TileGraph(World world)
    {
        //遍历世界上所有的瓷砖
        //对于每个图块,创建一个节点
        //我们是否为非地砖创建节点? 没有!
        //我们是否为完全不可行走的瓷砖(即墙壁)创建节点? 没有!
        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.width; x++)
        {
            for (int y = 0; y < world.height; y++)
            {
                Tile tile = world.GetTileAt(x, y);
                //移动成本为0的瓷砖是不能进行移动的位置
                //if (tile.movementCost > 0)
                //{
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = tile;
                nodes.Add(tile, n);
                // }
            }
        }

        //现在再次遍历所有节点
        //为邻居创建边
        int edgeCount = 0;

        foreach (Tile tile in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[tile];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // //获取Tile的邻居列表
            Tile[] neighbours = tile.GetNeighbours(true); //注意:某些数组点可能为空。

            //如果邻居是可步行的,则为相关节点创建边缘。
            for (int i = 0; i < neighbours.Length; i++)
            {
                //if (neighbours[i] != null && neighbours[i].movementCost > 0)
                if (neighbours[i] != null && neighbours[i].movementCost > 0 && IsClippingCorner(tile, neighbours[i]) == false)
                {
                    {
                        //这个邻居存在且可以步行,所以创造一个边缘。
                        Path_Edge <Tile> e = new Path_Edge <Tile>();
                        e.cost = neighbours[i].movementCost;
                        e.node = nodes[neighbours[i]];

                        //将边添加到我们的临时(和可扩展的!)列表中
                        edges.Add(e);

                        edgeCount++;
                    }
                }
                n.path_Edges = edges.ToArray();
            }
        }
        /// <summary>
        /// 是否斜角
        /// </summary>
        /// <param name="curr"></param>
        /// <param name="neigh"></param>
        /// <returns></returns>
        bool IsClippingCorner(Tile curr, Tile neigh)
        {
            //如果从curr到neigh的运动是对角线的(例如N-E)
            //然后检查以确保我们没有斜角(例如N和E都可以走路)
            int dX = curr.x - neigh.x;
            int dY = curr.y - neigh.y;

            if (Mathf.Abs(dX) + Mathf.Abs(dY) == 2)
            {
                //我们是对角的
                if (curr.world.GetTileAt(curr.x - dX, curr.y).movementCost == 0)
                {
                    //东方或西方是不可行的,因此这将是一个斜角的运动。
                    return(true);
                }

                if (curr.world.GetTileAt(curr.x, curr.y - dY).movementCost == 0)
                {//北或南是不可行的,因此这将是一个斜角的运动。
                    return(true);
                }

                //如果我们到达这里,我们是对角线,但不是斜角
            }
            //如果我们在这里,我们要么不斜角,要么不对角
            return(false);
        }
    }
    public Path_TileGraph(World world)
    {
        Debug.Log("Path_TileGraph");

        // Loop through all tiles of the world
        // For each tile, create a node
        //  Do we create nodes for non-floor tiles?  NO!
        //  Do we create nodes for tiles that are completely unwalkable (i.e. walls)?  NO!

        nodes = new Dictionary<Tile, Path_Node<Tile>>();

        for (int x = 0; x < world.Width; x++) {
            for (int y = 0; y < world.Height; y++) {

                Tile t = world.GetTileAt(x,y);

                //if(t.movementCost > 0) {	// Tiles with a move cost of 0 are unwalkable
                    Path_Node<Tile> n = new Path_Node<Tile>();
                    n.data = t;
                    nodes.Add(t, n);
                //}

            }
        }

        Debug.Log("Path_TileGraph: Created "+nodes.Count+" nodes.");

        // Now loop through all nodes again
        // Create edges for neighbours

        int edgeCount = 0;

        foreach(Tile t in nodes.Keys) {
            Path_Node<Tile> n = nodes[t];

            List<Path_Edge<Tile>> edges = new List<Path_Edge<Tile>>();

            // Get a list of neighbours for the tile
            Tile[] neighbours = t.GetNeighbours(true);	// NOTE: Some of the array spots could be null.

            // If neighbour is walkable, create an edge to the relevant node.
            for (int i = 0; i < neighbours.Length; i++) {
                if(neighbours[i] != null && neighbours[i].movementCost > 0 && IsClippingCorner( t, neighbours[i] ) == false) {
                    // This neighbour exists, is walkable, and doesn't requiring clipping a corner --> so create an edge.

                    Path_Edge<Tile> e = new Path_Edge<Tile>();
                    e.cost = neighbours[i].movementCost;
                    e.node = nodes[ neighbours[i] ];

                    // Add the edge to our temporary (and growable!) list
                    edges.Add(e);

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }

        Debug.Log("Path_TileGraph: Created "+edgeCount+" edges.");
    }
    public Path_TileGraph(World world)
    {
        // We don't create nodes for non-floor tiles nither unwalkable (i.e. walls) ones. <<-- IMPORTANT

        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        // Loop through all tiles of the world, creating a node for each one.
        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                // Get Tile
                Tile t = world.GetTileAt(x, y);
                //if (t.movementCost > 0) {   // Tiles with movementCost < 0 are unwalkable or empty
                // Create node
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                // Add Tile/node pair to dictionary
                nodes.Add(t, n);
                //}
            }
        }

        int edgeCount = 0; // For debug only

        // Loop through all nodes again creating edges for neighbours
        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> node = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // Get a list of neighbours for this tile.
            Tile[] neighbours = t.GetNeighbours(true); // NOTE: Some of the array spots could be null.

            //  If neighbour is walkable, create an edge.
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].movementCost > 0)
                {
                    // Neighbours exists and is walkable

                    if (IsCuttingCorner(t, neighbours[i]))
                    {
                        continue;   // Skip to the next neighbour
                    }
                    //Creates edge
                    Path_Edge <Tile> edge = new Path_Edge <Tile>();
                    edge.cost = neighbours[i].movementCost;
                    edge.node = nodes[neighbours[i]];

                    //Add the edge to temporary list
                    edges.Add(edge);
                    edgeCount++; // Remove after debug
                }
            }

            // Store edges in the node
            node.edges = edges.ToArray();
        }

        //Debug.Log(nodes.Count + " nodes and " + edgeCount + " edges created!");
    }
    public Path_TileGraph(World world)
    {
        Debug.Log("Path_TileGraph");

        //Loop through all tiles of the world
        //for each tile, create a node
        //do we create nodes for non-floor tiles ? NO
        //Do we create nodes for tiles that are completely unwalkable (i.e walls ?) NO

        nodes = new Dictionary <Tile, Path_Node <Tile> > ();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if(t.movementCost > 0) { //Tiles witha move cost of 0 are unwalkable
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

        Debug.Log("Path_TileGraph : Created " + nodes.Count + "nodes.");

        //now loop through all nodes again
        //create edges for neighbours

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            //Get a list of nieghbourgs for the tile
            Tile[] neighbours = t.GetNeighbours(true);             // NOTE : some of the array spots could be likely null !
            //if neighbour is walkable, create an edge to the revelant node
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].movementCost > 0 && IsClippingCorner(t, neighbours[i]) == false)
                {
                    //This neighbour exist and is walkable, and doesn't requiring clipping a corner -- > so create an edge !

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].movementCost;
                    e.node = nodes[neighbours[i]];

                    //add the edge to our temporary and growable list
                    edges.Add(e);
                    edgeCount++;
                }
            }
            //on convert la liste en array
            n.edges = edges.ToArray();
        }
        Debug.Log("Path_TileGraph : Created " + edgeCount + "edges.");
    }
示例#23
0
    public Path_TileGraph(World world)
    {
        Debug.Log("Path_TileGraph");
        // Loop through all tiles of the world
        // For each tile, create a node
        // Do we create nodes for non-floor tiles? No.
        // Do we create nodes for tiles that are complete unwalkable?  No.

        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                //if (t.movementCost > 0) // Tiles with move cost 0 are unwalkable
                //{
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

        Debug.Log("Path_TileGraph: Created " + nodes.Count + " nodes.");

        // Now loop through all nodes again
        // Create edges for neighbours

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            // Get a list of neighbours for the tile
            Tile[] neighbours = t.GetNeighbours(true);  // Note: some array spots could be null

            // If neighbour is walkable, create an edge to the relevant node.
            for (int i = 0; i < neighbours.Length; i++)
            {
                if (neighbours[i] != null && neighbours[i].movementCost > 0 && IsClippingCorner(t, neighbours[i]) == false)
                {
                    // Neighbour exists and is walkable, and doesn't clip corner, create an edge

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].movementCost;
                    e.node = nodes[neighbours[i]];

                    edges.Add(e);

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }

        Debug.Log("Path_TileGraph: Created " + edgeCount + " edges.");
    }
示例#24
0
    public Path_TileGraph(World world)
    {
//		Debug.Log ("Path_TileGraph");
        // looping thourh all the tiles in the world.
        // for each tile, create a node
        // do not create a node for a blocked tile

        nodes = new Dictionary <Tile, Path_Node <Tile> > ();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile t = world.GetTileAt(x, y);

                // tiles with a movement cost of 0 are unwalkable
                //if (t.movementCost > 0) {
                Path_Node <Tile> n = new Path_Node <Tile> ();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }

//		Debug.Log ("Path_TileGraph: Created " + nodes.Count + " nodes.");

        // now loop through all nodes again
        // and create edges for neighbors

        int edgeCount = 0;

        foreach (Tile t in nodes.Keys)
        {
            // Get a list of neighbors for the tile
            // for every walkable neighbor, create an edge
            Path_Node <Tile> n = nodes [t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> > ();

            Tile[] neighbors = t.GetNeighbors(false);

            for (int i = 0; i < neighbors.Length; i++)
            {
                if (neighbors [i] != null && neighbors [i].movementCost > 0)
                {
                    // this neighbor exists and is walkable so crate an edge for it

                    // remove next check if only going only in straight lines
                    if (IsClippingCorner(t, neighbors [i]))
                    {
                        continue;
                    }

                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbors[i].movementCost;
                    edges.Add(e);
                    e.node = nodes[neighbors[i]];

                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }

//		Debug.Log("Path_TileGraph: Created "+edgeCount+" edges.");
    }
示例#25
0
    public Path_TileGraph(World world)
    {
        // Loop through all tiles in the World
        // For each tile, create a node
        // Do we create nodes for non-floor tiles? Maybe...not sure yet
        // Do we create nodes for tiles that are completely unwalkalbe? (walls/buildings) Probably NO

        tileToNodeMap = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < world.Width; x++)
        {
            for (int y = 0; y < world.Height; y++)
            {
                Tile tile = world.GetTileAt(x, y);

                // Tiles with movementCost of 0 are impassable, 1 is normal & 1> is slower than normal movement
                //if (tile.MovementCost > 0)
                //{

                //}

                Path_Node <Tile> path_Node = new Path_Node <Tile>();
                path_Node.data = tile;
                tileToNodeMap.Add(tile, path_Node);
            }
        }

        // Now loop through all tiles again
        // Create edges for neighbours
        foreach (Tile tile in tileToNodeMap.Keys)
        {
            // Get a list of neighbours for the tile
            // If neighbour is walkable, create an edge to the relevant node.
            Path_Node <Tile> path_Node = tileToNodeMap[tile];

            List <Path_Edge <Tile> > path_Edges = new List <Path_Edge <Tile> >();

            // Some spots in array might by null (no neighbours, e.g. corner tiles in the World)
            Tile[] neighbours = tile.GetNeighbours(true);

            for (int i = 0; i < neighbours.Length; i++)
            {
                // There is a neighbour and the movement cost is more than 0 so it's walkable
                if (neighbours[i] != null && neighbours[i].MovementCost > 0)
                {
                    // Check for invalid diagonal clipping/movement, like squeezing through two diagonal walls.
                    if (IsClippingCorner(tile, neighbours[i]))
                    {
                        // Skip to next neighbour, without creating an edge
                        continue;
                    }

                    // Create new path_Edge of type Tile
                    Path_Edge <Tile> path_Edge = new Path_Edge <Tile>();

                    // Set travelcost equal to neighbouring tile's movementcost
                    path_Edge.travelCost = neighbours[i].MovementCost;

                    // Pair a path_Node, grab that from the dictionary
                    path_Edge.path_Node = tileToNodeMap[neighbours[i]];

                    // Add the created edge to the list
                    path_Edges.Add(path_Edge);
                }
            }

            // Set path_Node's array equal to the path_Edges list, casted to an array
            path_Node.edges = path_Edges.ToArray();
        }
    }
示例#26
0
    public Path_TileGraph(World _world)
    {
        //loop through all world walkable floor tiles, and create a Node for each.

        Debug.Log("Path_TileGraph");

        nodes = new Dictionary <Tile, Path_Node <Tile> >();

        for (int x = 0; x < _world.Width; x++)
        {
            for (int y = 0; y < _world.Height; y++)
            {
                Tile t = _world.GetTileAt(x, y);

                //if (t.MovementCost > 0) //Tiles with cost of 0 are non-walkable
                //{
                Path_Node <Tile> n = new Path_Node <Tile>();
                n.data = t;
                nodes.Add(t, n);
                //}
            }
        }
        Debug.Log("Path_TileGraph: created " + nodes.Count + " nodes");

        int edgeCount = 0;

        //Now loop through all nodes and create edges

        foreach (Tile t in nodes.Keys)
        {
            Path_Node <Tile> n = nodes[t];

            List <Path_Edge <Tile> > edges = new List <Path_Edge <Tile> >();

            //Get list of nieghbours for tiles
            Tile[] neighbours = t.GetNeighbours(true); //Some array spots may be null

            //create an edge if the nieghbour is walkable
            for (int i = 0; i < neighbours.Length; i++)
            {
                //See if neighbour exists and is walkable
                if (neighbours[i] != null && neighbours[i].MovementCost != 0)
                {
                    //make sure we won't clip through wall corners, or squeeze through 2 diagonal walls
                    if (IsClippingCorner(t, neighbours[i]))
                    {
                        continue; //skip to next neighbor with out building edge
                    }

                    //create an edge
                    Path_Edge <Tile> e = new Path_Edge <Tile>();
                    e.cost = neighbours[i].MovementCost;
                    e.node = nodes[neighbours[i]];

                    //add edge to temporary list
                    edges.Add(e);
                    edgeCount++;
                }
            }

            n.edges = edges.ToArray();
        }
        Debug.Log("Path_TileGraph: created " + edgeCount + " edges");
    }