Exemplo n.º 1
0
    public static HexTile FindByOffset(OffsetCoordinate center, OffsetCoordinate offset)
    {
        int x = center.x + offset.x;
        int y = center.y + offset.y;

        return(FindByCoordinate(x, y));
    }
Exemplo n.º 2
0
 public void GenerateHexagons()
 {
     for (int i = 0; i < GameConfiguration.Instance.GetColumnCount(); i++)
     {
         for (int j = 0; j < GameConfiguration.Instance.GetRowCount(); j++)
         {
             OffsetCoordinate coord = new OffsetCoordinate(i, j);
             Hexagon          hexa  = PoolManager.Instance.GetNewHexagon();
             hexa.gameObject.SetActive(true);
             hexa.name = "[" + i + ", " + j + "]";
             hexa.transform.SetParent(parent);
             if (!hexa.IsEventSubbed())
             {
                 hexa.SetEventSubbedFlag(true);
                 hexa.OnExploded.AddListener(() => GameManager.Instance.OnHexagonExploded());
                 hexa.OnExploded.AddListener(() => PoolManager.Instance.DespawnHexagon(hexa));
             }
             hexa.transform.position = CoordToWorld(coord);
             if (i > 0)
             {
                 //avoid pre-matches at startup
                 SetDifferentColorToHexagon(hexa, coord);
             }
             else
             {
                 int colorIndex = ColorReferencer.Instance.GetRandomColorIndexExcepts();
                 hexa.SetColor(ColorReferencer.Instance.GetColorByIndex(colorIndex));
                 hexa.SetColorIndex(colorIndex);
             }
             SetHexagon(coord, hexa);
         }
     }
 }
Exemplo n.º 3
0
        PairedGroup GetPairedGroup(HexagonGroup group)
        {
            Hexagon a = HexagonManager.Instance.GetHexagon(group.A);
            Hexagon b = HexagonManager.Instance.GetHexagon(group.B);
            Hexagon c = HexagonManager.Instance.GetHexagon(group.C);

            if (a.IsSameColorWith(b))
            {
                OffsetCoordinate[] pairs = new OffsetCoordinate[2];
                pairs[0] = group.A;
                pairs[1] = group.B;
                OffsetCoordinate other = group.C;
                return(new PairedGroup(pairs, other, a.GetColorIndex()));
            }

            if (b.IsSameColorWith(c))
            {
                OffsetCoordinate[] pairs = new OffsetCoordinate[2];
                pairs[0] = group.B;
                pairs[1] = group.C;
                OffsetCoordinate other = group.A;
                return(new PairedGroup(pairs, other, b.GetColorIndex()));
            }

            if (c.IsSameColorWith(a))
            {
                OffsetCoordinate[] pairs = new OffsetCoordinate[2];
                pairs[0] = group.C;
                pairs[1] = group.A;
                OffsetCoordinate other = group.B;
                return(new PairedGroup(pairs, other, c.GetColorIndex()));
            }

            return(null);
        }
Exemplo n.º 4
0
    public static float GetDistance(OffsetCoordinate a, OffsetCoordinate b)
    {
        var ac = OffsetCoordToCubeCoord(a);
        var bc = OffsetCoordToCubeCoord(b);

        return(GetDistance(ac, bc));
    }
Exemplo n.º 5
0
    public static void OffsetToCube(ref OffsetCoordinate o, HexCellOrientation orientation, out HexCubeCoordinate c)
    {
        switch (orientation)
        {
        case HexCellOrientation.PointyOdd:
            PointyOddToCube(ref o, out c);
            break;

        case HexCellOrientation.PointyEven:
            PointyEvenToCube(ref o, out c);
            break;

        case HexCellOrientation.FlatOdd:
            FlatOddToCube(ref o, out c);
            break;

        case HexCellOrientation.FlatEven:
            FlatEvenToCube(ref o, out c);
            break;

        default:
            PointyOddToCube(ref o, out c);
            break;
        }
    }
Exemplo n.º 6
0
        private Hexagon SwapHexagonLocation(OffsetCoordinate oldCoord, OffsetCoordinate newCoord)
        {
            Hexagon hexOld = GetHexagon(oldCoord);

            SetHexagon(oldCoord, GetHexagon(newCoord));
            SetHexagon(newCoord, hexOld);
            return(hexOld);
        }
