Exemple #1
0
        /// <summary>
        /// Get an unused chunk controller from the pool we made, while also making sure the chunk isn't already part of said pool.
        /// </summary>
        /// <returns></returns>
        bool getUnusedChunkController(Vector3 chunkLocationToSet, out ChunkController unusedChunkController)
        {
            unusedChunkController = null;
            bool foundUnusedController = false;

            foreach (ChunkController chunkController in chunkControllerPool)
            {
                // if the chunk is active and already has the location we're looking for, we return false
                if (chunkController.isActive) // these ifs are seprate because of the else if below.
                {
                    if (chunkController.chunkLocation == chunkLocationToSet)
                    {
                        if (unusedChunkController != null)
                        {
                            unusedChunkController.isActive = false;
                            unusedChunkController          = null;
                        }

                        return(false);
                    }
                    // if we found an inactive controller, and we're still looking for that, lets snag it and stop looking.
                }
                else if (!foundUnusedController)
                {
                    chunkController.isActive = true;
                    unusedChunkController    = chunkController;
                    foundUnusedController    = true;
                }
            }

            // return true if we never found a chunkController already set to this new chunk location.
            return(true);
        }
Exemple #2
0
        ///// PUBLIC FUNCTIONS

        /// <summary>
        /// Initilize this chunk controller for it's provided level.
        /// </summary>
        public void initializeFor(ILevel level)
        {
            if (chunkObjectPrefab == null)
            {
                World.Debugger.logError("UnityLevelController Missing chunk prefab, can't work");
            }
            else if (level == null)
            {
                World.Debugger.logError("No level provided to level controller");
            }
            else
            {
                /// init
                this.level = level;
                chunksWaitingForAFreeController    = new ConcurrentBag <IVoxelChunk>();
                chunkControllerAssignmentWaitQueue = new List <IVoxelChunk>();
                chunksWithNewlyGeneratedMeshes     = new ConcurrentBag <ChunkController>();
                chunksToMesh               = new List <ChunkController>();
                newlyActivatedChunks       = new ConcurrentBag <Coordinate>();
                chunksToActivate           = new List <Coordinate>();
                chunksWithOutOfFocusMeshes = new ConcurrentBag <ChunkController>();
                chunksToDeMesh             = new List <ChunkController>();
                newlyDeactivatedChunks     = new ConcurrentBag <Coordinate>();
                chunksToDeactivate         = new List <Coordinate>();

                // build the controller pool based on the maxed meshed chunk area we should ever have:
                IChunkResolutionAperture meshResolutionAperture = level.getApetureForResolutionLayer(Level.FocusResolutionLayers.Meshed);
                chunkControllerPool = new ChunkController[meshResolutionAperture.managedChunkRadius * meshResolutionAperture.managedChunkRadius * level.chunkBounds.y * 2];
                for (int index = 0; index < chunkControllerPool.Length; index++)
                {
                    // for each chunk we want to be able to render at once, create a new pooled gameobject for it with the prefab that has a unitu chunk controller on it
                    GameObject chunkObject = Instantiate(chunkObjectPrefab);
                    chunkObject.transform.parent = gameObject.transform;
                    ChunkController chunkController = chunkObject.GetComponent <ChunkController>();
                    if (chunkController == null)
                    {
                        //@TODO: make a queue for these maybe, just in case?
                        World.Debugger.logError($"No chunk controller on {chunkObject.name}");
                    }
                    else
                    {
                        chunkControllerPool[index]      = chunkController;
                        chunkController.levelController = this;
                        chunkObject.SetActive(false);
                    }
                }

                /// this controller is now loaded
                isLoaded = true;
            }
        }
Exemple #3
0
        /// <summary>
        /// Get the chunk controller that's already assigned to the given chunk location
        /// </summary>
        /// <param name="chunkLocation"></param>
        /// <returns></returns>
        bool tryToGetAssignedChunkController(Coordinate chunkLocation, out ChunkController assignedChunkController)
        {
            assignedChunkController = null;
            foreach (ChunkController chunkController in chunkControllerPool)
            {
                if (chunkController.isActive && chunkController.chunkLocation == chunkLocation.vec3)
                {
                    assignedChunkController = chunkController;
                    return(true);
                }
            }

            return(false);
        }