private Rectangle GetVisibleTiles(ArrayGrid <Sprite> grid, Vector2 cameraPosition, Point viewportSize, Vector2 layerOffset) { var gridPosition = grid.Position + layerOffset; Rectangle visibleCoordinates; Point offset; switch (grid.Perspective) { case Perspective.Standard: visibleCoordinates = GetVisibleCartesianCoordinates(cameraPosition, viewportSize, grid.TileSize, 1); offset = Projector.PixelsToCartesian(gridPosition, grid.TileSize).ToPoint(); break; case Perspective.Isometric: visibleCoordinates = GetVisibleIsometricCoordinates(cameraPosition, viewportSize, grid.TileSize, 1); offset = Projector.PixelsToIsometric(gridPosition, grid.TileSize).ToPoint(); break; default: throw new InvalidOperationException($"Can not process {grid.Perspective} perspective."); } visibleCoordinates.Location -= offset; // Is this right? return(visibleCoordinates); }
/// <summary> /// Gets the range of coordinates currently in camera view. /// </summary> /// <returns>A rectangle containing all visible coordinates.</returns> protected static Rectangle GetVisibleCartesianCoordinates(Vector2 cameraPosition, Point viewportSize, Vector2 unit, int padding = 0) { var location = Projector.PixelsToCartesian(cameraPosition.ToPoint(), unit) - new Point(padding); var size = Projector.PixelsToCartesian(viewportSize, unit) + new Point(padding * 2); return(new Rectangle(location, size)); }