Exemplo n.º 7
0
 public void ExplodeHexagon(OffsetCoordinate coord)
 {
     if (_hexagons[coord.Column, coord.Row] == null)
     {
         return;
     }
     _hexagons[coord.Column, coord.Row].Explode();
     _hexagons[coord.Column, coord.Row] = null;
 }
Exemplo n.º 8
0
    public static OffsetCoordinate CubeCoordToOffsetCoord(CubeCoordinate cubeCoordinate, bool isEven)
    {
        OffsetCoordinate offset = new OffsetCoordinate();

        offset.y = cubeCoordinate.x;
        offset.x = isEven ?
                   cubeCoordinate.z + (cubeCoordinate.x - (cubeCoordinate.x & 1)) / 2 :
                   cubeCoordinate.z + (cubeCoordinate.x + (cubeCoordinate.x & 1)) / 2;
        return(offset);
    }
Exemplo n.º 9
0
        private Color32 GetColorForTerrainAtPoint(OffsetCoordinate point)
        {
            // map noise over a larger hex grid size to get larger-scale
            //  hexagons of terrain. looks kinda neato
            var largeHexGridRadius        = 1;
            var largeHexGridRelativeScale = largeHexGridRadius * 2 + 1;
            var scaled = point.ToCube().GetCoordInLargerHexGrid(largeHexGridRadius);
            var sample = SampleNoise(new Vector2(scaled.x, scaled.y) * largeHexGridRelativeScale);

            return(colorRamp.Evaluate(sample));
        }
Exemplo n.º 10
0
    public static CubeCoordinate OffsetCoordToCubeCoord(OffsetCoordinate offsetCoordinate)
    {
        CubeCoordinate cube = new CubeCoordinate();

        cube.x = offsetCoordinate.y;
        cube.z = offsetCoordinate.IsEven() ?
                 offsetCoordinate.x - (offsetCoordinate.y - (offsetCoordinate.y & 1)) / 2 :
                 offsetCoordinate.x - (offsetCoordinate.y + (offsetCoordinate.y & 1)) / 2;
        cube.y = -cube.x - cube.z;
        return(cube);
    }
Exemplo n.º 11
0
        private IList <ITilemapMember> GetListFromCoord(OffsetCoordinate coordinate)
        {
            var vectorInGrid = new Vector2Int(coordinate.column - tileMapMin.column, coordinate.row - tileMapMin.row);

            if (vectorInGrid.x < 0 || vectorInGrid.y < 0 || vectorInGrid.y >= tileMapHeight || vectorInGrid.x >= tileMapWidth)
            {
                return(null);
            }

            return(tileGrid[vectorInGrid.y][vectorInGrid.x]);
        }
