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

[client] Make Chunk/Location reference classes inherit from Vector

This commit is contained in:
Starbeamrainbowlabs 2017-03-28 20:58:20 +01:00
parent 2ea097fdbc
commit b14f7c9948

View file

@ -1,20 +1,30 @@
"use strict"; "use strict";
import Vector from '../Utilities/Vector';
/** /**
* Represents a point in 2d space that's not tied to a particular plane. * Represents a point in 2d space that's not tied to a particular plane.
*/ */
class LocationReference class LocationReference extends Vector
{ {
/** /**
* Creates a new location reference * Fetches a reference to the chunk that this LocationReference
* @param {number} inX The x coordinate * falls inside.
* @param {number} inY The y coordinate * @param {number} chunkSize The chunk size.
* @return {LocationReference} The new location reference
*/ */
constructor(inX, inY) ContainingChunk(chunkSize) {
return new ChunkReference(
chunkSize,
this.x /= chunkSize,
this.y /= chunkSize
);
}
is(otherLocationRef)
{ {
this.X = inX; return this.x === otherLocationRef.x &&
this.Y = inY; this.y === otherLocationRef.y;
} }
/** /**
@ -23,14 +33,14 @@ class LocationReference
*/ */
toString() toString()
{ {
return `LocationReference: (${this.X}, ${this.Y})`; return `LocationReference: (${this.x}, ${this.y})`;
} }
} }
/** /**
* References a chunk in 2d space, that's not tied to a specific plane. * References a chunk in 2d space, that's not tied to a specific plane.
*/ */
class ChunkReference class ChunkReference extends Vector
{ {
/** /**
* Creates a new ChunkReference instance. * Creates a new ChunkReference instance.
@ -41,9 +51,8 @@ class ChunkReference
*/ */
constructor(inChunkSize, inX, inY) constructor(inChunkSize, inX, inY)
{ {
super(inX, inY);
this.ChunkSize = inChunkSize; this.ChunkSize = inChunkSize;
this.X = inX;
this.Y = inY;
} }
/** /**
@ -53,18 +62,32 @@ class ChunkReference
AsLocationReference() AsLocationReference()
{ {
return new LocationReference( return new LocationReference(
this.X * this.ChunkSize, this.x * this.ChunkSize,
this.Y * this.ChunkSize this.y * this.ChunkSize
); );
} }
/**
* Whether this chunk reference references the same chunk as another
* chunk reference.
* @param {ChunkReference} otherChunkRef The other chunk reference to
* compare this chunk reference to.
* @return {boolean}
*/
is(otherChunkRef)
{
return this.x === otherChunkRef.x &&
this.y === otherChunkRef.y &&
this.ChunkSize === otherChunkRef.ChunkSize; // The chunk size should always be the same, but you never know :P
}
/** /**
* This LocationReference represented as a string. * This LocationReference represented as a string.
* @return {string} This LocationReference as a string. * @return {string} This LocationReference as a string.
*/ */
toString() toString()
{ {
return `ChunkReference: ${this.ChunkSize} x (${this.X}, ${this.Y})`; return `ChunkReference: ${this.ChunkSize} x (${this.x}, ${this.y})`;
} }
} }