1
0
Fork 0
Nibriboard/Nibriboard/RippleSpace/Plane.cs

69 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
namespace Nibriboard.RippleSpace
{
/// <summary>
/// Represents an infinite plane.
/// </summary>
public class Plane
{
/// <summary>
/// The name of this plane.
/// </summary>
public readonly string Name;
/// <summary>
/// The size of the chunks on this plane.
/// </summary>
public readonly int ChunkSize;
/// <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;
/// <summary>
/// The chunkspace that holds the currently loaded and active chunks.
/// </summary>
protected Dictionary<ChunkReference, Chunk> loadedChunkspace = new Dictionary<ChunkReference, Chunk>();
public Plane(string inName, int inChunkSize)
{
Name = inName;
ChunkSize = inChunkSize;
StorageDirectory = $"./Planes/{Name}";
}
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;
}
}
}