mirror of
https://github.com/sbrl/Nibriboard.git
synced 2018-01-10 21:33:49 +00:00
[client] Create client-side chunk cache and {Location,Chunk}Reference classes
This commit is contained in:
parent
77ce5b4d71
commit
7f0574a67c
2 changed files with 139 additions and 0 deletions
68
Nibriboard/ClientFiles/RippleSpace/ChunkCache.js
Normal file
68
Nibriboard/ClientFiles/RippleSpace/ChunkCache.js
Normal file
|
@ -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;
|
71
Nibriboard/ClientFiles/RippleSpace/Reference.js
Normal file
71
Nibriboard/ClientFiles/RippleSpace/Reference.js
Normal file
|
@ -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 };
|
Loading…
Reference in a new issue