private static Hex FlatOffset(int offset, OffsetCoordinates coord)
    {
        int q = coord.Column;
        int r = coord.Row - (int)((q + offset * (q & 1)) / 2);

        return(new Hex(q, r));
    }
    private static Hex PointyOffset(int offset, OffsetCoordinates coord)
    {
        int r = coord.Row;
        int q = coord.Column - (int)((r + offset * (r & 1)) / 2);

        return(new Hex(q, r));
    }
        private Color GetColor(bool performColorCheck, OffsetCoordinates offsetCoordinates)
        {
            if (performColorCheck)
            {
                var checkA = new OffsetCoordinates(offsetCoordinates.Col - 1, offsetCoordinates.Row);
                var checkB = new OffsetCoordinates(offsetCoordinates.Col - 1, offsetCoordinates.Row - 1);
                var checkC = new OffsetCoordinates(offsetCoordinates.Col, offsetCoordinates.Row - 1);

                var colA = HexagonDatabase.Instance[checkA].GetComponent <Hexagon>().Color;
                var colB = HexagonDatabase.Instance[checkB].GetComponent <Hexagon>().Color;
                var colC = HexagonDatabase.Instance[checkC].GetComponent <Hexagon>().Color;

                if (colA == colB)
                {
                    return(ColourDatabase.Instance.RandomColour(except: colA));
                }

                if (colB == colC)
                {
                    return(ColourDatabase.Instance.RandomColour(except: colB));
                }

                if (colA == colC)
                {
                    return(ColourDatabase.Instance.RandomColour(except: colA));
                }
            }

            return(ColourDatabase.Instance.RandomColour());
        }
        FindNeighbors(OffsetCoordinates commonSpot, OffsetCoordinates[] otherSpots)
        {
            var indirectNeighbors = new List <Group>();
            var directNeighbors   = new List <Group>();

            foreach (var g in GroupDatabase.Instance.Groups)
            {
                if (Utils.Contains(g, commonSpot))
                {
                    var otherSpotCount = otherSpots.Count(s => Utils.Contains(g, s));

                    switch (otherSpotCount)
                    {
                    case 0:
                        indirectNeighbors.Add(g);
                        break;

                    case 1:
                        directNeighbors.Add(g);
                        break;

//                    case 2:
//                        // if it contains both of the other spots, it is the group itself, not one of the neighbors.
//                        break;
                    }
                }
            }

            return(indirectNeighbors, directNeighbors);
        }
        /// <summary>
        /// Checks if there are any colours that exists two times in the <paramref name="group"/>.
        /// </summary>
        /// <param name="group">The group to check.</param>
        /// <param name="pairSpots">The spots where the color pair is located. (null if method returns false)</param>
        /// <param name="thirdSpot">The third commonSpot, i.e. the commonSpot that has a different color. (Charlie if method returns false)</param>
        /// <param name="foundColor">The colour that exists 2 times in this group. (Color.Clear if method returns false)</param>
        private bool HasTwoColourPair(Group group, out OffsetCoordinates[] pairSpots,
                                      out OffsetCoordinates thirdSpot, out Color foundColor)
        {
            var(ac, bc, cc) = Utils.GetColors(group);

            if (ac == bc)
            {
                thirdSpot  = group.Charlie;
                foundColor = ac;
                pairSpots  = new[] { group.Alpha, group.Bravo };
                return(true);
            }

            if (bc == cc)
            {
                thirdSpot  = group.Alpha;
                foundColor = bc;
                pairSpots  = new[] { group.Bravo, group.Charlie };
                return(true);
            }

            if (ac == cc)
            {
                thirdSpot  = group.Bravo;
                foundColor = ac;
                pairSpots  = new[] { group.Alpha, group.Charlie };
                return(true);
            }

            thirdSpot  = group.Charlie;
            foundColor = Color.clear;
            pairSpots  = null;
            return(false);
        }
