private static void AddEdgeToNode(Edge edge, Node node) { if (node.edges == null) { node.edges = new Edge[3]; } for (int i = 0; i < node.edges.Length; ++i) { if (node.edges[i] == null) { node.edges[i] = edge; break; } } }
private static Node[] CreateNodes(Tile[,] tiles) { List<Node> nodes = new List<Node>(); for (int y = 0; y < tiles.GetLength(0); ++y) { for (int x = 0; x < tiles.GetLength(1); ++x) { Tile tile = tiles[y, x]; if (tile == null) { continue; } tile.nodes = new Node[6]; for (int n = 0; n < tile.nodes.Length; ++n) { Node neighboursNode = null; #region Switch Case - Find Node in neighbouring Tile switch (n) { case 0: neighboursNode = GetNeighboursNode(x, y, tiles, new List<KeyValuePair<NeighbourDirection, int>>() { new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.UP, 2), new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.UPRIGHT, 4) }); break; case 1: neighboursNode = GetNeighboursNode(x, y, tiles, new List<KeyValuePair<NeighbourDirection, int>>() { new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.UPRIGHT, 3), new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.RIGHT, 5) }); break; case 2: neighboursNode = GetNeighboursNode(x, y, tiles, new List<KeyValuePair<NeighbourDirection, int>>() { new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.RIGHT, 4), new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.DOWN, 0) }); break; case 3: neighboursNode = GetNeighboursNode(x, y, tiles, new List<KeyValuePair<NeighbourDirection, int>>() { new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.DOWN, 5), new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.DOWNLEFT, 2) }); break; case 4: neighboursNode = GetNeighboursNode(x, y, tiles, new List<KeyValuePair<NeighbourDirection, int>>() { new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.DOWNLEFT, 0), new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.LEFT, 2) }); break; case 5: neighboursNode = GetNeighboursNode(x, y, tiles, new List<KeyValuePair<NeighbourDirection, int>>() { new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.LEFT, 1), new KeyValuePair<NeighbourDirection, int>(NeighbourDirection.UP, 3) }); break; } #endregion Node node = null; if (neighboursNode != null) { node = neighboursNode; } else { node = new Node(); nodes.Add(node); } tile.nodes[n] = node; if (node.tiles == null || !node.tiles.Contains(tile)) { AddTileToNode(tile, node); } } } } return nodes.ToArray(); }
private static void AddTileToNode(Tile tile, Node node) { if (node.tiles == null) { node.tiles = new Tile[3]; } for (int i = 0; i < node.tiles.Length; ++i) { if (node.tiles[i] == null) { node.tiles[i] = tile; break; } } }