public Tile MatchClosestTile(TileInstance instance) { var minDistance = float.MaxValue; Tile closestTile = null; foreach (Tile tile in tiles) { // Check if the tile matches edges and tile flags if (!instance.MatchesEdges(tile, out var rotation) || !instance.MatchesTileFlags(tile)) { continue; } // Compute "distance" between tile dimensions and check if the tile is closer var distance = (tile.Size - instance.RotatedSize(rotation)).SqrMagnitude(); if (!(distance < minDistance)) { continue; } instance.matchedRotation = rotation; minDistance = distance; closestTile = tile; } return(closestTile); }
// public void MatchTile(TileInstance instance) // { // Tile matchedTile = null; // // switch (tileSizeMatch) // { // case SizeMatchMode.ClosestMatch: // matchedTile = tileset.MatchClosestTile(instance); // break; // case SizeMatchMode.Exact: // matchedTile = tileset.MatchTile(instance); // break; // default: // throw new ArgumentOutOfRangeException(nameof(tileSizeMatch), tileSizeMatch, null); // } // // instance.matchedTileId = matchedTile ? matchedTile.Id : -1; // } public void MatchTile(TileInstance instance) { var minDistance = float.MaxValue; var minRotation = int.MaxValue; Tile matchedTile = null; foreach (Tile tile in tileset) { // Check if the tile matches edges and tile flags if (!instance.MatchesEdges(tile, out var rotation) || !instance.MatchesTileFlags(tile)) { continue; } // Use exact matching if (tileSizeMatch == SizeMatchMode.Exact) { if (!instance.MatchesSize(tile, rotation)) { continue; } if (!preferFewerRotations) { matchedTile = tile; minRotation = rotation; break; } } else { // Use "closest match" matching // Compute "distance" between tile dimensions and check if the tile is closer var distance = (tile.Size - instance.RotatedSize(rotation)).SqrMagnitude(); if (distance > minDistance) { continue; } if (distance < minDistance) { matchedTile = tile; instance.matchedRotation = rotation; minRotation = rotation; minDistance = distance; continue; } minDistance = distance; } // Check if the current tile was matched with fewer rotations than the current matchedTile if (preferFewerRotations) { if (rotation < minRotation) { matchedTile = tile; minRotation = rotation; } } } instance.SetMatchedTile(matchedTile, minRotation); }
public Tile MatchTile(TileInstance instance) { // Simple match return(tiles.FirstOrDefault(instance.Matches)); }