Beispiel #6
0
 public void EqualOffsetcoord(string name, OffsetCoordinates a, OffsetCoordinates b)
 {
     if (!(a.Column == b.Column && a.Row == b.Row))
     {
         Assert.Fail(name);
     }
 }
    /// <summary>
    /// Return offset coordinates of the neigboring tile in the specified direction.
    /// </summary>
    /// <param name="hex">Hex base.</param>
    /// <param name="dir">Specified direction.</param>
    /// <param name="pairs">Coordinate pairs.</param>
    /// <returns></returns>
    public static OffsetCoordinates GetNeighborFromDir(Hex hex, CubeDirections dir, OffsetCoordinates pairs)
    {
        CubeCoordinates cubePairs = OffsetToCube(hex, pairs.col, pairs.row);

        switch (dir)
        {
        case CubeDirections.Left:
            return(CubeToOffset(hex, cubePairs.x - 1, cubePairs.y + 1, cubePairs.z));

        case CubeDirections.Right:
            return(CubeToOffset(hex, cubePairs.x + 1, cubePairs.y - 1, cubePairs.z));

        case CubeDirections.TopLeft:
            return(CubeToOffset(hex, cubePairs.x, cubePairs.y + 1, cubePairs.z - 1));

        case CubeDirections.BottomRight:
            return(CubeToOffset(hex, cubePairs.x, cubePairs.y - 1, cubePairs.z + 1));

        case CubeDirections.TopRight:
            return(CubeToOffset(hex, cubePairs.x + 1, cubePairs.y, cubePairs.z - 1));

        case CubeDirections.BottomLeft:
            return(CubeToOffset(hex, cubePairs.x - 1, cubePairs.y, cubePairs.z + 1));

        default:
            Debug.LogError("Some unknown CubeDirections enum received.");
            return(new OffsetCoordinates(-1, -1));
        }
    }
Beispiel #8
0
 public Group(OffsetCoordinates alpha, OffsetCoordinates bravo, OffsetCoordinates charlie,
              GroupOrientation orientation)
 {
     Alpha       = alpha;
     Bravo       = bravo;
     Charlie     = charlie;
     Orientation = orientation;
 }
Beispiel #9
0
        private IEnumerable <Group> AssembleHexagonGroups(int columnCount, int rowCount)
        {
            // 2-right even (a)

            for (int col = 0; col < columnCount - 1; col += 2)
            {
                for (int row = 0; row < rowCount - 1; row++)
                {
                    var alpha   = new OffsetCoordinates(col, row);
                    var bravo   = new OffsetCoordinates(col + 1, row + 1);
                    var charlie = new OffsetCoordinates(col + 1, row);

                    yield return(new Group(alpha, bravo, charlie, GroupOrientation.TwoRight));
                }
            }

            // 2-left even (b)

            for (int col = 0; col < columnCount - 1; col += 2)
            {
                for (int row = 0; row < rowCount - 1; row++)
                {
                    var alpha   = new OffsetCoordinates(col, row);
                    var bravo   = new OffsetCoordinates(col, row + 1);
                    var charlie = new OffsetCoordinates(col + 1, row + 1);

                    yield return(new Group(alpha, bravo, charlie, GroupOrientation.TwoLeft));
                }
            }

            // 2-right odd (c)

            for (int col = 1; col < columnCount - 1; col += 2)
            {
                for (int row = 1; row < rowCount; row++)
                {
                    var alpha   = new OffsetCoordinates(col, row);
                    var bravo   = new OffsetCoordinates(col + 1, row);
                    var charlie = new OffsetCoordinates(col + 1, row - 1);

                    yield return(new Group(alpha, bravo, charlie, GroupOrientation.TwoRight));
                }
            }

            // 2-left odd (d)

            for (int col = 1; col < columnCount - 1; col += 2)
            {
                for (int row = 0; row < rowCount - 1; row++)
                {
                    var alpha   = new OffsetCoordinates(col, row);
                    var bravo   = new OffsetCoordinates(col, row + 1);
                    var charlie = new OffsetCoordinates(col + 1, row);

                    yield return(new Group(alpha, bravo, charlie, GroupOrientation.TwoLeft));
                }
            }
        }