Exemplo n.º 12
0
        void SpawnNewHexagons(int column, int emptyCount)
        {
            for (int i = 0; i < emptyCount; i++)
            {
                OffsetCoordinate coord  = new OffsetCoordinate(column, GameConfiguration.Instance.GetRowCount() - 1 - i);
                Vector2          target = CoordToWorld(coord);
                Hexagon          newHexagon;
                if (_nextHexagonIsBomb)
                {
                    newHexagon = PoolManager.Instance.GetNewBombHexagon();
                    BombHexagon bombHexagon = (BombHexagon)newHexagon;
                    bombHexagon.SetCounter(5);
                    bombHexagon.UpdateText();
                    //if detonate event not registered yet
                    if (bombHexagon.OnBombDetonated.GetPersistentEventCount() == 0)
                    {
                    }
                    if (!bombHexagon.IsEventSubbed())
                    {
                        bombHexagon.OnBombDetonated.AddListener(
                            () => GameManager.Instance.LoseGame(GameOverReason.BombExploded));
                        bombHexagon.OnExploded.AddListener(() => GameManager.Instance.OnHexagonExploded());
                        bombHexagon.OnExploded.AddListener(() => RemoveBombHexagonFromList(bombHexagon));
                        bombHexagon.OnExploded.AddListener(() => PoolManager.Instance.DespawnBombHexagon(bombHexagon));
                        bombHexagon.SetEventSubbedFlag(true);
                    }

                    bombHexagon.SetNewlySpawnedFlag(true);
                    _nextHexagonIsBomb = false;
                    _bombHexagons.Add(bombHexagon);
                }
                else
                {
                    newHexagon = PoolManager.Instance.GetNewHexagon();
                    if (!newHexagon.IsEventSubbed())
                    {
                        newHexagon.OnExploded.AddListener(() => PoolManager.Instance.DespawnHexagon(newHexagon));
                        newHexagon.OnExploded.AddListener(() => GameManager.Instance.OnHexagonExploded());
                        newHexagon.SetEventSubbedFlag(true);
                    }
                }
                newHexagon.name = "[" + coord.Column + ", " + coord.Row + "]";
                newHexagon.transform.SetParent(parent);
                newHexagon.transform.position = new Vector3(target.x, CameraManager.Instance.GetTopOfCamera() + 1.5f + ((emptyCount - i) * 0.7f));
                int colorIndex = ColorReferencer.Instance.GetRandomColorIndexExcepts();
                newHexagon.SetColorIndex(colorIndex);
                newHexagon.SetColor(ColorReferencer.Instance.GetColorByIndex(colorIndex));
                newHexagon.gameObject.SetActive(true);
                _hexagons[coord.Column, coord.Row] = newHexagon;
                StartCoroutine(newHexagon.Move(target, 10f));
            }
        }
Exemplo n.º 13
0
        public void CalculateGroups(int columnCount, int rowCount)
        {
            //two hexagons on right, only even numbered columns
            for (int column = 0; column < columnCount - 1; column += 2)
            {
                for (int row = 0; row < rowCount - 1; row++)
                {
                    var a = new OffsetCoordinate(column, row);
                    var b = new OffsetCoordinate(column + 1, row + 1);
                    var c = new OffsetCoordinate(column + 1, row);
                    _groupList.Add(new HexagonGroup(a, b, c, GroupRotation.TwoHexagonsRight));
                }
            }

            //two hexagons on right, only odd numbered columns
            for (int column = 1; column < columnCount - 1; column += 2)
            {
                for (int row = 1; row < rowCount; row++)
                {
                    var a = new OffsetCoordinate(column, row);
                    var b = new OffsetCoordinate(column + 1, row);
                    var c = new OffsetCoordinate(column + 1, row - 1);
                    _groupList.Add(new HexagonGroup(a, b, c, GroupRotation.TwoHexagonsRight));
                }
            }

            //two hexagons on left, only even numbered columns
            for (int column = 0; column < columnCount - 1; column += 2)
            {
                for (int row = 0; row < rowCount - 1; row++)
                {
                    var a = new OffsetCoordinate(column, row);
                    var b = new OffsetCoordinate(column, row + 1);
                    var c = new OffsetCoordinate(column + 1, row + 1);
                    _groupList.Add(new HexagonGroup(a, b, c, GroupRotation.TwoHexagonsLeft));
                }
            }

            //two hexagons on left, only odd numbered columns
            for (int column = 1; column < columnCount - 1; column += 2)
            {
                for (int row = 0; row < rowCount - 1; row++)
                {
                    var a = new OffsetCoordinate(column, row);
                    var b = new OffsetCoordinate(column, row + 1);
                    var c = new OffsetCoordinate(column + 1, row);
                    _groupList.Add(new HexagonGroup(a, b, c, GroupRotation.TwoHexagonsLeft));
                }
            }
        }
Exemplo n.º 14
0
        private IEnumerable <OffsetCoordinate> GetTilesInRectangle(HexTileMapManager tilemapManager)
        {
            var totalCells = new Vector2Int(tilemapManager.hexWidth, tilemapManager.hexHeight);//new Vector2Int(10, 10);
            var min        = tilemapManager.tileMapMin.ToAxial();

            for (var verticalIndex = 0; verticalIndex < totalCells.y; verticalIndex++)
            {
                for (var horizontalIndex = 0; horizontalIndex < totalCells.x; horizontalIndex++)
                {
                    var newPos = new OffsetCoordinate(horizontalIndex, verticalIndex);
                    yield return((newPos.ToAxial() + min).ToOffset());
                }
            }
        }
