Ejemplo n.º 1
0
 /// <summary>
 /// Sauvegarde le terrain.
 /// </summary>
 public void SaveLoadedChunks()
 {
     foreach (Chunk chunk in LoadedChunks.Values)
     {
         AsyncChunkOps.AddChunkToSaveList(this, chunk);
     }
 }
Ejemplo n.º 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;
    }