public static Tile[] Tiles2DToTiles(Tile[,] tiles2D) { Tile[] tiles = new Tile[19]; int i = 0; for (int y = 0; y < tiles2D.GetLength(0); ++y) { for (int x = 0; x < tiles2D.GetLength(1); ++x) { if (tiles2D[y, x] != null) { tiles[i++] = tiles2D[y, x]; } } } return tiles; }
private static Tile[] CreateTiles(Random rng) { Tile[] tiles = new Tile[19]; TilePool tilePool = new TilePool(); TokenPool tokenPool = new TokenPool(); for (int i = 0; i < 19; ++i) { Tile tile = tilePool.GetRandomTile(rng); if (tile.terrain != TerrainType.Desert) { tile.token = tokenPool.GetRandomToken(rng); } else { tile.token = 0; } tiles[i] = tile; } return tiles; }
public static Tile[,] TilesToTiles2D(Tile[] tiles) { Tile[,] tiles2D = new Tile[5, 5]; for (int i = 0; i < tiles.Length; ++i) { if (i < 3) { tiles2D[0, i + 2] = tiles[i]; } else if (i < 7) { tiles2D[1, i - 3 + 1] = tiles[i]; } else if (i < 12) { tiles2D[2, i - 7] = tiles[i]; } else if (i < 16) { tiles2D[3, i - 12] = tiles[i]; } else { tiles2D[4, i - 16] = tiles[i]; } } return tiles2D; }
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(); }
public TilePool() { tiles = new List<Tile>(); foreach (TerrainType terrain in PoolDefaultCollections.GetDefaultTiles()) { Tile tile = new Tile(); tile.terrain = terrain; tiles.Add(tile); } }
private static void AddEdgeToTile(Edge edge, Tile tile) { if (tile.edges == null) { tile.edges = new Edge[6]; } for (int i = 0; i < tile.edges.Length; ++i) { if (tile.edges[i] == null) { tile.edges[i] = edge; break; } } }
private static void AddTileToEdge(Tile tile, Edge edge) { if (edge.tiles == null) { edge.tiles = new Tile[2]; } for (int i = 0; i < edge.tiles.Length; ++i) { if (edge.tiles[i] == null) { edge.tiles[i] = tile; break; } } }
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; } } }
private static Tile GetNeighbour(int x, int y, Tile[,] tiles2D, NeighbourDirection neighbourDirection) { Tile result = null; switch (neighbourDirection) { case NeighbourDirection.UP: if (y > 0) { result = tiles2D[y - 1, x]; } break; case NeighbourDirection.UPRIGHT: if (y > 0 && x < 4) { result = tiles2D[y - 1, x + 1]; } break; case NeighbourDirection.RIGHT: if (x < 4) { result = tiles2D[y, x + 1]; } break; case NeighbourDirection.DOWN: if (y < 4) { result = tiles2D[y + 1, x]; } break; case NeighbourDirection.DOWNLEFT: if (y < 4 && x > 0) { result = tiles2D[y + 1, x - 1]; } break; case NeighbourDirection.LEFT: if (x > 0) { result = tiles2D[y, x - 1]; } break; } return result; }
private static Node GetNeighboursNode(int x, int y, Tile[,] tiles2D, List<KeyValuePair<NeighbourDirection, int>> neighbours) { Node result = null; foreach (KeyValuePair<NeighbourDirection, int> neighbour in neighbours) { Tile neighbourTile = GetNeighbour(x, y, tiles2D, neighbour.Key); if (neighbourTile != null && neighbourTile.nodes != null) { result = neighbourTile.nodes[neighbour.Value]; break; } } return result; }
private static Edge[] CreateEdges(Tile[,] tiles) { List<Edge> edges = new List<Edge>(); foreach (Tile tile in tiles) { if (tile == null) { continue; } for (int i = 0; i < tile.nodes.Length; ++i) { Node n1 = tile.nodes[i]; Node n2 = tile.nodes[(i + 1) % tile.nodes.Length]; Edge existingEdge = edges.Find(e => (e.nodes[0] == n1 && e.nodes[1] == n2) || e.nodes[0] == n2 && e.nodes[1] == n1); if (existingEdge == null) { Edge edge = new Edge() { nodes = new Node[] { n1, n2 } }; AddEdgeToNode(edge, n1); AddEdgeToNode(edge, n2); AddEdgeToTile(edge, tile); AddTileToEdge(tile, edge); edges.Add(edge); } else { AddEdgeToTile(existingEdge, tile); AddTileToEdge(tile, existingEdge); } } } return edges.ToArray(); }