Пример #1
0
        /// <summary>
        /// Returns the closest periodic coordinate of the input tile coordinate - Assumes coordinates are not further than 1 map size away from actual map
        public override Vector3Int WrapTileCoordinate(Vector3Int position)
        {
            Vector2Int offsetPosition = HexConverter.TileCoordToOffsetTileCoord(position); //We need to use the upperBound parameter instead of using the mapsize Property because for edges we need mapsize.y*2

            offsetPosition.x = (offsetPosition.x % mapSize.x);
            if (offsetPosition.x < 0)
            {
                offsetPosition.x = mapSize.x + offsetPosition.x;
            }
            return(HexConverter.OffsetTileCoordToTileCoord(offsetPosition));
        }
Пример #2
0
        /// <summary>
        /// returns indexed positions which form a rectangular shaped map with the specified width(x) and length(y).
        /// Most common map shape for periodic maps
        /// </summary>
        /// <remarks>
        /// ![Rectangular map. Mapsize: x = 9 and y = 9](RectangularMap.png)
        /// </remarks>
        public static Dictionary <Vector3Int, int> CreateRectangularShapedMap(Vector2Int mapSize)
        {
            Vector3IntEqualityComparer   vector3IntEqualityComparer = new Vector3IntEqualityComparer();
            Dictionary <Vector3Int, int> TileToIndexMap             = new Dictionary <Vector3Int, int>(vector3IntEqualityComparer);
            int idx = 0;

            for (int y = 0; y < mapSize.y; y++)
            {
                for (int x = 0; x < mapSize.x; x++)
                {
                    Vector3Int cubeTileCoord = HexConverter.OffsetTileCoordToTileCoord(new Vector2Int(x, y));
                    TileToIndexMap.Add(cubeTileCoord, idx);
                    idx++;
                }
            }
            return(TileToIndexMap);
        }
Пример #3
0
        /// <summary>
        /// returns indexed positions which form a equilateral triangular map with the specified side length
        /// </summary>
        /// <remarks>
        /// ![Triangular map with with sidelength of 9](TriangularMap.png)
        /// </remarks>
        public static Dictionary <Vector3Int, int> CreateTriangularShapedMap(int sideLength)
        {
            Vector3IntEqualityComparer   vector3IntEqualityComparer = new Vector3IntEqualityComparer();
            Dictionary <Vector3Int, int> TileToIndexMap             = new Dictionary <Vector3Int, int>(vector3IntEqualityComparer);

            int idx = 0;

            for (int y = 0; y < sideLength; y++)
            {
                for (int x = y / 2; x < (sideLength - (y + 1) / 2); x++)
                {
                    Vector3Int cubeTileCoord = HexConverter.OffsetTileCoordToTileCoord(new Vector2Int(x, y));
                    TileToIndexMap.Add(cubeTileCoord, idx);
                    idx++;
                }
            }
            return(TileToIndexMap);
        }
Пример #4
0
        /// <summary>
        /// returns the closest periodic edge position of the input edge position - Assumes coordinates are not further than 1 map size away from actual map
        /// </summary>
        public override Vector3Int WrapEdgeCoordinate(Vector3Int position)
        {
            Vector2Int offsetPosition = HexConverter.TileCoordToOffsetTileCoord(position);

            offsetPosition.x = (offsetPosition.x % (mapSize.x * 2));
            if (offsetPosition.x == (mapSize.x * 2) - 1)
            {
                offsetPosition.x = -1;
            }
            if (offsetPosition.x < -1)
            {
                offsetPosition.x = (mapSize.x * 2) + offsetPosition.x;
            }

            Vector3Int edgeCoord = HexConverter.OffsetTileCoordToTileCoord(offsetPosition);

            return(edgeCoord);
        }
