void StepSimulation() { simulationFrame++; // TODO: find a way to retrieve all active viewports, not just the main one. var mainViewport = GetViewport().GetViewportRid(); // disable main viewport VisualServer.ViewportSetActive(mainViewport, false); try { var loadedTilesList = LoadedTiles.ToList(); foreach (var tile in loadedTilesList) { tile.RunSimulationOnNextDraw(simulationFrame); } VisualServer.ForceDraw(false); foreach (var tile in loadedTilesList) { if (!tile.SwapBuffer()) { continue; } var neighbors = GetTilesRange(tile.TileX - 1, tile.TileX + 1, tile.TileY - 1, tile.TileY + 1).Where(n => n != tile); foreach (var neighbor in neighbors) { neighbor.SetDataTexture(tile.TileX, tile.TileY, tile.DataTexture); } } } finally { // restore main viewport VisualServer.ViewportSetActive(mainViewport, true); } }