/// <summary> /// Finds a Tile for every placed Section in the world. /// </summary> private void PlaceTiles() { TileSet.VariantKey searchKey = new TileSet.VariantKey(); searchKey.variant = "default"; List <Tile> searchResult = new List <Tile>(); foreach (Vector2Int position in grid.GetOccupiedCells()) { Profiler.BeginSample("Calculating tile information"); Profiler.BeginSample("Extracting section"); // Search by keys; guaranteed not null, so we cast from Section? -> Section Section section = (Section)grid[position]; Profiler.EndSample(); Profiler.BeginSample("Getting physical adjacencies"); // Find all the Sections that neighbor this one ("physical adjacency"). // Cached + optimized algorithm Connection physicalAdjacencies = grid.GetPhysicalAdjacencies(position); Profiler.EndSample(); Profiler.BeginSample("Resolving TileType"); // Knowing physical adjacencies, get the TileType and rotation (TileType type, int rotation) = TileType.GetTileType(physicalAdjacencies, section.internalConnections); Profiler.EndSample(); Profiler.BeginSample("Searching for Tiles"); // Hardcoded to have "default" variant for efficieny. // TODO replace searchKey.variant with Section.getVariant(), and logic // to fallback to a search for "default". searchKey.tileType = type; if (!tileSet.Get(searchKey, ref searchResult)) { Debug.LogError("Couldn't resolve Section into a Tile."); } Profiler.EndSample(); // Arbitrarily choose first one; TODO add smarter logic to choose which // variant to take (and add parameters for it) Tile chosenTile = searchResult[0]; chosenTile.section = section; Profiler.EndSample(); Profiler.BeginSample("Placing physical GO"); // Compute the GameObject values from the known parameters Vector3 worldPosition = new Vector3(distanceScale * position.x, 0, distanceScale * position.y); Quaternion worldRotation = Quaternion.AngleAxis(90f * rotation, Vector3.up); // Create a new instance of the Tile's prefab and place it in the world gameObjectLoader.Load(chosenTile.gameObject, worldPosition, worldRotation, tileScale * Vector3.one); Profiler.EndSample(); } gameObjectLoader.LastGameObjectSent(); }
public Connection GetPhysicalAdjacencies(Vector2Int position) => backingGrid.GetPhysicalAdjacencies(position);