Пример #5
0
        /// <summary>
        /// returns the closest periodic corner position of the input corner position - Assumes coordinates are not further than 1 map size away from actual map
        /// </summary>
        public override Vector3Int WrapCornerCoordinate(Vector3Int position)
        {
            //Debug.Log(mapSize.x);
            Vector2Int offsetPosition = HexConverter.TileCoordToOffsetTileCoord(position);

            Debug.Log(position);
            //Debug.Log(-2 / 3);
            offsetPosition.x = (offsetPosition.x % (mapSize.x * 3));
            if ((offsetPosition.x == (mapSize.x * 3) - 2))
            {
                offsetPosition.x = -2;
            }
            if (offsetPosition.x < -2)
            {
                offsetPosition.x = (mapSize.x * 3) + offsetPosition.x;
            }

            Vector3Int cornerCoord = HexConverter.OffsetTileCoordToTileCoord(offsetPosition);

            return(cornerCoord);
        }
Пример #6
0
        public override Vector3Int ShiftTargetToClosestPeriodicEdgePosition(Vector3Int origin, Vector3Int target)
        {
            Vector2Int originOffsetCoord = HexConverter.TileCoordToOffsetTileCoord(origin);
            Vector2Int targetOffsetCoord = HexConverter.TileCoordToOffsetTileCoord(target);
            int        distance          = Mathf.Abs(originOffsetCoord.x - targetOffsetCoord.x);

            if (distance * 2 <= mapSize.x * 2)
            {
                return(target);
            }

            if (originOffsetCoord.x < targetOffsetCoord.x) //target is right of the origin so we shift it left!
            {
                targetOffsetCoord.x -= mapSize.x * 2;
            }
            else
            {
                targetOffsetCoord.x += mapSize.x * 2;
            }
            return(HexConverter.OffsetTileCoordToTileCoord(targetOffsetCoord));
        }
Пример #7
0
        /// <summary>
        /// This returns the closest "virtual" position of target tile position on a wrapping map, intended to be used in distance calculations
        /// TODO ADD BETTER EXPLANATION
        /// </summary>
        public override Vector3Int ShiftTargetToClosestPeriodicTilePosition(Vector3Int origin, Vector3Int target)
        {
            Vector2Int originOffsetCoord = HexConverter.TileCoordToOffsetTileCoord(origin);
            Vector2Int targetOffsetCoord = HexConverter.TileCoordToOffsetTileCoord(target);
            int        distance          = Mathf.Abs(originOffsetCoord.x - targetOffsetCoord.x);

            if (distance * 2 <= mapSize.x)
            {
                return(target);
            }

            //now we check if the target is "right" or "left of the origin and shift its imaginary position to the opposite
            if (originOffsetCoord.x < targetOffsetCoord.x) //target is right of the origin so we shift it left!
            {
                targetOffsetCoord.x -= mapSize.x;
            }
            else
            {
                targetOffsetCoord.x += mapSize.x;  //target is left of the origin  so we shift it right
            }
            return(HexConverter.OffsetTileCoordToTileCoord(targetOffsetCoord));
        }
Пример #8
0
        public override Vector3Int ShiftTargetToClosestPeriodicCornerPosition(Vector3Int origin, Vector3Int target)
        {
            Vector2Int originOffsetCoord = HexConverter.TileCoordToOffsetTileCoord(origin);
            Vector2Int targetOffsetCoord = HexConverter.TileCoordToOffsetTileCoord(target);
            int        distance          = Mathf.Abs(originOffsetCoord.x - targetOffsetCoord.x);

            if (distance * 2 <= mapSize.x * 3)
            {
                return(target);
            }

            if (originOffsetCoord.x < targetOffsetCoord.x) //target is right of the origin so we shift it left!
            {
                targetOffsetCoord.x -= mapSize.x * 3;
            }
            else
            {
                targetOffsetCoord.x += mapSize.x * 3;
            }
            return(HexConverter.OffsetTileCoordToTileCoord(targetOffsetCoord));

            //throw new System.NotImplementedException();
        }