Exemple #1
0
        public async Task <Chunk> FetchChunk(ChunkReference chunkLocation, bool autoCreate)
        {
            // If the chunk is in the loaded chunk-space, then return it immediately
            if (loadedChunkspace.ContainsKey(chunkLocation))
            {
                return(loadedChunkspace[chunkLocation]);
            }

            // Uh-oh! The chunk isn't loaded at moment. Load it quick & then
            // return it fast.
            string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilepath());
            Chunk  loadedChunk;

            if (File.Exists(chunkFilePath))            // If the chunk exists on disk, load it
            {
                loadedChunk = await Chunk.FromFile(this, chunkFilePath);
            }
            else
            {
                // Ooooh! It's a _new_, never-before-seen one! Create a brand new chunk :D
                // ....but only if we've been told it's ok to create new chunks.
                if (!autoCreate)
                {
                    return(null);
                }
                loadedChunk = new Chunk(this, ChunkSize, chunkLocation);
            }
            loadedChunk.OnChunkUpdate += HandleChunkUpdate;
            loadedChunkspace.Add(chunkLocation, loadedChunk);

            return(loadedChunk);
        }
Exemple #2
0
        public async Task SaveChunk(ChunkReference chunkLocation)
        {
            // It doesn't exist, so we can't save it :P
            if (!loadedChunkspace.ContainsKey(chunkLocation))
            {
                return;
            }

            Chunk  chunk         = loadedChunkspace[chunkLocation];
            string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilepath());

            // If it's empty, then there's no point in saving it
            if (chunk.IsEmpty)
            {
                // Delete the existing chunk file, if it
                if (File.Exists(chunkFilePath))
                {
                    File.Delete(chunkFilePath);
                }
                return;
            }


            using (Stream chunkDestination = File.Open(chunkFilePath, FileMode.OpenOrCreate))
                await chunk.SaveTo(chunkDestination);
        }
Exemple #3
0
        /// <summary>
        /// Works out whether a chunk currently exists.
        /// </summary>
        /// <param name="chunkLocation">The chunk location to check.</param>
        /// <returns>Whether the chunk at specified location exists or not.</returns>
        public bool HasChunk(ChunkReference chunkLocation)
        {
            if (loadedChunkspace.ContainsKey(chunkLocation))
            {
                return(true);
            }

            string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilepath());

            if (File.Exists(chunkFilePath))
            {
                return(true);
            }

            return(false);
        }
Exemple #4
0
        public override bool Equals(object obj)
        {
            ChunkReference otherChunkReference = obj as ChunkReference;

            if (otherChunkReference == null)
            {
                return(false);
            }

            if (X == otherChunkReference.X && Y == otherChunkReference.Y &&
                Plane == otherChunkReference.Plane)
            {
                return(true);
            }
            return(false);
        }
Exemple #5
0
        private async Task LoadPrimaryChunks()
        {
            List <ChunkReference> primaryChunkRefs = new List <ChunkReference>();

            ChunkReference currentRef = new ChunkReference(this, -PrimaryChunkAreaSize, -PrimaryChunkAreaSize);

            while (currentRef.Y < PrimaryChunkAreaSize)
            {
                primaryChunkRefs.Add(currentRef.Clone() as ChunkReference);

                currentRef.X++;

                if (currentRef.X > PrimaryChunkAreaSize)
                {
                    currentRef.X = -PrimaryChunkAreaSize;
                    currentRef.Y++;
                }
            }

            await FetchChunks(primaryChunkRefs, false);
        }
Exemple #6
0
 public Chunk(Plane inPlane, int inSize, ChunkReference inLocation)
 {
     plane    = inPlane;
     Size     = inSize;
     Location = inLocation;
 }
Exemple #7
0
        public async Task <bool> RemoveLineSegment(ChunkReference containingChunk, string targetLineUniqueId)
        {
            Chunk chunk = await FetchChunk(containingChunk);

            return(chunk.Remove(targetLineUniqueId));
        }
Exemple #8
0
 public async Task <Chunk> FetchChunk(ChunkReference chunkLocation)
 {
     return(await FetchChunk(chunkLocation, true));
 }
Exemple #9
0
        /// <summary>
        /// Splits this line into a list of lines that don't cross chunk boundaries.
        /// </summary>
        /// <returns>A list of lines, that, when stitched together, will produce this line.</returns>
        public List <DrawnLine> SplitOnChunks()
        {
            List <DrawnLine> results = new List <DrawnLine>();

            // Don't bother splitting the line up if it all falls in the same chunk
            if (!SpansMultipleChunks)
            {
                results.Add(this);
                return(results);
            }

            DrawnLine      nextLine     = new DrawnLine(LineId);
            ChunkReference currentChunk = null;

            foreach (LocationReference point in Points)
            {
                if (currentChunk != null && !point.ContainingChunk.Equals(currentChunk))
                {
                    // We're heading into a new chunk! Split the line up here.
                    // TODO: Add connecting lines to each DrawnLine instance to prevent gaps
                    nextLine.Colour = Colour;
                    nextLine.Width  = Width;
                    if (nextLine.Points.Count > 0)
                    {
                        results.Add(nextLine);
                    }
                    nextLine = new DrawnLine(LineId);
                }

                nextLine.Points.Add(point);
                if (!point.ContainingChunk.Equals(currentChunk))
                {
                    currentChunk = point.ContainingChunk;
                }
            }

            if (nextLine.Points.Count > 0)
            {
                nextLine.Colour = Colour;
                nextLine.Width  = Width;
                results.Add(nextLine);
            }

            // Set the ContinuesIn and ContinuesFrom properties
            // so that clients can find the next / previous chunk line fragmentss
            for (int i = 0; i < results.Count - 1; i++)
            {
                // Set the ContinuesFrom reference, but not on the first fragment in the list
                if (i > 0)
                {
                    results[i].ContinuesFrom   = results[i - 1].ContainingChunk;
                    results[i].ContinuesFromId = results[i - 1].UniqueId;
                }

                // Set the ContinuesIn reference, but not on the last fragment in the list
                if (i < results.Count - 1)
                {
                    results[i].ContinuesIn     = results[i + 1].ContainingChunk;
                    results[i].ContinuesWithId = results[i + 1].UniqueId;
                }
            }

            return(results);
        }