Entity based mapping has gotten to the renderer

This commit is contained in:
Danielv123 committed 2018-01-03 14:17:33 +01:00
1 parent 1b405b8701
commit 9ae20886b3
6 files changed
+106 -14

No files matched your search

+48 -3
View File
@@ -4,8 +4,12 @@ socket.on('hello', function (data) {
socket.emit("registerMapRequester", {instanceID: '1611985668'});
socket.on("displayChunk", function(chunk){
console.log(chunk)
drawChunk(chunk, ctx)
console.log(chunk);
drawChunk(chunk, ctx);
});
socket.on("displayEntity", function(entity){
console.log("Displaying entity "+JSON.stringify(entity));
drawEntity(entity, ctx);
});
});
@@ -32,6 +36,47 @@ setTimeout(function(){
entityImages = {};
entityImages["express-transport-belt"] = new Image();
entityImages["express-transport-belt"].src = 'https://wiki.factorio.com/images/Express_transport_belt.png';
// map view position, top left corner (or another corner?)
playerPosition = {
x:0,
y:0,
}
function requestMapDraw(){
let xLow = Math.floor(playerPosition.x % 16);
let yLow = Math.floor(playerPosition.y % 16);
let xHigh = xLow+64;
let yHigh = yLow+64;
for(let x = xLow; x < xHigh; x++){
for(let y = yLow; y < yHigh; y++){
socket.emit("requestEntity", {x, y, instanceID: '1611985668'});
}
}
}
function drawEntity(entity, ctx){
if(entity.x && entity.y){
if(entity.entity && entity.entity.name && typeof entity.entity.name == "string"){
let name = entity.entity.name;
let xPos = (entity.x * 16) - playerPosition.x;
let yPos = (entity.y * 16) - playerPosition.y;
if(!entityImages[name]){
// download the entityImages
entityImages[name] = new Image();
entityImages[name].onload = function(){
ctx.drawImage(entityImages[name], xPos, yPos, 16, 16);
console.log("Drawing "+name+" at X: "+xPos+", Y: "+yPos);
}
entityImages[name].src = getImageFromName(name);
}
} else {
throw new Error(entity);
}
} else {
throw new Error("drawEntity on entity without x and y coordinates")
}
}
function drawChunk(chunk, ctx){
Object.keys(chunk.dataObject).forEach(xValue => {
let xRow = chunk.dataObject[xValue];
@@ -50,4 +95,4 @@ function drawChunk(chunk, ctx){
}
});
});
}
}