/// <summary> /// Called when the BlockWorld behaviour is enabled. /// </summary> private void OnEnable() { if (m_WorldContainer != null) { return; // Already enabled } var chunkSize = new GridSize(4); var world = new World(chunkSize); var blockList = new BlockTypeList(); var remesh = new RemeshHandler(); var database = new WorldDatabase("/home/thedudefromci/Bones3/TestWorld"); remesh.AddDistributor(new StandardDistributor()); m_WorldContainer = new WorldContainer(world, remesh, blockList, database); m_WorldContainer.BlockContainerProvider.OnBlockContainerCreated += OnChunkCreated; m_WorldContainer.BlockContainerProvider.OnBlockContainerDestroyed += OnChunkDestroyed; m_WorldContainer.RemeshHandler.OnRemeshFinish += OnRemeshFinished; #if UNITY_EDITOR if (!Application.isPlaying) { UnityEditor.EditorApplication.update += Update; } #endif }
/// <summary> /// Saves all chunks in the world. /// </summary> internal void SaveWorld(WorldContainer container) { Directory.CreateDirectory(m_ChunkFolder); List <Task> tasks = new List <Task>(); foreach (var chunk in container.World.ChunkIterator()) { if (chunk.IsModified) { var task = Task.Run(() => SaveChunk(chunk)); tasks.Add(task); } } for (int i = 0; i < tasks.Count; i++) { try { tasks[i].Wait(); } catch (Exception e) { UnityEngine.Debug.LogException(e); } } }
/// <summary> /// Creates a new server thread for the given world. /// </summary> /// <param name="world">The world.</param> internal ServerThread(WorldContainer world) { m_WorldContainer = world; var thread = new Thread(Run); thread.Name = "Block World"; thread.Start(); }
/// <summary> /// Called when the world object is constructed to initialize data. /// </summary> protected void Awake() { var world = new World(ChunkSize, ID); m_WorldContainer = new WorldContainer(world, this); m_WorldSaver = new WorldSaver(world); m_ChunkCreator = new ChunkCreator(this); m_ChunkMeshBuilder = new ChunkMeshBuilder(this); }
/// <summary> /// Analyses the given chunk and starts a set of remesh tasks for handling that chunk. /// This method blocks until all remesh tasks have been completed. /// </summary> /// <param name="worldContainer">The world to operate on.</param> /// <param name="chunkPos">The chunk target position.</param> internal RemeshTaskStack RemeshChunk(WorldContainer worldContainer, ChunkPosition chunkPos) { var taskStack = new RemeshTaskStack(chunkPos); var chunkGroup = new ChunkGroup(worldContainer, chunkPos); foreach (var dis in m_Distributors) { dis.CreateTasks(chunkGroup, taskStack); } return(taskStack); }
/// <summary> /// Creates a new Unity world builder object. /// </summary> /// <param name="transform">The transform to add chunk gameobjects to.</param> /// <param name="blockList">The block list to read from.</param> /// <param name="chunkSize">The chunk size of the world.</param> /// <param name="id">The ID value of the world.</param> internal UnityWorldBuilder(Transform transform, BlockListManager blockList, WorldProperties worldProperties) { ChunkSize = worldProperties.ChunkSize; var container = new WorldContainer(worldProperties); container.EventQueue.OnWorldEvent += OnBlockWorldEvent; { // TODO TEMP CODE REMOVE THIS container.BlockList.UpdateBlockType(CreateBlock(2, "Grass", 0)); container.BlockList.UpdateBlockType(CreateBlock(3, "SideDirt", 1)); container.BlockList.UpdateBlockType(CreateBlock(4, "Dirt", 2)); } m_ServerThread = new ServerThread(container); m_ChunkCreator = new ChunkCreator(transform, ChunkSize); m_ChunkMeshBuilder = new ChunkMeshBuilder(blockList); }
/// <summary> /// Called when the BlockWorld behaviour is disabled. /// </summary> private void OnDisable() { if (m_WorldContainer == null) { return; // Already disabled } m_WorldContainer.BlockContainerProvider.OnBlockContainerCreated -= OnChunkCreated; m_WorldContainer.BlockContainerProvider.OnBlockContainerDestroyed -= OnChunkDestroyed; m_WorldContainer = null; foreach (var chunk in m_Chunks) { m_ChunkCreator.DestroyChunk(chunk); } m_Chunks.Clear(); #if UNITY_EDITOR if (!Application.isPlaying) { UnityEditor.EditorApplication.update -= Update; } #endif }