Exemple #1
0
        /// Returns all tiles within a certain hex-grid manhattan distance of the origin
        public static List <Vector2Int> GetAllWithinManhattanRange(Vector3Int origin, int range, bool includeSelf)
        {
            //Performance Tip if this turns out to be a bottleNeck in your games Performance: precalculate all the offsets for range 1,2,3,4,5,6... etc just once and store them in a list or dictionary
            //Don't optimize it when not needed though, keeping things simple should be the priority
            List <Vector2Int> positions = new List <Vector2Int>();

            int minX = origin.x - range;
            int maxX = origin.x + range;
            int minY = origin.y - range;
            int maxY = origin.y + range;

            for (int x = minX; x <= maxX; x++)
            {
                for (int y = minY; y <= maxY; y++)
                {
                    int z = -x - y;
                    if (Mathf.Abs(z - origin.z) > range)
                    {
                        continue;
                    }
                    positions.Add(HexConversions.CubeCoordToOffsetCoord(new Vector3Int(x, y, z)));
                }
            }

            if (!includeSelf)
            {
                positions.Remove(HexConversions.CubeCoordToOffsetCoord(origin));
            }

            return(positions);
        }
Exemple #2
0
        private GameObject[,] GenerateTiles()
        {
            GameObject[,] tiles = new GameObject[mapSize.x, mapSize.y];

            for (int y = 0; y < mapSize.y; ++y)
            {
                for (int x = 0; x < mapSize.x; ++x)
                {
                    // TODO - Deal with terrain randomization here

                    Vector2Int offsetCoordinates = new Vector2Int(x, y);
                    Vector3Int cubeCoordinates   = HexConversions.OffsetCoordToCubeCoord(offsetCoordinates);
                    Vector3    worldCoordinates  = HexConversions.OffsetCoordToWorldPosition(offsetCoordinates);

                    // Create the tile itself in the world view and assign its variables
                    GameObject tile = GameObject.Instantiate(tilePrefab);
                    tile.transform.SetParent(transform, true);
                    tile.transform.localPosition = worldCoordinates;
                    tile.name = cubeCoordinates.ToString();
                    ((Tile)tile.GetComponent(typeof(Tile))).cubeCoordinates = cubeCoordinates;

                    // Add generated tile to array
                    tiles[x, y] = tile;
                }
            }

            return(tiles);
        }
Exemple #3
0
        public void HightlightCurrentHex(Color color)
        {
            Vector2Int hexPos = HexConversions.CubeCoordToOffsetCoord(curPosition);
            Tile       tile   = (Tile)gameManager.map.Tiles[hexPos.x, hexPos.y].GetComponent(typeof(Tile));

            tile.HightlightHex(color);
        }
Exemple #4
0
        /// Returns all tiles which have a specific distance to the origin. Thickness goes inwards
        public static List <Vector3Int> GetRing(Vector3Int origin, int radius, int thickness)
        {
            //This is also not the most performant way to do it but again for almost all use cases it should be absolutely no issue.
            //If you happen to need a faster solution, again just precalculate the offets for each range once, store it in a list or dictionary and used that stored data
            List <Vector3Int> ring = new List <Vector3Int>();
            List <Vector2Int> allInManhattanrange = GetAllWithinManhattanRange(origin, radius, false);

            foreach (var v in allInManhattanrange)
            {
                if (Distance(origin, HexConversions.OffsetCoordToCubeCoord(v)) > radius - thickness)
                {
                    ring.Add(HexConversions.OffsetCoordToCubeCoord(v));
                }
            }

            return(ring);
        }
Exemple #5
0
 // Give a unitgroup a hex to move to and it should issue orders to all of its sub-units to move there
 // after checking if the move is possible
 // TODO - More sophisticated checking if a move is valid
 public bool MoveTo(Vector3Int destinationHex)
 {
     // Check if the destination is within the UnitGroup's speed
     if (Hex.Distance(curPosition, destinationHex) <= this.Speed)
     {
         // Set new current hex and move all sub-units to the new hex
         // TODO - for now, the code works for one unit only.  This function will need to tell each unit where to go so it looks nice in the hex.
         HightlightCurrentHex(Color.white);
         HighlightMovementRange(Color.white);
         curPosition             = destinationHex;
         this.transform.position = HexConversions.CubeCoordToWorldPosition(destinationHex);
         List <Vector3> unitPositions = getUnitPositions(units.Count);
         for (int i = 0; i < units.Count; ++i)
         {
             units[i].MoveTo(this.transform.position + unitPositions[i]);
         }
         return(true);
     }
     else
     {
         Debug.Log("Squad attempted to move to a position outside of its range of movement");
         return(false);
     }
 }
Exemple #6
0
        public Tile GetTileData(Vector3Int coord)
        {
            Vector2Int offsetCoord = HexConversions.CubeCoordToOffsetCoord(coord);

            return((Tile)Tiles[offsetCoord.x, offsetCoord.y].GetComponent(typeof(Tile)));
        }