public IEnumerator GenerateTree() { // Variables that change per tile int tileDepth = 0; Stack <Hexigon> currentHexigonDepth = new Stack <Hexigon>(); Stack <Hexigon> nextHexigonDepth = new Stack <Hexigon>(); Hexigon root = new Hexigon(Hexigon.Layouts[0], PositionToHash(Vector3.zero), Vector3.zero, tileDepth); currentHexigonDepth.Push(root); root = ApplyLayoutConstraints(root); SpawnHexigonWorldObject(root); tileDepth++; while (tileDepth < 10) { Hexigon cur = currentHexigonDepth.Pop(); for (int i = 0; i < cur.layout.connections.Length; i++) { Hexigon.ConnectorType connectionType = cur.layout.connections[i]; if (!CheckLegalOutput(i, connectionType)) { continue; } Vector3 worldPosition = GenerateHexigonWorldPosition(cur, i); int hash = PositionToHash(worldPosition, true); Debug.Assert(cur.Hash != hash, "Current hash is equal to new tile hash!"); Hexigon next = new Hexigon(ChooseLegalLayout(hash, worldPosition), hash, worldPosition, tileDepth); next = ApplyLayoutConstraints(next); SpawnHexigonWorldObject(next); nextHexigonDepth.Push(next); yield return(new WaitForEndOfFrame()); } // Swap stacks if (currentHexigonDepth.Count < 1) { Stack <Hexigon> swapReference = currentHexigonDepth; currentHexigonDepth = nextHexigonDepth; nextHexigonDepth = swapReference; tileDepth++; } } }
protected Hexigon ApplyLayoutConstraints(Hexigon hexigon) { hexigon.layout.isFringe = false; layoutRequirements[hexigon.Hash] = hexigon.layout; // Surrounding tiles need rules for (int i = 0; i < hexigon.layout.connections.Length; i++) { Hexigon.ConnectorType connectionType = hexigon.layout.connections[i]; if (!CheckLegalOutput(i, connectionType)) { continue; } Vector3 worldPosition = GenerateHexigonWorldPosition(hexigon, i); int hash = PositionToHash(worldPosition); if (!layoutRequirements.ContainsKey(hash)) { layoutRequirements[hash] = new Hexigon.Layout(-1, new[] { 0, 0, 0, 0, 0, 0 }); } Hexigon.Layout result = layoutRequirements[hash]; if (result.isFringe) { result.AddConnection((i + 3) % 6, Hexigon.ConnectorType.INPUT); layoutRequirements[hash] = result; } Debug.DrawLine(hexigon.WorldPosition, worldPosition, Color.red, 100); Debug.DrawRay(worldPosition, Vector3.up, Color.green, 100); Debug.DrawRay(worldPosition, Vector3.up * 50, Color.white, 5); } return(hexigon); }
protected bool CheckLegalOutput(int direction, Hexigon.ConnectorType type) { return(type == Hexigon.ConnectorType.OUTPUT); }