Example #1
0
        /// <summary>
        /// Given the specified coordinate in local / object space, this method returns the
        /// tile index that is located there.
        /// </summary>
        /// <param name="localPos"></param>
        /// <param name="pickMode">Specifies the desired behavior when attempting to get a tile outside the rendered area.</param>
        /// <returns></returns>
        public Point2 GetTileAtLocalPos(Vector2 localPos, TilePickMode pickMode)
        {
            // Early-out, if the specified local position is not within the tilemap rect
            Rect localRect = this.LocalTilemapRect;

            if (pickMode == TilePickMode.Reject && !localRect.Contains(localPos))
            {
                return(new Point2(-1, -1));
            }

            Tilemap tilemap   = this.ActiveTilemap;
            Tileset tileset   = tilemap != null ? tilemap.Tileset.Res : null;
            Point2  tileCount = tilemap != null ? tilemap.Size : Point2.Zero;
            Vector2 tileSize  = tileset != null ? tileset.TileSize : Tileset.DefaultTileSize;

            // Determine the tile index at the specified local position
            Point2 tileIndex = new Point2(
                (int)MathF.Floor((localPos.X - localRect.X) / tileSize.X),
                (int)MathF.Floor((localPos.Y - localRect.Y) / tileSize.Y));

            // Clamp or reject the tile index when required
            if (pickMode != TilePickMode.Free)
            {
                if (tileCount.X <= 0 || tileCount.Y <= 0)
                {
                    return(new Point2(-1, -1));
                }

                tileIndex = new Point2(
                    MathF.Clamp(tileIndex.X, 0, tileCount.X - 1),
                    MathF.Clamp(tileIndex.Y, 0, tileCount.Y - 1));
            }

            return(tileIndex);
        }
Example #2
0
        /// <summary>
        /// Given the specified coordinate in local / object space, this method returns the
        /// tile index that is located there.
        /// </summary>
        /// <param name="localPos"></param>
        /// <param name="pickMode">Specifies the desired behavior when attempting to get a tile outside the rendered area.</param>
        /// <returns></returns>
        public Point2 GetTileAtLocalPos(Vector2 localPos, TilePickMode pickMode)
        {
            // Early-out, if the specified local position is not within the tilemap rect
            Rect localRect = this.LocalTilemapRect;
            if (pickMode == TilePickMode.Reject && !localRect.Contains(localPos))
                return new Point2(-1, -1);

            Tilemap tilemap = this.ActiveTilemap;
            Tileset tileset = tilemap != null ? tilemap.Tileset.Res : null;
            Point2 tileCount = tilemap != null ? tilemap.Size : Point2.Zero;
            Vector2 tileSize = tileset != null ? tileset.TileSize : Tileset.DefaultTileSize;

            // Determine the tile index at the specified local position
            Point2 tileIndex = new Point2(
                (int)MathF.Floor((localPos.X - localRect.X) / tileSize.X),
                (int)MathF.Floor((localPos.Y - localRect.Y) / tileSize.Y));

            // Clamp or reject the tile index when required
            if (pickMode != TilePickMode.Free)
            {
                if (tileCount.X <= 0 || tileCount.Y <= 0)
                    return new Point2(-1, -1);

                tileIndex = new Point2(
                    MathF.Clamp(tileIndex.X, 0, tileCount.X - 1),
                    MathF.Clamp(tileIndex.Y, 0, tileCount.Y - 1));
            }

            return tileIndex;
        }
        private Point2 GetTileAtLocalPos(ICmpTilemapRenderer renderer, Point localPos, TilePickMode pickMode)
        {
            Component component = renderer as Component;
            Transform transform = component.GameObj.Transform;

            // Determine where the cursor is hovering in various coordinate systems
            Vector3 worldCursorPos = this.CameraComponent.GetSpaceCoord(new Vector3(localPos.X, localPos.Y, transform.Pos.Z));
            Vector2 localCursorPos = transform.GetLocalPoint(worldCursorPos.Xy);

            // Determine tile coordinates of the cursor
            return renderer.GetTileAtLocalPos(localCursorPos, pickMode);
        }