implement block placing and destroying

This commit is contained in:
LabyStudio
2022-02-01 00:57:25 +01:00
parent 02c36a79ba
commit 4d93f2aeeb
13 changed files with 434 additions and 83 deletions
-1
View File
@@ -243,7 +243,6 @@ window.BoundingBox = class {
this.maxZ += z;
}
/**
* Create a new bounding box with the given offset
*
+4 -4
View File
@@ -38,8 +38,8 @@ window.EnumBlockFace = class {
let c = window.EnumBlockFace;
c.TOP = new EnumBlockFace(0, 1, 0);
c.BOTTOM = new EnumBlockFace(0, -1, 0);
c.NORTH = new EnumBlockFace(-1, 0, 0);
c.EAST = new EnumBlockFace(0, 0, -1);
c.SOUTH = new EnumBlockFace(1, 0, 0);
c.WEST = new EnumBlockFace(0, 0, 1);
c.NORTH = new EnumBlockFace(0, 0, -1);
c.EAST = new EnumBlockFace(1, 0, 0);
c.SOUTH = new EnumBlockFace(0, 0, 1);
c.WEST = new EnumBlockFace(-1, 0, 0);
}
@@ -0,0 +1,11 @@
window.MovingObjectPosition = class {
constructor(vector, face, x, y, z) {
this.vector = vector;
this.face = face;
this.x = x;
this.y = y;
this.z = z;
}
}
+18 -36
View File
@@ -1,49 +1,35 @@
window.Timer = class {
static MS_PER_SECOND = 1000;
static MAX_MS_PER_UPDATE = 1000;
static MAX_TICKS_PER_UPDATE = 100;
/**
* Timer to control the tick speed independently of the framerate
*
* @param ticksPerSecond Amount of ticks per second
*/
constructor(ticksPerSecond) {
this.MS_PER_SECOND = 1000;
this.MAX_MS_PER_UPDATE = 1000;
this.MAX_TICKS_PER_UPDATE = 100;
/**
* Amount of ticks per second
*/
// Amount of ticks per second
this.ticksPerSecond = ticksPerSecond;
/**
* Last time updated in nano seconds
*/
this.lastTime = this._nanoTime();
// Last time updated in milliseconds
this.lastTime = Date.now();
/**
* Scale the tick speed
*/
// Scale the tick speed
this.timeScale = 1.0;
/**
* Framerate of the advanceTime update
*/
// Framerate of the advanceTime update
this.fps = 0.0;
/**
* Passed time since last game update
*/
// Passed time since last game update
this.passedTime = 0.0;
/**
* The amount of ticks for the current game update.
* It's the passed time as an integer
*/
// The amount of ticks for the current game update.
// It's the passed time as an integer
this.ticks = 0;
/**
* The overflow of the current tick, caused by casting the passed time to an integer
*/
// The overflow of the current tick, caused by casting the passed time to an integer
this.partialTicks = 0;
}
@@ -53,7 +39,7 @@ window.Timer = class {
* Call this function in the main render loop of the game
*/
advanceTime() {
let now = this._nanoTime();
let now = Date.now();
let passedMs = now - this.lastTime;
// Store nano time of this update
@@ -61,24 +47,20 @@ window.Timer = class {
// Maximum and minimum
passedMs = Math.max(0, passedMs);
passedMs = Math.min(this.MAX_MS_PER_UPDATE, passedMs);
passedMs = Math.min(Timer.MAX_MS_PER_UPDATE, passedMs);
// Calculate fps
this.fps = this.MS_PER_SECOND / passedMs;
this.fps = Timer.MS_PER_SECOND / passedMs;
// Calculate passed time and ticks
this.passedTime += passedMs * this.timeScale * this.ticksPerSecond / this.MS_PER_SECOND;
this.passedTime += passedMs * this.timeScale * this.ticksPerSecond / Timer.MS_PER_SECOND;
this.ticks = parseInt(this.passedTime);
// Maximum ticks per update
this.ticks = Math.min(this.MAX_TICKS_PER_UPDATE, this.ticks);
this.ticks = Math.min(Timer.MAX_TICKS_PER_UPDATE, this.ticks);
// Calculate the overflow of the current tick
this.passedTime -= this.ticks;
this.partialTicks = this.passedTime;
}
_nanoTime() {
return Date.now();
}
}
+72
View File
@@ -0,0 +1,72 @@
window.Vector3 = class {
constructor(x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
addVector(x, y, z) {
return new Vector3(this.x + x, this.y + y, this.z + z);
}
squareDistanceTo(vec) {
let d0 = vec.x - this.x;
let d1 = vec.y - this.y;
let d2 = vec.z - this.z;
return d0 * d0 + d1 * d1 + d2 * d2;
}
/**
* Returns a new vector with x value equal to the second parameter, along the line between this vector and the
* passed in vector, or null if not possible.
*/
getIntermediateWithXValue(vec, x) {
let d0 = vec.x - this.x;
let d1 = vec.y - this.y;
let d2 = vec.z - this.z;
if (d0 * d0 < 1.0000000116860974E-7) {
return null;
} else {
let d3 = (x - this.x) / d0;
return d3 >= 0.0 && d3 <= 1.0 ? new Vector3(this.x + d0 * d3, this.y + d1 * d3, this.z + d2 * d3) : null;
}
}
/**
* Returns a new vector with y value equal to the second parameter, along the line between this vector and the
* passed in vector, or null if not possible.
*/
getIntermediateWithYValue(vec, y) {
let d0 = vec.x - this.x;
let d1 = vec.y - this.y;
let d2 = vec.z - this.z;
if (d1 * d1 < 1.0000000116860974E-7) {
return null;
} else {
let d3 = (y - this.y) / d1;
return d3 >= 0.0 && d3 <= 1.0 ? new Vector3(this.x + d0 * d3, this.y + d1 * d3, this.z + d2 * d3) : null;
}
}
/**
* Returns a new vector with z value equal to the second parameter, along the line between this vector and the
* passed in vector, or null if not possible.
*/
getIntermediateWithZValue(vec, z) {
let d0 = vec.x - this.x;
let d1 = vec.y - this.y;
let d2 = vec.z - this.z;
if (d2 * d2 < 1.0000000116860974E-7) {
return null;
} else {
let d3 = (z - this.z) / d2;
return d3 >= 0.0 && d3 <= 1.0 ? new Vector3(this.x + d0 * d3, this.y + d1 * d3, this.z + d2 * d3) : null;
}
}
}