1
0
Fork 0
mirror of https://github.com/sbrl/Nibriboard.git synced 2018-01-10 21:33:49 +00:00

Set up initial chunk loading skeleton.

This commit is contained in:
Starbeamrainbowlabs 2017-01-06 21:14:31 +00:00
parent f3c515263e
commit 89e2ccafd2
3 changed files with 99 additions and 2 deletions

View file

@ -1,11 +1,22 @@
using System; using System;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections;
namespace Nibriboard.RippleSpace namespace Nibriboard.RippleSpace
{ {
/// <summary> /// <summary>
/// Represents a single chunk of an infinite <see cref="Nibriboard.RippleSpace.Plane" />. /// Represents a single chunk of an infinite <see cref="Nibriboard.RippleSpace.Plane" />.
/// </summary> /// </summary>
public class Chunk public class Chunk : IEnumerable<DrawnLine>
{ {
/// <summary>
/// The lines that this chunk currently contains.
/// </summary>
private List<DrawnLine> lines = new List<DrawnLine>();
/// <summary> /// <summary>
/// The plane that this chunk is located on. /// The plane that this chunk is located on.
/// </summary> /// </summary>
@ -16,10 +27,68 @@ namespace Nibriboard.RippleSpace
/// </summary> /// </summary>
public readonly int Size; public readonly int Size;
/// <summary>
/// The time at which this chunk was loaded.
/// </summary>
public readonly DateTime TimeLoaded = DateTime.Now;
/// <summary>
/// The time at which this chunk was last accessed.
/// </summary>
public DateTime TimeLastAccessed { get; private set; } = DateTime.Now;
public Chunk(Plane inPlane, int inSize) public Chunk(Plane inPlane, int inSize)
{ {
Plane = inPlane; Plane = inPlane;
Size = inSize; Size = inSize;
} }
public void UpdateAccessTime()
{
TimeLastAccessed = DateTime.Now;
}
public DrawnLine this[int i]
{
get {
UpdateAccessTime();
return lines[i];
}
set {
UpdateAccessTime();
lines[i] = value;
}
}
public IEnumerator<DrawnLine> GetEnumerator()
{
UpdateAccessTime();
return lines.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
UpdateAccessTime();
return GetEnumerator();
}
public static async Task<Chunk> FromFile(Plane plane, string filename)
{
StreamReader chunkSource = new StreamReader(filename);
return await FromStream(plane, chunkSource);
}
public static async Task<Chunk> FromStream(Plane plane, StreamReader chunkSource)
{
Chunk result = new Chunk(
plane,
int.Parse(chunkSource.ReadLine())
);
string nextLine = string.Empty;
while((nextLine = await chunkSource.ReadLineAsync()) != null)
{
throw new NotImplementedException();
}
return result;
}
} }
} }

View file

@ -29,5 +29,10 @@ namespace Nibriboard.RippleSpace
Y / Plane.ChunkSize Y / Plane.ChunkSize
); );
} }
public string AsFilename()
{
return $"{Plane.Name}-{X},{Y}.chunk";
}
} }
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
namespace Nibriboard.RippleSpace namespace Nibriboard.RippleSpace
{ {
/// <summary> /// <summary>
@ -7,6 +8,11 @@ namespace Nibriboard.RippleSpace
/// </summary> /// </summary>
public class Plane public class Plane
{ {
/// <summary>
/// The name of this plane.
/// </summary>
public readonly string Name;
/// <summary> /// <summary>
/// The size of the chunks on this plane. /// The size of the chunks on this plane.
/// </summary> /// </summary>
@ -17,9 +23,26 @@ namespace Nibriboard.RippleSpace
/// </summary> /// </summary>
protected Dictionary<ChunkReference, Chunk> loadedChunkspace = new Dictionary<ChunkReference, Chunk>(); protected Dictionary<ChunkReference, Chunk> loadedChunkspace = new Dictionary<ChunkReference, Chunk>();
public Plane(int inChunkSize) public Plane(string inName, int inChunkSize)
{ {
Name = inName;
ChunkSize = inChunkSize; ChunkSize = inChunkSize;
} }
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.
Chunk loadedChunk = await Chunk.FromFile(this, chunkLocation.AsFilename());
loadedChunkspace.Add(chunkLocation, loadedChunk);
return loadedChunk;
}
} }
} }