private IEnumerator Start()
        {
            MinecraftSynchronizationContext.InitializeSynchronizationContext();
            SyncContext = SynchronizationContext.Current;

            WorldSettings settings = WorldSettings.Active;

            DataManager = new DataManager(settings.ResourcePackageName);

            yield return(DataManager.InitBlocks());

            yield return(DataManager.InitItems());

            yield return(DataManager.InitMaterials());

            Initialized       = false;
            Active            = this;
            m_PlayerTransform = m_Player.transform;
            m_CameraTransform = m_MainCamera.transform;

            Initialize(settings);

            yield return(DataManager.DoLua());

            DataManager.LuaFullGC();
            GC.Collect();
        }
예제 #2
0
        public ChunkManager(WorldSettings settings, Camera mainCamera, Transform player, string chunkSavingDirectory, Material solidMaterial, Material liquidMaterial)
        {
            m_ChunkPositionComparer = new ChunkPositionComparer();

            m_Chunks = new ConcurrentDictionary <Vector2Int, Chunk>(m_ChunkPositionComparer);

            m_ChunksToLoad               = new ConcurrentQueue <Vector2Int>();
            m_ChunksToUnload             = new ConcurrentQueue <Vector2Int>();
            m_ChunkLoader                = new ChunkLoader(settings.Seed, chunkSavingDirectory, settings.Type);
            m_BuildAndUnloadChunksThread = new Thread(BuildAndUnloadChunksThreadMethod)
            {
                IsBackground = true
            };

            m_ChunksToRender = new HashSet <Vector2Int>(m_ChunkPositionComparer);

            m_UpdateChunksThread = new Thread(UpdateChunksThreadMethod)
            {
                IsBackground = true
            };

            m_BlocksToTickQueue  = new Queue <Vector3Int>();
            m_TickBlockInterval  = 0.5f;
            m_TickBlockDeltaTime = 0;

            m_BlocksToLightQueue = new Stack <Vector3Int>();

            m_TickChunkRandom    = new Random(settings.Seed);
            m_TickChunkInterval  = 2;
            m_TickChunkDeltaTime = 0;

            m_PlayerPositionX        = 0;
            m_PlayerPositionY        = 0;
            m_PlayerPositionZ        = 0;
            m_PlayerForwardX         = 0;
            m_PlayerForwardZ         = 0;
            m_ChunksUpdatingRequired = false;

            m_RenderChunkRadius       = GlobalSettings.Instance.RenderChunkRadius;
            m_SqrRenderRadius         = m_RenderChunkRadius * m_RenderChunkRadius * ChunkWidth * ChunkWidth;
            m_HorizontalFOVInDEG      = GlobalSettings.Instance.HorizontalFOVInDEG;
            m_MaxChunkCountInMemory   = GlobalSettings.Instance.MaxChunkCountInMemory;
            m_SharedSolidMaterial     = solidMaterial;
            m_SharedLiquidMaterial    = liquidMaterial;
            m_MainCamera              = mainCamera;
            m_MainCamera.farClipPlane = GlobalSettings.Instance.RenderRadius;
            m_PlayerTransform         = player;

            MaterialProperties = new ExposedMaterialProperties(new MaterialPropertyBlock(), new MaterialPropertyBlock());

            InitMaterialProperties();

            m_IsStartUp = true;
            m_Disposed  = false;

#if UNITY_EDITOR
            m_BuildAndUnloadChunksThread.Name = "Build And Unload Chunks Thread";
            m_UpdateChunksThread.Name         = "Update Chunks Thread";
#endif
        }
        private void Initialize(WorldSettings settings)
        {
            Settings = settings ?? throw new ArgumentNullException(nameof(settings));
            WorldSettingsSavingPath = Application.persistentDataPath + "/Worlds";

            string chunkSavingDirectory = WorldSettingsSavingPath + $"/{settings.Name}/Chunks";

            ChunkManager  = new ChunkManager(settings, MainCamera, m_PlayerTransform, chunkSavingDirectory, DataManager.ChunkMaterial, DataManager.LiquidMaterial);
            EntityManager = new EntityManager(DataManager.BlockEntityMaterial, m_Player);

            m_PlayerTransform.position      = (settings.Position.y < 0 || settings.Position.y >= WorldConsts.WorldHeight) ? new Vector3(0, WorldConsts.WorldHeight, 0) : settings.Position;
            m_PlayerTransform.localRotation = settings.BodyRotation;
            m_CameraTransform.localRotation = settings.CameraRotation;

            ChunkManager.OnChunksReadyWhenStartingUp += () =>
            {
                ScreenCapture.CaptureScreenshot(WorldSettingsSavingPath + "/" + Settings.Name + "/Thumbnail.png"); // 没办法

                if (Settings.Position.y < 0 || Settings.Position.y >= WorldConsts.WorldHeight)
                {
                    Chunk chunk = ChunkManager.GetChunkByNormalizedPosition(0, 0);
                    m_PlayerTransform.position = new Vector3(0, chunk.GetTopNonAirIndex(0, 0) + 5, 0);
                }

                m_Player.enabled = true;

                Destroy(m_LoadingMenu);
            };

            ChunkManager.StartChunksUpdatingThread();
            ChunkManager.StartChunksBuildingThread();

            Initialized = true;
        }