WorldChunk FindClosestChunk(Location l, out double distance, double maxDistance) { WorldChunk current = null; double currentDistance = 0; foreach (var c in world.Chunks) { var d = Location.HorizontalDistance(c.ChunkLocation, l); if (current == null || d < currentDistance) { current = c; currentDistance = d; } } distance = currentDistance; if (currentDistance > maxDistance) { return(null); } return(current); }
private IEnumerator StartWorld() { LogText($"Loading previous session file..."); yield return(new WaitForSeconds(0.5f)); if (RestoreWorldFromLocalStorage()) { LogText($"Restored world with {world.Chunks.Count} chunks"); yield return(new WaitForSeconds(0.5f)); if (world.Chunks.Count > 0) { double distance; var closestChunk = FindClosestChunk(state.CurrentLocation, out distance, 1000.0); if (closestChunk != null) { LogText($"Found closes chunk at {closestChunk.ChunkLocation}, d = {distance}"); yield return(new WaitForSeconds(0.5f)); SetCurrentChunk(closestChunk); LogText($"Current Chunk Set"); yield return(new WaitForSeconds(0.5f)); yield break; } else { LogText($"No chunk nearby!"); yield return(new WaitForSeconds(0.5f)); var i = 0; foreach (var c in world.Chunks) { var d = Location.HorizontalDistance(state.CurrentLocation, c.ChunkLocation); LogText($"Chunk {i} at {c.ChunkLocation}, d = {d}"); yield return(new WaitForSeconds(0.5f)); i++; } } } } else { LogText($"No world to restore!"); yield return(new WaitForSeconds(0.5f)); } LogText("Creating new chunk..."); yield return(new WaitForSeconds(0.5f)); var chunk = CreateTestChunk(); chunk.IsFresh = true; world.Chunks.Add(chunk); SetCurrentChunk(chunk); UpdateChunkLocation(chunk); LogText("Added new chunk!"); yield return(new WaitForSeconds(0.5f)); }