From 7f0574a67c73e7e0b1523ef2170b6152605b66d5 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 28 Mar 2017 19:40:24 +0100
Subject: [PATCH] [client] Create client-side chunk cache and
 {Location,Chunk}Reference classes

---
 .../ClientFiles/RippleSpace/ChunkCache.js     | 68 ++++++++++++++++++
 .../ClientFiles/RippleSpace/Reference.js      | 71 +++++++++++++++++++
 2 files changed, 139 insertions(+)
 create mode 100644 Nibriboard/ClientFiles/RippleSpace/ChunkCache.js
 create mode 100644 Nibriboard/ClientFiles/RippleSpace/Reference.js

diff --git a/Nibriboard/ClientFiles/RippleSpace/ChunkCache.js b/Nibriboard/ClientFiles/RippleSpace/ChunkCache.js
new file mode 100644
index 0000000..28981e1
--- /dev/null
+++ b/Nibriboard/ClientFiles/RippleSpace/ChunkCache.js
@@ -0,0 +1,68 @@
+"use strict";
+
+/**
+ * Manages a local cache of chunks.
+ */
+class ChunkCache
+{
+	constructor()
+	{
+		this.chunks = new Map();
+	}
+	
+	/**
+	 * Adds the specified chunks to the chunk cache.
+	 * @param {Chunk[]} chunks The chunks to add.
+	 */
+	addChunks(...chunks)
+	{
+		for (let { reference, chunk } of chunks) {
+			this.chunks.set(reference.toString(), chunk);
+		}
+	}
+	
+	/**
+	 * Figures out whether a chunk with the specified chunk reference is
+	 * currently present in this chunk cache.
+	 * @param	{ChunkReference}	chunkRef	The chunk reference to search for.
+	 * @return	{Boolean}			Whether the specified chunk reference is
+	 * 								currently in this chunk cache.
+	 */
+	hasChunk(chunkRef)
+	{
+		return this.chunks.has(chunkRef.toString());
+	}
+	
+	/**
+	 * Forgets the specified chunk references.
+	 * @param  {ChunkReference[]} chunkRefs References to the chunks to forget.
+	 */
+	forgetChunk(...chunkRefs)
+	{
+		for (let chunkRef of chunkRefs)
+			this.chunks.delete(chunkRef.toString());
+	}
+	
+	/**
+	 * Forgets all the chunks currently in the chunk cache.
+	 */
+	forgetAll()
+	{
+		this.chunks = new Map();
+	}
+	
+	/**
+	 * Returns the number of chunks currently in the chunk cache.
+	 */
+	get CacheSize()
+	{
+		return this.chunks.size;
+	}
+	
+	[Symbol.iterator]()
+	{
+		return this.chunks[Symbol.iterator];
+	}
+}
+
+export default ChunkCache;
diff --git a/Nibriboard/ClientFiles/RippleSpace/Reference.js b/Nibriboard/ClientFiles/RippleSpace/Reference.js
new file mode 100644
index 0000000..89a4ded
--- /dev/null
+++ b/Nibriboard/ClientFiles/RippleSpace/Reference.js
@@ -0,0 +1,71 @@
+"use strict";
+
+/**
+ * Represents a point in 2d space that's not tied to a particular plane.
+ */
+class LocationReference
+{
+	/**
+	 * Creates a new location reference
+	 * @param  {number} inX The x coordinate
+	 * @param  {number} inY The y coordinate
+	 * @return {LocationReference}	The new location reference
+	 */
+	constructor(inX, inY)
+	{
+		this.X = inX;
+		this.Y = inY;
+	}
+	
+	/**
+	 * Returns this LocationReference as a string.
+	 * @return {string} This LocationReference as a string.
+	 */
+	toString()
+	{
+		return `LocationReference: (${this.X}, ${this.Y})`;
+	}
+}
+
+/**
+ * References a chunk in 2d space, that's not tied to a specific plane.
+ */
+class ChunkReference
+{
+	/**
+	 * Creates a new ChunkReference instance.
+	 * @param	{number}	inChunkSize	The size of the chunk.
+	 * @param	{number}	inX			The x position of the chunk in chunk-space.
+	 * @param	{number}	inY			The y position of the chunk in chunk-space.
+	 * @return	{ChunkReference}		The new chunk reference.
+	 */
+	constructor(inChunkSize, inX, inY)
+	{
+		this.ChunkSize = inChunkSize;
+		this.X = inX;
+		this.Y = inY;
+	}
+	
+	/**
+	 * Converts ths ChunkReference into a LocationReference.
+	 * @return	{LocationReference}	This ChunkReference as a LocationReference.
+	 */
+	AsLocationReference()
+	{
+		return new LocationReference(
+			this.X * this.ChunkSize,
+			this.Y * this.ChunkSize
+		);
+	}
+	
+	/**
+	 * This LocationReference represented as a string.
+	 * @return {string} This LocationReference as a string.
+	 */
+	toString()
+	{
+		return `ChunkReference: ${this.ChunkSize} x (${this.X}, ${this.Y})`;
+	}
+}
+
+export { LocationReference, ChunkReference };
