public void Run(IAABBox startViewBox, IQuadTreeNode rootNode, IArrayBackgroundWorkerListener listener) { var visibleNodesCollector = new VisibleNodesCollector(); rootNode.VisitVisibleNodes(startViewBox, visibleNodesCollector); var planetFactory = mPlanetFactoryCreator.CreatePlanetFactory(); foreach (var curLeaf in visibleNodesCollector.GetVisibleLeaves()) { // ReSharper disable once PossibleNullReferenceException curLeaf.SetPlanets(planetFactory.CreatePlanetsForSector()); } var nodesInCameraCollector = new VisibleNodesCollector(); rootNode.VisitVisibleNodes(new AABBox(0f, 0f, mConstants.GetMaxCameraSize(), mConstants.GetMaxCameraSize()), nodesInCameraCollector); mBackgroundWorker.AddListener(listener); mBackgroundWorker.Run(nodesInCameraCollector.GetVisibleLeaves(), mPlanetFactoryCreator); }
public void Run(IReadOnlyList <QuadTreeLeaf> loadingNodes, IPlanetFactoryCreator planetFactoryCreator) { mErrorMessage = null; var processorCount = Environment.ProcessorCount; var leafsPerProc = loadingNodes.Count / processorCount; mBackgroundWorkers = new BackgroundWorker[processorCount]; //mIsWorking = true; for (int j = 0; j < processorCount; ++j) { var startIndex = j * leafsPerProc; var endIndex = startIndex + leafsPerProc; if (endIndex >= loadingNodes.Count) { endIndex = loadingNodes.Count - 1; } BackgroundWorker worker = new BackgroundWorker(); mBackgroundWorkers[j] = worker; var planetFactory = planetFactoryCreator.CreatePlanetFactory(); worker.DoWork += (sender, args) => { var totalLeafs = endIndex - startIndex; for (int i = startIndex; i < endIndex; ++i) { if (loadingNodes[i].IsPlanetDataNull()) { loadingNodes[i].SetPlanets(planetFactory.CreatePlanetsForSector()); } var floatData = ((float)(i - startIndex) / totalLeafs) * 100f; worker.ReportProgress((int)floatData); } }; worker.WorkerReportsProgress = true; worker.ProgressChanged += (sender, args) => { CallBackFromLoad(sender, args); }; worker.RunWorkerAsync(); worker.RunWorkerCompleted += WorkerOnRunWorkerCompleted; } }