示例#1
0
    /// <summary>
    /// Force le rechargement des chunks.
    /// </summary>
    public void Reload(Position centerChunkPosition, bool save = true)
    {
        CenterChunkPosition = centerChunkPosition;

        if (save)
        {
            SaveLoadedChunks();
        }

        AsyncChunkOps.ClearLoadingQueue();
        AsyncChunkOps.ClearLoadedQueue();
        LoadedChunks.Clear();

        foreach (Chunk chunk in DisplayedChunks.Values)
        {
            GameObject.Destroy(chunk.ChunkObject);
        }

        DisplayedChunks.Clear();

        Position start = centerChunkPosition.SubtractAll(loadDistance);
        Position end   = centerChunkPosition.AddAll(loadDistance);

        //load chunks
        for (int x = start.X; x <= end.X; x++)
        {
            for (int y = start.Y; y <= end.Y; y++)
            {
                for (int z = start.Z; z <= end.Z; z++)
                {
                    Position tmp = new Position(x, y, z);

                    if (Position.DistanceBetween(tmp, centerChunkPosition).AnySupTo(3))
                    {
                        AsyncChunkOps.AddChunkToLoadList(this, tmp);
                    }
                    else
                    {
                        Chunk chunk = GetChunkFromFile(tmp);
                        LoadedChunks.Add(tmp, chunk);
                        DisplayedChunks.Add(tmp, chunk);
                        chunk.Instantiate();
                    }
                }
            }
        }
    }
示例#2
0
    /// <summary>
    /// Met à jour les chunks chargés.
    /// </summary>
    public void Update(Vector3 playerPosition)
    {
        Chunk loaded = AsyncChunkOps.GetLoadedChunk();

        if ((loaded != null) && !LoadedChunks.ContainsKey(loaded.Position))
        {
            LoadedChunks.Add(loaded.Position, loaded);

            if (Position.DistanceBetween(loaded.Position, CenterChunkPosition).AllInfEqTo(LoadDistance))
            {
                DisplayedChunks.Add(loaded.Position, loaded);
                loaded.Instantiate();
            }
        }

        if (ToInstantiate.Count > 0)
        {
            Chunk chunk = ToInstantiate.Dequeue();
            chunk.Instantiate();
        }

        long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

        if (now - LastChunkUpdate > 1000)
        {
            LastChunkUpdate = now;
            foreach (Chunk chunk in LoadedChunks.Values)
            {
                chunk.Update();
            }
        }

        Position playerChunkPosition = EntityUtils.ToChunkPosition(playerPosition);

        if (playerChunkPosition == CenterChunkPosition)
        {
            return;
        }

        //unload chunks
        foreach (Chunk chunk in new List <Chunk>(LoadedChunks.Values))
        {
            if (Position.DistanceBetween(chunk.Position, playerChunkPosition).AnySupTo(loadDistance * 2))
            {
                DisplayedChunks.Remove(chunk.Position);
                LoadedChunks.Remove(chunk.Position);
                chunk.Destroy();
                AsyncChunkOps.AddChunkToSaveList(this, chunk);
            }
        }

        //hide chunks
        foreach (Chunk chunk in new List <Chunk>(DisplayedChunks.Values))
        {
            if (Position.DistanceBetween(chunk.Position, playerChunkPosition).AnySupTo(loadDistance))
            {
                DisplayedChunks.Remove(chunk.Position);
                chunk.Hide();
            }
        }

        Position start = playerChunkPosition.SubtractAll(loadDistance);
        Position end   = playerChunkPosition.AddAll(loadDistance);

        //load chunks.
        for (int x = start.X; x <= end.X; x++)
        {
            for (int y = start.Y; y <= end.Y; y++)
            {
                for (int z = start.Z; z <= end.Z; z++)
                {
                    Position tmp = new Position(x, y, z);

                    if (!IsChunkLoaded(tmp))
                    {
                        AsyncChunkOps.AddChunkToLoadList(this, tmp);
                    }
                    else
                    {
                        if (!IsChunkDisplayed(tmp))
                        {
                            Chunk toDisplay = LoadedChunks[tmp];
                            ToInstantiate.Enqueue(toDisplay);
                            DisplayedChunks.Add(tmp, toDisplay);
                        }
                    }
                }
            }
        }

        CenterChunkPosition = playerChunkPosition;
    }