/// <summary> /// GetTileAtPosition(Vector2) - Gets the positional data for a /// Tile within the Map object from the given position. Checks for whether /// the position given is a valid position within the Map; returns detail. /// </summary> /// <param name="position">(Vector2) - Potential absolute position (in pixels) within the Map object.</param> /// <returns>TilePositionDetail - Object for holding positional data for a Tile on the Map</returns> public TilePositionDetail GetTileAtPosition(Vector2 position) { // Object for holding positional detail for a Tile TilePositionDetail detail = new TilePositionDetail(); // get the positional index for finding the Tile int x = (int)position.X / tileWidth; int y = (int)position.Y / tileHeight; // Set detail Coordinates to the index (x, y) given by the position vector. detail.Coordinates = new Point(x, y); // if coordinates are NOT within the bounds of the Map, set IsValidPositon to false; return detail. if (x < 0 || y < 0 || x > mapTiles.GetUpperBound(0) || y > mapTiles.GetUpperBound(1)) { detail.IsValidPosition = false; // Coordinates NOT within the Map. return(detail); } // Tile found at position detail.IsValidPosition = true; // Coordinates ARE within the Map. detail.Tile = mapTiles[x, y]; // Update TilePositionDetail with found Tile. return(detail); // return detail }
public TilePositionDetail GetTileAtPosition(Vector2 position) { TilePositionDetail detail = new TilePositionDetail(); int x = (int)position.X / tileWidth; int y = (int)position.Y / tileHeight; detail.Coordinates = new Point(x, y); if (x < 0 || y < 0 || x > tiles.GetUpperBound(0) || y > tiles.GetUpperBound(1)) { detail.IsValidPosition = false; return(detail); } detail.IsValidPosition = true; detail.Tile = tiles[x, y]; return(detail); }