public bool playerIsInNewChunk() { int x = UMaths.scaledFloor(chunkSize, transform.position.x); int y = UMaths.scaledFloor(chunkSize, transform.position.z); if (lastChunkX != x || lastChunkY != y) { lastChunkX = x; lastChunkY = y; return(true); } return(false); }
public void drawPoints(float size = 1) { int x = UMaths.scaledFloor(chunkSize, transform.position.x); int y = UMaths.scaledFloor(chunkSize, transform.position.z); for (int i = -chunkAdjacentLayers; i <= chunkAdjacentLayers; i++) { for (int j = -chunkAdjacentLayers; j <= chunkAdjacentLayers; j++) { Vector3 chunkPos = new Vector3(x + (i * chunkSize), 0, y + (j * chunkSize)); Gizmos.DrawSphere(chunkPos, size); } } }
void Update() { if (_clearHashMap) { clearHashMap(); _clearHashMap = false; } if (playerIsInNewChunk()) { /// chunk Position int x = UMaths.scaledFloor(chunkSize, transform.position.x); int y = UMaths.scaledFloor(chunkSize, transform.position.z); /// chunk number in the world starting from world position 0,0 int xNum = (int)(x / chunkSize); int yNum = (int)(y / chunkSize); /// starting tile // Only for debug startTile = new USlippyTile(startLon, startLat, zoom); /// sapwn all the new needed chunks for (int i = -chunkAdjacentLayers; i <= chunkAdjacentLayers; i++) { for (int j = -chunkAdjacentLayers; j <= chunkAdjacentLayers; j++) { USlippyTile sTile = new USlippyTile(xNum + i + startTile.x, yNum + j + startTile.y, zoom); /// This line fix that world is flipped upside down sTile.y = startTile.y + (startTile.y - sTile.y); string key = genTerrainName(sTile.x, sTile.y); if (!map.ContainsKey(key)) { GameObject t = createTerrain(sTile, key); Vector3 pos = new Vector3(); pos.Set(x + (i * chunkSize), 0, y + (j * chunkSize)); t.transform.position = pos; map.Add(key, t); } } } /// after spawning the new chunks, remove the olders that we dont need removeResidualChunks(); } }
public static Int3 findChunk(float _x, float _y, float _z, float chunkSize) { return(new Int3((int)(UMaths.scaledFloor(chunkSize, _x) / chunkSize), (int)(UMaths.scaledFloor(chunkSize, _y) / chunkSize), (int)(UMaths.scaledFloor(chunkSize, _z) / chunkSize))); }