// attempt to place the specified terrain tile at the specified (x,z) position, with consistency checks bool TryToAddTerrain(int tileX, int tileZ, Terrain terrain) { bool added = false; if (terrain != null) { Terrain existing = GetTerrain(tileX, tileZ); if (existing != null) { // already a terrain in the location -- check it is the same tile if (existing != terrain) { // ERROR - multiple different terrains at the same coordinate! m_errorCode |= TerrainMapStatusCode.Overlapping; } } else { // add terrain to the terrain map AddTerrainInternal(tileX, tileZ, terrain); added = true; } } return(added); }
void ValidateTerrain(int tileX, int tileZ) { Terrain terrain = GetTerrain(tileX, tileZ); if (terrain != null) { // grab neighbors (according to grid) Terrain left = GetTerrain(tileX - 1, tileZ); Terrain right = GetTerrain(tileX + 1, tileZ); Terrain top = GetTerrain(tileX, tileZ + 1); Terrain bottom = GetTerrain(tileX, tileZ - 1); // check edge alignment { if (left) { if (!Mathf.Approximately(terrain.transform.position.x, left.transform.position.x + left.terrainData.size.x) || !Mathf.Approximately(terrain.transform.position.z, left.transform.position.z)) { // unaligned edge, tile doesn't match expected location m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch; } } if (right) { if (!Mathf.Approximately(terrain.transform.position.x + terrain.terrainData.size.x, right.transform.position.x) || !Mathf.Approximately(terrain.transform.position.z, right.transform.position.z)) { // unaligned edge, tile doesn't match expected location m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch; } } if (top) { if (!Mathf.Approximately(terrain.transform.position.x, top.transform.position.x) || !Mathf.Approximately(terrain.transform.position.z + terrain.terrainData.size.z, top.transform.position.z)) { // unaligned edge, tile doesn't match expected location m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch; } } if (bottom) { if (!Mathf.Approximately(terrain.transform.position.x, bottom.transform.position.x) || !Mathf.Approximately(terrain.transform.position.z, bottom.transform.position.z + bottom.terrainData.size.z)) { // unaligned edge, tile doesn't match expected location m_errorCode |= TerrainMapStatusCode.EdgeAlignmentMismatch; } } } } }
void AddTerrainInternal(int x, int z, Terrain terrain) { if (m_terrainTiles.Count == 0) { m_patchSize = terrain.terrainData.size; } else { // check consistency with existing terrains if (terrain.terrainData.size != m_patchSize) { // ERROR - terrain is not the same size as other terrains m_errorCode |= TerrainMapStatusCode.SizeMismatch; } } m_terrainTiles.Add(new TerrainTileCoord(x, z), terrain); }
public TerrainMap() { m_errorCode = TerrainMapStatusCode.OK; m_terrainTiles = new Dictionary <TerrainTileCoord, Terrain>(); }