2017-01-06 18:57:58 +00:00
|
|
|
|
using System;
|
2017-01-06 20:45:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-01-06 21:14:31 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-01-07 17:35:56 +00:00
|
|
|
|
using System.IO;
|
2017-01-06 18:57:58 +00:00
|
|
|
|
namespace Nibriboard.RippleSpace
|
|
|
|
|
{
|
2017-01-06 19:05:27 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents an infinite plane.
|
|
|
|
|
/// </summary>
|
2017-01-06 18:57:58 +00:00
|
|
|
|
public class Plane
|
|
|
|
|
{
|
2017-01-06 21:14:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The name of this plane.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly string Name;
|
|
|
|
|
|
2017-01-06 20:45:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The size of the chunks on this plane.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly int ChunkSize;
|
|
|
|
|
|
2017-01-07 17:35:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The path to the directory that the plane's information will be stored in.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly string StorageDirectory;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of milliseconds that should pass since a chunk's last
|
|
|
|
|
/// access in order for it to be considered inactive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int InactiveMillisecs = 60 * 1000;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of chunks in a square around (0, 0) that should always be
|
|
|
|
|
/// loaded.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int PrimaryChunkAreaSize = 10;
|
|
|
|
|
|
2017-01-06 20:45:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The chunkspace that holds the currently loaded and active chunks.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Dictionary<ChunkReference, Chunk> loadedChunkspace = new Dictionary<ChunkReference, Chunk>();
|
|
|
|
|
|
2017-01-06 21:14:31 +00:00
|
|
|
|
public Plane(string inName, int inChunkSize)
|
2017-01-06 18:57:58 +00:00
|
|
|
|
{
|
2017-01-06 21:14:31 +00:00
|
|
|
|
Name = inName;
|
2017-01-06 20:45:35 +00:00
|
|
|
|
ChunkSize = inChunkSize;
|
2017-01-07 17:35:56 +00:00
|
|
|
|
|
|
|
|
|
StorageDirectory = $"./Planes/{Name}";
|
2017-01-06 18:57:58 +00:00
|
|
|
|
}
|
2017-01-06 21:14:31 +00:00
|
|
|
|
|
|
|
|
|
public async Task<Chunk> FetchChunk(ChunkReference chunkLocation)
|
|
|
|
|
{
|
|
|
|
|
// 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.
|
2017-01-07 20:19:21 +00:00
|
|
|
|
string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilename());
|
|
|
|
|
Chunk loadedChunk = await Chunk.FromFile(this, chunkFilePath);
|
2017-01-06 21:14:31 +00:00
|
|
|
|
loadedChunkspace.Add(chunkLocation, loadedChunk);
|
|
|
|
|
|
|
|
|
|
return loadedChunk;
|
|
|
|
|
}
|
2017-01-08 14:45:48 +00:00
|
|
|
|
|
|
|
|
|
public async Task AddLine(DrawnLine newLine)
|
|
|
|
|
{
|
|
|
|
|
List<DrawnLine> chunkedLine;
|
|
|
|
|
// Split the line up into chunked pieces if neccessary
|
|
|
|
|
if(newLine.SpansMultipleChunks)
|
|
|
|
|
chunkedLine = newLine.SplitOnChunks(ChunkSize);
|
|
|
|
|
else
|
|
|
|
|
chunkedLine = new List<DrawnLine>() { newLine };
|
|
|
|
|
|
|
|
|
|
// Add each segment to the appropriate chunk
|
|
|
|
|
foreach(DrawnLine newLineSegment in chunkedLine)
|
|
|
|
|
{
|
|
|
|
|
Chunk containingChunk = await FetchChunk(newLineSegment.ContainingChunk);
|
|
|
|
|
containingChunk.Add(newLineSegment);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-06 18:57:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|