Exemplo n.º 15
0
        public void Awake()
        {
            coordinateSystem = new HexCoordinateSystem(hexRadius);
            tileMapMax       = new OffsetCoordinate(tileMapMin.column + hexWidth, tileMapMin.row + hexHeight);

            tileGrid = new IList <ITilemapMember> [hexHeight][];
            for (var verticalIndex = 0; verticalIndex < hexHeight; verticalIndex++)
            {
                tileGrid[verticalIndex] = new IList <ITilemapMember> [hexWidth];
                for (var horizontalIndex = 0; horizontalIndex < hexWidth; horizontalIndex++)
                {
                    tileGrid[verticalIndex][horizontalIndex] = new List <ITilemapMember>();
                }
            }

            tileMemberChunkMap = new Dictionary <AxialCoordinate, IList <ITilemapMember> >();
        }
        public void ShouldGeneratePathBetweenPoints()
        {
            var origin             = new OffsetCoordinate(-1, 4).ToAxial();
            var destination        = new OffsetCoordinate(3, -1).ToAxial();
            var expectedPathLength = 7;

            var coordSystem = new HexCoordinateSystem(2);

            var actualPath = coordSystem.GetRouteGenerator(origin, destination)
                             .ToList();

            Assert.AreEqual(expectedPathLength, actualPath.Count);

            foreach (var pair in actualPath.RollingWindow(2))
            {
                var distanceBetween = pair[0].DistanceTo(pair[1]);
                Assert.AreEqual(
                    1,
                    distanceBetween,
                    $"Jump between {pair[0]} and {pair[1]} should have been distance of 1 but was {distanceBetween}");
            }
        }
Exemplo n.º 17
0
        public void SetDifferentColorToHexagon(Hexagon hexagon, OffsetCoordinate coord)
        {
            OffsetCoordinate leftCoord      = new OffsetCoordinate(coord.Column - 1, coord.Row);
            OffsetCoordinate topLeftCoord   = new OffsetCoordinate(coord.Column - 1, coord.Row + 1);
            OffsetCoordinate lowerLeftCoord = new OffsetCoordinate(coord.Column - 1, coord.Row - 1);

            if (coord.Row == 0)
            {
                Hexagon leftHexa    = GetHexagon(leftCoord);
                Hexagon topLeftHexa = GetHexagon(topLeftCoord);
                int     colorIndex  = ColorReferencer.Instance.GetRandomColorIndexExcepts(leftHexa.GetColorIndex(), topLeftHexa.GetColorIndex());
                hexagon.SetColorIndex(colorIndex);
                hexagon.SetColor(ColorReferencer.Instance.GetColorByIndex(colorIndex));
            }
            else
            {
                Hexagon leftHexa      = GetHexagon(leftCoord);
                Hexagon lowerLeftHexa = GetHexagon(lowerLeftCoord);
                int     colorIndex    = ColorReferencer.Instance.GetRandomColorIndexExcepts(leftHexa.GetColorIndex(), lowerLeftHexa.GetColorIndex());
                hexagon.SetColorIndex(colorIndex);
                hexagon.SetColor(ColorReferencer.Instance.GetColorByIndex(colorIndex));
            }
        }
        public static void CalculateServiceRanges(HexTileMapManager manager)
        {
            //foreach (var market in allMarkets)
            //{
            //    var marketMember = market.GetComponentInParent<HexMember>();

            //    var myHexPosition = marketMember.PositionInTileMap;
            //    var effectiveRange = HexTileMapManager
            //        .GetPositionsWithinJumpDistance(myHexPosition, 2);
            //    market.myServiceRange = effectiveRange.ToArray();
            //}

            var allMarkets = manager.GetAllOfType <MarketBehavior>().ToList();

            Debug.Log($"Initializing {allMarkets.Count} markets");
            var marketPositions = allMarkets
                                  .Select(x => x.GetComponentInParent <HexMember>())
                                  .Select(x => x.PositionInTileMap)
                                  .ToList();

            var minimum      = manager.tileMapMin;
            var minimumAxial = minimum.ToAxial();
            var maximum      = manager.tileMapMax;

            var voroniData = VoroniTilingMapper.SetupVoroniMap(marketPositions, minimum, maximum);

            for (var row = 0; row < voroniData.Length; row++)
            {
                for (var col = 0; col < voroniData[row].Length; col++)
                {
                    var index      = voroniData[row][col];
                    var coordinate = new OffsetCoordinate(col, row).ToAxial() + minimumAxial;
                    allMarkets[index].myServiceRange.Add(coordinate.ToOffset());
                }
            }
        }
