private bool IsInActiveRange(Position ObjectPos, Position Pos) { if (Position.Distance(ObjectPos, Pos) < maxActiveDistance) { return(true); } else { return(false); } }
private void RebuildObjects(Position Pos, int Width, int Height) { //Ceiling all the ranges for the sake of consistency Width = (int)Math.Ceiling(Width / 2.0); Height = (int)Math.Ceiling(Height / 2.0); lastCheckPosition = Pos; visibleObjects.Clear(); //This rebuilds the entire active objects list in one pass. //If there is only 1 active object and this is being called (this happens when you move the player to a grid, as all active objects //get deactivated when you move off a grid) then rebuild the entire thing immediately. //Alternatively if the player has moved too far from their previous position AND the batch updating hasn't finished yet, do it all //in one go instead. Lags a bit more but avoids a pop-in effect of gameobjects being reactivated on screen. if (activeObjects.Count <= 1 || Position.Distance(Pos, lastObjectCheckPos) > fullRecheckActiveObjectsdistance) { foreach (GameObject go in activeObjects) { go.Active = false; } activeObjects.Clear(); lastObjectCheckPos = Pos; BatchObjects.Clear(); CurrentBatchRebuild = 0; BatchComplete = false; foreach (GameObject go in gameObjects) { if (IsInActiveRange(go.Position, Pos)) { activeObjects.Add(go); go.Active = true; if (IsInVisibleRange(go.Position, Pos, Width, Height)) { visibleObjects.Add(go); } } } } else if (Position.Distance(Pos, lastObjectCheckPos) > recheckActiveObjectsDistance) { for (int i = 0; i <= BatchSize; i++) { if (i + CurrentBatchRebuild >= gameObjects.Count) { BatchComplete = true; break; } if (gameObjects[i + CurrentBatchRebuild] != null) { if (IsInActiveRange(gameObjects[i + CurrentBatchRebuild].Position, Pos)) { BatchObjects.Add(gameObjects[i + CurrentBatchRebuild]); } } if (i >= BatchSize) { CurrentBatchRebuild = i + CurrentBatchRebuild; } } if (BatchComplete) { activeObjects.Clear(); foreach (GameObject go in BatchObjects) { activeObjects.Add(go); } lastObjectCheckPos = Pos; BatchObjects.Clear(); CurrentBatchRebuild = 0; BatchComplete = false; } foreach (GameObject go in activeObjects) { if (IsInVisibleRange(go.Position, Pos, Width, Height)) { visibleObjects.Add(go); } } } else { foreach (GameObject go in activeObjects) { if (IsInVisibleRange(go.Position, Pos, Width, Height)) { visibleObjects.Add(go); } } } }