Example #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));
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
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));
        }
Example #5
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));
        }
Example #6
0
        public static MapSizeData CalculateMapCenterAndExtents(IEnumerable <Vector3Int> tiles, float yOffset = 0)
        {
            int minXOffset = int.MaxValue;
            int maxXOffset = int.MinValue;
            int minYOffset = int.MaxValue;
            int maxYOffset = int.MinValue;

            foreach (var coord in tiles)
            {
                Vector2Int offsetCoord = HexConverter.TileCoordToOffsetTileCoord(coord);
                if (offsetCoord.x < minXOffset)
                {
                    minXOffset = offsetCoord.x;
                }
                if (offsetCoord.x > maxXOffset)
                {
                    maxXOffset = offsetCoord.x;
                }
                if (offsetCoord.y < minYOffset)
                {
                    minYOffset = offsetCoord.y;
                }
                if (offsetCoord.y > maxYOffset)
                {
                    maxYOffset = offsetCoord.y;
                }
            }

            Vector3 worldMin = HexConverter.OffsetTileCoordToCartesianCoord(new Vector2Int(minXOffset, minYOffset));
            Vector3 worldMax = HexConverter.OffsetTileCoordToCartesianCoord(new Vector2Int(maxXOffset, maxYOffset));

            Vector3 center  = ((worldMin + worldMax) / 2);
            Vector3 extents = new Vector3((worldMax.x - worldMin.x) / 2f, 0, (worldMax.z - worldMin.z) / 2f);

            MapSizeData mapSizeData = new MapSizeData(minXOffset, maxXOffset, minYOffset, maxYOffset, center, extents);

            return(mapSizeData);
        }
Example #7
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();
        }
Example #8
0
        /// <summary>
        /// updates all the mouse position data
        /// </summary>
        private void UpdatePlayerPositionData()
        {
            CursorIsOnMap = false;
            CartesianCoordInfiniteGrid = transform.position;
            CartesianCoordWrapped      = transform.position;

            CubeCoordRaw = HexConverter.CartesianCoordToTileCoord(CartesianCoordInfiniteGrid);
            TileCoord    = CubeCoordRaw;

            OffsetCoordInfiniteGrid = HexConverter.CartesianCoordToOffsetCoord(CartesianCoordInfiniteGrid);
            OffsetCoord             = OffsetCoordInfiniteGrid;

            ClosestEdgeCoordInfiniteGrid = HexConverter.CartesianCoordToClosestEdgeCoord(CartesianCoordInfiniteGrid);
            ClosestEdgeCoord             = ClosestEdgeCoordInfiniteGrid;

            ClosestCornerCoordInfiniteGrid = HexConverter.CartesianCoordToClosestCornerCoord(CartesianCoordInfiniteGrid);
            ClosestCornerCoord             = ClosestCornerCoordInfiniteGrid;

            //SelectionRay = Camera.main.ScreenPointToRay(Input.mousePosition);


            if (hexMap != null)
            {
                if (hexMap.CoordinateWrapper != null)
                {
                    CartesianCoordWrapped = hexMap.CoordinateWrapper.WrapCartesianCoordinate(CartesianCoordInfiniteGrid);
                }

                CursorIsOnMap = hexMap.GetTilePosition.IsInputCoordinateOnMap(CartesianCoordWrapped);

                TileCoord          = HexConverter.CartesianCoordToTileCoord(CartesianCoordWrapped);
                OffsetCoord        = HexConverter.TileCoordToOffsetTileCoord(TileCoord);
                ClosestEdgeCoord   = HexConverter.CartesianCoordToClosestEdgeCoord(CartesianCoordWrapped);
                ClosestCornerCoord = HexConverter.CartesianCoordToClosestCornerCoord(CartesianCoordWrapped);
            }
        }