Beispiel #10
0
        public Vector2f GetTopLeftCorner(AxialCoordinates position)
        {
            OffsetCoordinates offsetCoords = position;

            return(new Vector2f(
                       (offsetCoords.Row & 1) * HorizontalSize / 2 + HorizontalSize * offsetCoords.Column,
                       offsetCoords.Row * (VerticalSize - EdgeLength / 2)
                       ));
        }
        private IEnumerator Put(GameObject hex, OffsetCoordinates coords)
        {
            // set in hexagon database
            HexagonDatabase.Instance[coords] = hex;

            // sync the position of the GameObject
            yield return
                (hex.GetComponent <Hexagon>().MoveTo(coords.ToUnity(), 0.25f));
        }
        private void Explode(OffsetCoordinates coords)
        {
            var hex = HexagonDatabase.Instance[coords];

            HexagonDatabase.Instance.MarkAsDestroyed(coords);
            hex.GetComponent <Hexagon>().ExplodeSelf();

            ScoreDatabase.Instance.OnHexagonExploded();
        }
Beispiel #13
0
    public HexCell GetCell(CubeCoordinates coordinates)
    {
        int x = OffsetCoordinates.FromCubeCoordinates(coordinates).X;
        int z = OffsetCoordinates.FromCubeCoordinates(coordinates).Z;

        if (z < 0 || z > cellCountZ - 1 || x < 0 || x > cellCountX - 1)
        {
            return(null);
        }
        return(cells[x, z]);
    }
Beispiel #14
0
        private void Place(OffsetCoordinates position, TerrainFactory terrainFactory)
        {
            var field = new Field(
                world: this,
                position: position
                );

            field.Create(terrainFactory);

            this[position] = field;
        }
Beispiel #15
0
        private static IEnumerator Shift(int col, int row, int shiftCount, GameObject hex, Action callback)
        {
            Utils.LogConditional($"{nameof(GridShifter)}.{nameof(Shift)}: shifting... " +
                                 $"pos: [{col}, {row}] shiftCount: {shiftCount} {nameof(hex)}: {hex}");

            // data shift can be instant, nothing will/should interfere
            HexagonDatabase.Swap(col, row, row - shiftCount);

            var newCoords = new OffsetCoordinates(col, row - shiftCount);

            yield return
                (hex.GetComponent <Hexagon>().MoveTo(newCoords.ToUnity(), 0.5f));

            callback();
        }
Beispiel #16
0
        private void BuildHexagonGrid(int columnCount, int rowCount)
        {
            for (var col = 0; col < columnCount; col++)
            {
                for (var row = 0; row < rowCount; row++)
                {
                    var performColorCheck = (col > 0) && (row > 0);

                    var offsetCoordinates = new OffsetCoordinates(col, row);
                    var hex = HexagonCreator.Instance.CreateHexagon(offsetCoordinates, false, performColorCheck);
                    hex.name = $"({col}, {row})";
                    HexagonDatabase.Instance[offsetCoordinates] = hex;
                }
            }
        }
Beispiel #17
0
        protected override void OnPaint(PaintEventArgs e)
        {
            var g = e.Graphics;

            for (var x = 0; x < 6; x++)
            {
                for (var y = 0; y < 6; y++)
                {
                    var offset = new OffsetCoordinates(x, y);
                    var cube   = _grid.ToCubeCoordinates(offset);
                    var points = _grid.PolygonCorners(cube).Select(p => new System.Drawing.Point((int)p.X, (int)p.Y)).ToList();
                    points.Add(points[0]);
                    g.DrawLines(Pens.Brown, points.ToArray());
                }
            }
        }
        /// <summary>
        /// DOES NOT REGISTER THE NEW HEXAGON WITH <see cref="HexagonDatabase"/>. MAKE IT YOURSELF.
        /// </summary>
        public GameObject CreateHexagon(OffsetCoordinates offsetCoordinates, bool isBomb, bool performColorCheck)
        {
            var prefab = isBomb ? PrefabDatabase.Instance.BombHexagon : PrefabDatabase.Instance.Hexagon;

            var newHexagon = Instantiate(prefab, transform);

            newHexagon.transform.position = offsetCoordinates.ToUnity();


            var colour = GetColor(performColorCheck, offsetCoordinates);


            newHexagon.GetComponent <Hexagon>().SetColor(colour);


            return(newHexagon);
        }