Exemplo n.º 19
0
    public HexTile[] GetNeighborsInRange(int radius)
    {
        HashSet <HexTile> tiles = new HashSet <HexTile>();

        for (int dx = -radius; dx <= radius; ++dx)
        {
            for (int dy = Mathf.Max(-radius, -dx - radius); dy <= Mathf.Min(radius, -dx + radius); ++dy)
            {
                int dz = -dx - dy;

                var     center     = new OffsetCoordinate(this.x, this.y);
                var     offsetCube = new CubeCoordinate(dx, dy, dz);
                HexTile neighbor   = HexTile.FindByOffset(center, HexUtils.CubeCoordToOffsetCoord(offsetCube, center.IsEven()));
                if (neighbor != null)
                {
                    tiles.Add(neighbor);
                }
            }
        }

        HexTile[] results = new HexTile[tiles.Count];
        tiles.CopyTo(results);
        return(results);
    }
Exemplo n.º 20
0
 public PairedGroup(OffsetCoordinate[] pairs, OffsetCoordinate other, int colorIndex)
 {
     Pairs = pairs;
     Other = other;
     ColorIndex = colorIndex;
 }
Exemplo n.º 21
0
 private static void PointyEvenToCube(ref OffsetCoordinate o, out HexCubeCoordinate c)
 {
     c.x = o.col - (o.row + (o.row & 1)) / 2;
     c.z = o.row;
     c.y = -c.x - c.z;
 }
Exemplo n.º 22
0
 private static void FlatOddToCube(ref OffsetCoordinate o, out HexCubeCoordinate c)
 {
     c.x = o.col;
     c.z = o.row - (o.col - (o.col & 1)) / 2;
     c.y = -c.x - c.z;
 }
Exemplo n.º 23
0
 private static void CubeToFlatEven(ref HexCubeCoordinate c, out OffsetCoordinate o)
 {
     o.row = c.x;
     o.col = c.z + (c.x + (c.x & 1)) / 2;
 }
Exemplo n.º 24
0
 public Hexagon GetHexagon(OffsetCoordinate coord)
 {
     return(_hexagons[coord.Column, coord.Row]);
 }
Exemplo n.º 25
0
 private static void CubeToPointyEven(ref HexCubeCoordinate c, out OffsetCoordinate o)
 {
     o.row = c.z;
     o.col = c.x + (c.z + (c.z & 1)) / 2;
 }
Exemplo n.º 26
0
    public static void CubeToOffset(ref HexCubeCoordinate c, HexCellOrientation orientation, out OffsetCoordinate o)
    {
        switch (orientation)
        {
        case HexCellOrientation.PointyOdd:
            CubeToPointyOdd(ref c, out o);
            break;

        case HexCellOrientation.PointyEven:
            CubeToPointyEven(ref c, out o);
            break;

        case HexCellOrientation.FlatOdd:
            CubeToFlatOdd(ref c, out o);
            break;

        case HexCellOrientation.FlatEven:
            CubeToFlatEven(ref c, out o);
            break;

        default:
            CubeToPointyOdd(ref c, out o);
            break;
        }
    }
Exemplo n.º 27
0
 public void GetOffsetCoordinates(HexCellOrientation orientation, out OffsetCoordinate o)
 {
     CubeToOffset(ref this, orientation, out o);
 }
Exemplo n.º 28
0
 public void SetHexagon(OffsetCoordinate coord, Hexagon hexagon)
 {
     _hexagons[coord.Column, coord.Row] = hexagon;
 }
Exemplo n.º 29
0
 public Vector3 CoordToWorld(OffsetCoordinate coord)
 {
     return(coord.ToPixel() - CenterOffset());
 }