private AbstractTile InstantiateTile(GridData data, Transform parent, TileType type, Vector2Int gridPosition) { GameObject prefab = prefabsPerTileTypes.First(pair => pair.Key.Equals(type)).Value.GetRandomItem(); if (prefab == null) { throw new NullReferenceException($"A prefab for {type} is not assigned!"); } GameObject instance = Instantiate(prefab, parent); instance.transform.position += CalculatePosition(data, parent, gridPosition); instance.name = $"{type.ToString()} {gridPosition.ToString()}"; AbstractTile tile = instance.GetComponent <AbstractTile>(); if (tile == null) { throw new NullReferenceException($"Prefab {prefab.name} does not have an AbstractTileComponent attached to it!"); } tile.Initialize(gridPosition); return(tile); }
private void switchTile(uint x, uint y, GameObject type) { AbstractTile oldTile = tiles[x, y].GetComponent <AbstractTile>(); GameObject newTile = Instantiate(type, new Vector3(x, y, oldTile.elevation / 200.0f), Quaternion.identity) as GameObject; AbstractTile absTile = newTile.GetComponent <AbstractTile>(); absTile.Initialize(x, y, this, oldTile.elevation, SurfaceWater, oldTile.waterLevel); Destroy(tiles[x, y]); tiles[x, y] = newTile; }