Beispiel #19
0
    public void InitFlat(Hex hex, bool even = true)
    {
        Hex = hex;
        //gameObject.transform.rotation = Quaternion.Euler(0,0,30);
        if (even)
        {
            _coordinates = OffsetCoordinates.EvenFlatOffset(hex);
        }
        else
        {
            _coordinates = OffsetCoordinates.OddFlatOffset(hex);
        }
        var x = WorldY % 2 == 0 ? Mathf.Sqrt(3f) * WorldX : Mathf.Sqrt(3f) * (WorldX - 1) + (Mathf.Sqrt(3f) / 2f);
        var y = WorldX % 2 == 0 ? 3f * WorldY / 2f : 3f * WorldY;

        transform.position = new Vector3(x, 3f * WorldY / 2f);
    }
Beispiel #20
0
        public void TestConversionRoundtrip()
        {
            CubeCoordinates   a = new CubeCoordinates(3, 4, -7);
            OffsetCoordinates b = new OffsetCoordinates(1, -3);
            var flatOdd         = HexLayoutFactory.CreateFlatHexLayout(new Point(10, 15), new Point(35, 71), Offset.Odd);
            var flatEven        = HexLayoutFactory.CreateFlatHexLayout(new Point(10, 15), new Point(35, 71), Offset.Even);
            var pointyOdd       = HexLayoutFactory.CreatePointyHexLayout(new Point(10, 15), new Point(35, 71), Offset.Odd);
            var pointyEven      = HexLayoutFactory.CreatePointyHexLayout(new Point(10, 15), new Point(35, 71), Offset.Even);

            EqualHex("conversion_roundtrip even-Q", a, flatEven.ToCubeCoordinates(flatEven.ToOffsetCoordinates(a)));
            EqualOffsetcoord("conversion_roundtrip even-Q", b, flatEven.ToOffsetCoordinates(flatEven.ToCubeCoordinates(b)));
            EqualHex("conversion_roundtrip odd-Q", a, flatOdd.ToCubeCoordinates(flatOdd.ToOffsetCoordinates(a)));
            EqualOffsetcoord("conversion_roundtrip odd-Q", b, flatOdd.ToOffsetCoordinates(flatOdd.ToCubeCoordinates(b)));
            EqualHex("conversion_roundtrip even-R", a, pointyEven.ToCubeCoordinates(pointyEven.ToOffsetCoordinates(a)));
            EqualOffsetcoord("conversion_roundtrip even-R", b, pointyEven.ToOffsetCoordinates(pointyEven.ToCubeCoordinates(b)));
            EqualHex("conversion_roundtrip odd-R", a, pointyOdd.ToCubeCoordinates(pointyOdd.ToOffsetCoordinates(a)));
            EqualOffsetcoord("conversion_roundtrip odd-R", b, pointyOdd.ToOffsetCoordinates(pointyOdd.ToCubeCoordinates(b)));
        }
Beispiel #21
0
        private IEnumerator Refill(int col, int row, int refillSpawnRow, Action callback)
        {
            var fillTarget = new OffsetCoordinates(col, row);

            var hex = HexagonCreator.Instance.CreateHexagon(new OffsetCoordinates(col, refillSpawnRow),
                                                            _shouldSpawnBomb, false);

            if (_shouldSpawnBomb)
            {
                _shouldSpawnBomb = false;
            }

            // data shift can be instant, nothing will/should interfere
            HexagonDatabase.Instance[fillTarget] = hex;

            yield return
                (hex.GetComponent <Hexagon>().MoveTo(fillTarget.ToUnity(), 0.5f));

            callback();
        }
Beispiel #22
0
    public void InitPointy(Hex hex, bool even = true)
    {
        Hex = hex;
        float x;

        if (even)
        {
            _coordinates = OffsetCoordinates.EvenPointyOffset(hex);
            x            = WorldY % 2 == 0 ? Mathf.Sqrt(3f) * WorldX :
                           Mathf.Sqrt(3f) * (WorldX - 1) + (Mathf.Sqrt(3f) / 2f);
        }
        else
        {
            _coordinates = OffsetCoordinates.OddPointyOffset(hex);
            x            = WorldY % 2 == 0 ? Mathf.Sqrt(3f) * WorldX :
                           Mathf.Sqrt(3f) * (WorldX - 1) + (Mathf.Sqrt(3f) / 2f);
        }
        var y = WorldX % 2 == 0 ? 3f * WorldY / 2f : 3f * WorldY;

        transform.position = new Vector3(x, 3f * WorldY / 2f);
    }
