1
0
Fork 0

[client] Write ChunkCache renderer method renderVisible()

This commit is contained in:
Starbeamrainbowlabs 2017-04-30 14:40:53 +01:00
parent 4e2a27df94
commit 5d716fe925
2 changed files with 73 additions and 2 deletions

View File

@ -30,6 +30,38 @@ class Chunk
return true;
return false;
}
update(dt)
{
}
render(canvas, context)
{
context.save();
for(let line of this.lines)
{
context.beginPath();
context.moveTo(
line.Points[0].x - this.chunkRef.x,
line.Points[0].y - this.chunkRef.y
);
for(let i = 1; i < line.Points.length; i++)
{
context.lineTo(
line.Points[i].x - this.chunkRef.x,
line.Points[i].y - this.chunkRef.y
);
}
context.lineWidth = line.Width;
context.strokeStyle = line.Colour;
context.stroke();
}
context.restore();
}
}
export default Chunk;

View File

@ -1,9 +1,13 @@
"use strict";
import ChunkReference from './ChunkReference.js';
import Rectangle from './Utilities/Rectangle.js';
class ChunkCache
{
constructor()
constructor(inBoardWindow)
{
this.boardWindow = inBoardWindow;
this.cache = new Map();
}
@ -16,7 +20,42 @@ class ChunkCache
if(this.cache.contains(chunkData.chunkRef.toString()))
throw new Error("Error: We already have a chunk at that location stored.");
this.cache.set(chunkData.toString(), chunkData);
this.cache.set(chunkData.chunkRef.toString(), chunkData);
}
/**
* Renders the specified area to the given canvas with the given context.
* @param {Rectangle} visibleArea The area to render.
* @param {number} chunkSize The size of the chunks on the current plane.
* @param {HTMLCanvasElement} canvas The canvas to draw on.
* @param {CanvasRenderingContext2D} context The rendering context to
* use to draw on the canvas.
*/
renderVisible(visibleArea, chunkSize, canvas, context)
{
context.save();
chunkArea = new Rectangle(
Math.floor(visibleArea.x / chunkSize) * chunkSize,
Math.floor(visibleArea.y / chunkSize) * chunkSize,
Math.floor((visibleArea.x + visibleArea.width) / chunkSize) * chunkSize,
Math.floor((visibleArea.y + visibleArea.height) / chunkSize) * chunkSize
);
for(let cx = chunkArea.x; cx <= chunkArea.x + chunkArea.width; cx += chunkSize)
{
for(let cy = chunkArea.y; cy <= chunkArea.y + chunkArea.height; cy += chunkSize)
{
let cChunk = new ChunkReference(
this.boardWindow.currentPlaneName,
cx, cy
);
let chunk = this.cache.get(cChunk.toString());
chunk.render(canvas, context);
}
}
context.restore();
}
}