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

[client] Fix issues with fetching chunks

This commit is contained in:
Starbeamrainbowlabs 2017-12-15 15:45:49 +00:00
parent e1ba8f9a18
commit b4bd60ab59
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
6 changed files with 42 additions and 15 deletions

View file

@ -96,6 +96,9 @@ class BoardWindow extends EventEmitter
this.displayGrid = this.displayGrid ? false : true; this.displayGrid = this.displayGrid ? false : true;
console.info(`[BoardWindow/KeyboardHandler] Grid display set to ${this.displayGrid ? "on" : "off"}`); console.info(`[BoardWindow/KeyboardHandler] Grid display set to ${this.displayGrid ? "on" : "off"}`);
}).bind(this)); }).bind(this));
this.keyboard.on("keyup-x", (function(event) {
console.debug("Chunk Cache: ", this.chunkCache.cache);
}).bind(this));
this.keyboard.on("keyup-left", (function(event) { this.keyboard.on("keyup-left", (function(event) {
this.interface.seekColour("backwards"); this.interface.seekColour("backwards");
}).bind(this)); }).bind(this));

View file

@ -21,14 +21,22 @@ class ChunkCache
/** /**
* Fetches the chunk with the specified chunk reference. * Fetches the chunk with the specified chunk reference.
* @param {ChunkReference} chunkRef The chunk reference of the chunk to fetch. * @param {ChunkReference} chunkRef The chunk reference of the chunk to fetch.
* @param {bool} quiet Whether to be quiet if the chunk wasn't found.
* @return {Chunk|null} The requested chunk, or null if it isn't present in the cache. * @return {Chunk|null} The requested chunk, or null if it isn't present in the cache.
*/ */
fetchChunk(chunkRef) fetchChunk(chunkRef, quiet)
{ {
// Return null if we don't currently have that chunk in storage
if(!this.cache.has(chunkRef.toString())) if(!this.cache.has(chunkRef.toString()))
return null; return null;
return this.cache.get(chunkRef.toString()); let requestedChunk = this.cache.get(chunkRef.toString());
if(!(requestedChunk instanceof Chunk)) {
if(!quiet)
console.warn(`Attempt to access a chunk at ${chunkRef} that's not loaded yet.`);
return null;
}
return requestedChunk;
} }
/** /**
@ -71,7 +79,7 @@ class ChunkCache
add(chunkData, override = false) add(chunkData, override = false)
{ {
var hasChunk = this.cache.has(chunkData.chunkRef.toString()) && var hasChunk = this.cache.has(chunkData.chunkRef.toString()) &&
this.cache.get(chunkData.chunkRef.toString()).requestedFromServer; this.cache.get(chunkData.chunkRef.toString()) instanceof Chunk;
if(!override && hasChunk) if(!override && hasChunk)
throw new Error("Error: We already have a chunk at that location stored."); throw new Error("Error: We already have a chunk at that location stored.");
@ -81,6 +89,11 @@ class ChunkCache
return !hasChunk; return !hasChunk;
} }
/**
* Updates the chunk cache ready for the next frame.
* @param {number} dt The amount of time, in milliseconds, since that last frame was rendered.
* @param {viewport_object} visibleArea The area that's currently visible on-screen.
*/
update(dt, visibleArea) update(dt, visibleArea)
{ {
let chunkSize = this.boardWindow.gridSize; let chunkSize = this.boardWindow.gridSize;
@ -140,8 +153,8 @@ class ChunkCache
cx / chunkSize, cy / chunkSize cx / chunkSize, cy / chunkSize
); );
let chunk = this.cache.get(cChunk.toString()); let chunk = this.fetchChunk(cChunk, true);
if(typeof chunk != "undefined" && !chunk.requestedFromServer) if(chunk != null)
chunk.render(canvas, context, this, chunkArea); chunk.render(canvas, context, this, chunkArea);
if(this.showRenderedChunks) { if(this.showRenderedChunks) {

View file

@ -2,6 +2,8 @@
import Vector from './Utilities/Vector.js'; import Vector from './Utilities/Vector.js';
import ChunkReference from './ChunkReference.js';
class CursorSyncer class CursorSyncer
{ {
constructor(inBoardWindow, syncFrequency) constructor(inBoardWindow, syncFrequency)
@ -22,6 +24,15 @@ class CursorSyncer
this.absCursorPosition = new Vector(0, 0); this.absCursorPosition = new Vector(0, 0);
} }
get chunkRefUnderCursor()
{
return new ChunkReference(
this.boardWindow.currentPlaneName,
Math.floor(locRef.x / this.boardWindow.gridSize),
Math.floor(locRef.y / this.boardWindow.gridSize)
);
}
setup() setup()
{ {
// The last time we sent a cursor update to the server. // The last time we sent a cursor update to the server.
@ -31,8 +42,8 @@ class CursorSyncer
this.cursorPosition.x = event.clientX; this.cursorPosition.x = event.clientX;
this.cursorPosition.y = event.clientY; this.cursorPosition.y = event.clientY;
this.absCursorPosition.x = this.boardWindow.viewport.x + this.cursorPosition.x; this.absCursorPosition.x = this.boardWindow.viewport.x + this.cursorPosition.x * 1/this.boardWindow.viewport.zoomLevel;
this.absCursorPosition.y = this.boardWindow.viewport.y + this.cursorPosition.y; this.absCursorPosition.y = this.boardWindow.viewport.y + this.cursorPosition.y * 1/this.boardWindow.viewport.zoomLevel;
setTimeout((function() { setTimeout((function() {
// Throttle the cursor updates we send to the server - a high // Throttle the cursor updates we send to the server - a high

View file

@ -219,8 +219,10 @@ class Interface extends EventEmitter
updateDebugInfo(dt) updateDebugInfo(dt)
{ {
this.debugDisplay.querySelector("#debug-framespacing").value = `${dt}ms`;
this.debugDisplay.querySelector("#debug-viewport").value = this.boardWindow.viewport; this.debugDisplay.querySelector("#debug-viewport").value = this.boardWindow.viewport;
if(typeof this.boardWindow.cursorSyncer != "undefined")
this.debugDisplay.querySelector("#debug-cursor").value = `${this.boardWindow.cursorSyncer.cursorPosition} -> ${this.boardWindow.cursorSyncer.absCursorPosition}`;
this.debugDisplay.querySelector("#debug-framespacing").value = `${dt}ms`;
} }
} }

View file

@ -139,12 +139,8 @@ class Pencil
if(!this.pencilDown) if(!this.pencilDown)
return; // Only erase when the pencil is down return; // Only erase when the pencil is down
let locRef = this.boardWindow.cursorSyncer.absCursorPosition; let cursorPos = this.boardWindow.cursorSyncer.absCursorPosition;
let hoverChunkRef = new ChunkReference( let hoverChunkRef = this.boardWindow.cursorSyncer.chunkRefUnderCursor;
this.boardWindow.currentPlaneName,
Math.floor(locRef.x / this.boardWindow.gridSize),
Math.floor(locRef.y / this.boardWindow.gridSize)
);
let hoverChunk = this.boardWindow.chunkCache.fetchChunk(hoverChunkRef); let hoverChunk = this.boardWindow.chunkCache.fetchChunk(hoverChunkRef);
if(hoverChunk == null) { if(hoverChunk == null) {
@ -152,7 +148,7 @@ class Pencil
break; // If it's null, then we haven't received it yet from the server break; // If it's null, then we haven't received it yet from the server
} }
let lineToErase = hoverChunk.getLineUnderPoint(locRef); let lineToErase = hoverChunk.getLineUnderPoint(cursorPos);
if(lineToErase == null) { if(lineToErase == null) {
console.debug(`No line found at abs ${locRef}.`); console.debug(`No line found at abs ${locRef}.`);
break; // There's no line underneath the cursor atm break; // There's no line underneath the cursor atm

View file

@ -173,6 +173,8 @@
<aside id="debuginfo"> <aside id="debuginfo">
<label>Viewport:</label> <output id="debug-viewport" class="debug-value">?</output> <label>Viewport:</label> <output id="debug-viewport" class="debug-value">?</output>
<br /> <br />
<label>Cursor:</label> <output id="debug-cursor" class="debug-value">?</output>
<br />
<label>Frame spacing:</label> <output id="debug-framespacing" class="debug-value">?</output> <label>Frame spacing:</label> <output id="debug-framespacing" class="debug-value">?</output>
<br /> <br />
<small><em><kbd>g</kbd>: toggle grid, <kbd>c</kbd>: toggle chunk higlighting</em></small> <small><em><kbd>g</kbd>: toggle grid, <kbd>c</kbd>: toggle chunk higlighting</em></small>