Beispiel #23
0
 public static bool Contains(Group g, OffsetCoordinates oc)
 {
     return(g.Alpha == oc || g.Bravo == oc || g.Charlie == oc);
 }
 public GameObject this[OffsetCoordinates offsetCoordinates]
 {
     get => HexagonGrid[offsetCoordinates.Col, offsetCoordinates.Row];
Beispiel #25
0
 public Field this[OffsetCoordinates position]
 {
     get { return(fields[position.Column][position.Row]); }
     set { fields[position.Column][position.Row] = value; }
 }
Beispiel #26
0
        public void Mock(IEnumerable <Player> players)
        {
            Size   = new Vector2i(10, 10);
            fields = new Field[Size.X][];
            grid   = new List <List <VertexArray> >();

            for (int i = 0; i < Size.X; i++)
            {
                fields[i] = new Field[Size.Y];
                grid.Add(new List <VertexArray>());
                for (int j = 0; j < Size.Y; j++)
                {
                    var position = new OffsetCoordinates(i, j);
                    if (i == 7 && j == 8)
                    {
                        Place(position, Forest.Factory);
                    }
                    else if (i == 8 && j >= 7 && j <= 9)
                    {
                        Place(position, Hill.Factory);
                    }
                    else if (i == 5 && j == 2)
                    {
                        Place(position, Grass.Factory);
                    }
                    else if (i >= 3 && i <= 7 && j >= 1 && j <= 3)
                    {
                        Place(position, Water.Factory);
                    }
                    else if (i >= 4 && i <= 6 && j >= 4 && j <= 5)
                    {
                        Place(position, Desert.Factory);
                    }
                    else
                    {
                        Place(position, Grass.Factory);
                    }

                    var apexes = Model.GetApexesPositions(new Vector2f(
                                                              position.Column * Model.HorizontalSize + (Model.HorizontalSize * 0.5f * (position.Row & 1)),
                                                              position.Row * (Model.VerticalSize - Model.EdgeLength / 2)
                                                              ));

                    var vertex = new VertexArray(PrimitiveType.LinesStrip);
                    var color  = Color.Red;
                    for (uint k = 0; k < apexes.Length; k++)
                    {
                        vertex.Append(new Vertex(apexes[k], color));
                    }
                    vertex.Append(new Vertex(apexes[0], color));
                    grid[i].Add(vertex);
                }
            }

            var unitPosition1 = new OffsetCoordinates(7, 7);
            var unitPosition2 = new OffsetCoordinates(5, 7);
            var unitPosition3 = new OffsetCoordinates(7, 8);
            var unitPosition4 = new OffsetCoordinates(5, 8);
            var unitPosition5 = new OffsetCoordinates(6, 7);
            var unitPosition6 = new OffsetCoordinates(6, 8);

            var improvementPosition = new OffsetCoordinates(6, 6);
            var depositPosition     = new OffsetCoordinates(8, 8);

            var player1 = players.First();
            var player2 = players.Skip(1).First();

            this[unitPosition1].Create(Archer.Factory, player1);
            this[unitPosition2].Create(Archer.Factory, player2);
            this[unitPosition3].Create(Swordsman.Factory, player1);
            this[unitPosition4].Create(Swordsman.Factory, player2);
            this[unitPosition5].Create(Worker.Factory, player1);
            this[unitPosition6].Create(Worker.Factory, player2);
            this[improvementPosition].Create(Farm.Factory, player1);
            this[depositPosition].Create(Iron.Factory);
        }
 public static Hex OddPointyOffset(OffsetCoordinates coord) => PointyOffset(-1, coord);
Beispiel #28
0
 public Field this[OffsetCoordinates index]
 {
     get { return(fields[index.Column][index.Row]); }
 }
Beispiel #29
0
 public bool Contains(OffsetCoordinates position) => position.Column >= 0 && position.Row >= 0 && position.Column < Size.X && position.Row < Size.Y;
Beispiel #30
0
 public bool Contains(OffsetCoordinates position)
 {
     return(position.Column < Size.X && position.Row < Size.Y && position.Column >= 0 && position.Row >= 0);
 }