// Needed to compare two keys public override bool Equals(object obj) { if (obj == null) { return(false); } TerrainKey tk = obj as TerrainKey; return(x == tk.x && z == tk.z); }
Terrain getTerrain(TerrainKey tk) { if (!tk.isValid()) { return(null); } if (terrainDictionary[tk].gameObject == null) { return(null); } return(terrainDictionary[tk].terrain); }
void setNeighbors(TerrainKey tk) { if (!tk.isValid()) { return; } Terrain left = getTerrain(new TerrainKey(tk.x - 1, tk.z)); Terrain top = getTerrain(new TerrainKey(tk.x, tk.z + 1)); Terrain right = getTerrain(new TerrainKey(tk.x + 1, tk.z)); Terrain bottom = getTerrain(new TerrainKey(tk.x, tk.z - 1)); getTerrain(tk).SetNeighbors(left, top, right, bottom); }
// Called by player objects public static void reportLocation(Vector3 pos) { if (instance == null) { return; } TerrainKey key = new TerrainKey(pos); if (!key.isValid()) { Debug.Log("Player outside terrain area"); return; } // Mark neighbors as being needed TerrainKey[] neighbors = instance.terrainDictionary[key].neighbors; for (int i = 0; i < neighbors.Length; i++) { key = neighbors[i]; instance.terrainDictionary[key].lastNeeded = Time.timeSinceLevelLoad + 1.0f; } }
// What tile keys are around us? public TerrainKey[] getNeighbors() { if (!isValid()) { Debug.Log("Calling getNeighbors with invalid key"); return(new TerrainKey[] { }); } TerrainKey left = new TerrainKey(x - 1, z); TerrainKey right = new TerrainKey(x + 1, z); TerrainKey top = new TerrainKey(x, z + 1); TerrainKey topleft = new TerrainKey(x - 1, z + 1); TerrainKey topright = new TerrainKey(x + 1, z + 1); TerrainKey bottom = new TerrainKey(x, z - 1); TerrainKey bottomleft = new TerrainKey(x - 1, z - 1); TerrainKey bottomright = new TerrainKey(x + 1, z - 1); if (left.isValid()) { if (right.isValid()) { if (top.isValid()) { if (bottom.isValid()) // Return all { return new TerrainKey[] { topleft, top, topright, left, this, right, bottomleft, bottom, bottomright } } ; else // Remove bottom { return new TerrainKey[] { topleft, top, topright, left, this, right } }; } else { if (bottom.isValid()) // Remove top { return new TerrainKey[] { left, this, right, bottomleft, bottom, bottomright } } ; } } else { if (top.isValid()) { if (bottom.isValid()) // Remove right { return new TerrainKey[] { topleft, top, left, this, bottomleft, bottom } } ; else // Remove right and bottom { return new TerrainKey[] { topleft, top, left, this } }; } else { if (bottom.isValid()) // Remove right and top { return new TerrainKey[] { left, this, bottomleft, bottom } } ; } } } else { if (right.isValid()) { if (top.isValid()) { if (bottom.isValid()) // Remove left { return new TerrainKey[] { top, topright, this, right, bottom, bottomright } } ; else // Remove left and bottom { return new TerrainKey[] { top, topright, this, right } }; } else { if (bottom.isValid()) // Remove left and top { return new TerrainKey[] { this, right, bottom, bottomright } } ; } } } // Some cases are left out because we should never be less than size 3 Debug.Log("xMax or zMax cannot be less than 3"); return(new TerrainKey[] { }); }