protected void SpawnHexigonWorldObject(Hexigon hexigon)
 {
     Debug.DrawRay(hexigon.WorldPosition, Vector3.up * 50, Color.yellow, 5);
     if (hexigon.layout.primitiveLayoutType < 0)
     {
         Debug.LogError("Tile type not found");
         return;
     }
     GameObject hexi = Instantiate(tiles[hexigon.layout.primitiveLayoutType], hexigon.WorldPosition + Vector3.up * hexigon.Depth * HexigonWidthLong * 0.1f, Quaternion.AngleAxis((60 * -hexigon.layout.rotationOffset) + 30, Vector3.up));
 }
    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 int PositionToHash(Hexigon parentHexigon, int direction)
    {
        Vector3 position = GenerateHexigonWorldPosition(parentHexigon, direction);

        return(new Vector2Int(Mathf.RoundToInt(position.x), Mathf.RoundToInt(position.z)).GetHashCode());
    }
 protected Vector3 GenerateHexigonWorldPosition(Hexigon hexigon, int direction)
 {
     return(hexigon.WorldPosition + Quaternion.AngleAxis((60 * direction) + modelAngleFix, Vector3.up) * Vector3.forward * HexigonWidthShort);
 }