Exemplo n.º 1
0
		public static Island Construct(int seed) {
			Random rng = new Random(seed);
			Island island = new Island();

			island.tiles = CreateTiles(rng);
			Tile[,] tiles = Utilities.TilesToTiles2D(island.tiles);

			island.nodes = CreateNodes(tiles);

			island.edges = CreateEdges(tiles);

			return island;
		}
Exemplo n.º 2
0
		public void CreateIsland(Island island) {
			tilesParent = new GameObject("Tiles").transform;
			tilesParent.SetParent(transform);

			nodesParent = new GameObject("Nodes").transform;
			nodesParent.SetParent(transform);

			edgesParent = new GameObject("Edges").transform;
			edgesParent.SetParent(transform);

			List<TileGameObject> tileGameObjects = CreateTiles(island);

			List<NodeGameObject> nodeGameObjects = CreateNodes(tileGameObjects);

			CreateEdges(island, nodeGameObjects);
		}
Exemplo n.º 3
0
		private List<TileGameObject> CreateTiles(Island island) {
			Tile[,] tiles = Utilities.TilesToTiles2D(island.tiles);

			List<TileGameObject> tileGameObjects = new List<TileGameObject>();

			float spacingHorizontal = Mathf.Sqrt(3) / 2;
			float spacingVertical = 1f;

			int tileIndex = 0;
			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;
					}

					Vector3 position = new Vector3(
						(spacingHorizontal * GetHorizontalOffset(y)) + (spacingHorizontal * x) - (spacingHorizontal * 2),
						0,
						spacingVertical * GetVerticalOffset(y));

					GameObject go = CreateTile();
					go.transform.position = position;
					go.name = go.name + " " + tileIndex++.ToString("00");
					go.GetComponent<MeshRenderer>().material.color = GetTerrainColor(tiles[y, x].terrain);

					if (tile.terrain != TerrainType.Desert) {
						PrefabLoader.LoadAndGetComponent<Token>(token => {
							token.transform.SetParent(go.transform);
							token.transform.localPosition = Vector3.zero;
							token.GetComponent<Token>().SetNumber(tile.token);
						});
					}

					tileGameObjects.Add(new TileGameObject() {
						tile = tile,
						gameObject = go
					});
				}
			}

			return tileGameObjects;
		}
Exemplo n.º 4
0
		private List<EdgeGameObject> CreateEdges(Island island, List<NodeGameObject> nodeGameObjects) {
			List<EdgeGameObject> edgeGameObjects = new List<EdgeGameObject>();

			int edgeIndex = 0;
			foreach (Edge edge in island.edges) {
				if (edgeGameObjects.Any(e => e.edge == edge)) {
					continue;
				}

				NodeGameObject n1 = null;
				NodeGameObject n2 = null;

				foreach (NodeGameObject nodeGameObject in nodeGameObjects) {
					if (nodeGameObject.node == edge.nodes[0]) {
						n1 = nodeGameObject;
					}

					if (nodeGameObject.node == edge.nodes[1]) {
						n2 = nodeGameObject;
					}

					if (n1 != null && n2 != null) {
						break;
					}
				}

				Vector3 nodePos = n1.gameObject.transform.position;
				Vector3 otherNodePos = n2.gameObject.transform.position;
				Vector3 position = nodePos + (otherNodePos - nodePos).normalized * 0.25f;

				GameObject go = CreateEdge();
				go.transform.position = position;
				go.transform.LookAt(nodePos);
				go.name = "Edge " + edgeIndex++.ToString("00");

				edgeGameObjects.Add(new EdgeGameObject() {
					edge = edge,
					gameObject = go
				});
			}

			return edgeGameObjects;
		}