From e6d734d4ebcaf9ca6b2b4a1083347eb6a7d6cbc4 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Wed, 28 Feb 2018 18:01:34 +0100 Subject: [PATCH] remoteMap place rotated items, fix selection box offset --- sharedPlugins/remoteMap/index.js | 4 ++-- static/remoteMap.js | 11 ++++++++--- static/remoteMap/itemSelection.js | 18 +++++++++++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/sharedPlugins/remoteMap/index.js b/sharedPlugins/remoteMap/index.js index f13d7771..691218fe 100644 --- a/sharedPlugins/remoteMap/index.js +++ b/sharedPlugins/remoteMap/index.js @@ -69,8 +69,8 @@ class remoteMap { // add entity to the chunkStore database because create_entity does not trigger any events let name = req.name; let entity = {name}; - if(req.direction){ - entity.direction = req.direction; + if(typeof req.direction == "string"){ + entity.rot = req.direction; } this.addEntity(req.position.x, req.position.y, entity); } diff --git a/static/remoteMap.js b/static/remoteMap.js index e5dd56ba..f0ad06f5 100644 --- a/static/remoteMap.js +++ b/static/remoteMap.js @@ -26,7 +26,8 @@ var itemSelectorGUI = new itemSelector("#remoteMapItemSelector", [ {name:"stone-furnace"}, {name:"accumulator"}, {name:"electric-furnace"}, - {name:"inserter"} + {name:"inserter"}, + {name:"express-transport-belt"}, ]); if(!localStorage.remoteMapConfig || localStorage.remoteMapConfig == "[object Object]"){ @@ -289,6 +290,7 @@ selectionCanvas.addEventListener('mousemove', function(evt) { }, false); selectionCanvas.addEventListener("mousedown", function(evt) { if(evt.which === 1) window.mousePos.clicked = true; + if(evt.which === 3) window.mousePos.rightClicked = true; // place item in world by sending it with websockets and shit let tileSize = remoteMapConfig.tileSize; @@ -298,6 +300,7 @@ selectionCanvas.addEventListener("mousedown", function(evt) { x: Math.floor((playerPosition.x + window.mousePos.x) / tileSize), y: Math.floor((playerPosition.y + window.mousePos.y) / tileSize), }, + direction: itemSelectorGUI.getItem().direction.toString(), }; if(evt.which === 1){ // left click to place console.log(JSON.stringify(entity)); @@ -310,6 +313,7 @@ selectionCanvas.addEventListener("mousedown", function(evt) { }); selectionCanvas.addEventListener("mouseup", function(evt) { if(evt.which === 1) window.mousePos.clicked = false; + if(evt.which === 3) window.mousePos.rightClicked = false; }); function renderLoop(){ // render game canvas @@ -371,14 +375,15 @@ function renderLoop(){ selectionCtx.lineWidth = remoteMapConfig.tileSize / 8; selectionCtx.strokeStyle="yellow"; if(mousePosition.clicked) selectionCtx.strokeStyle = "red"; + if(mousePosition.rightClicked) selectionCtx.strokeStyle = "blue"; let tileSize = remoteMapConfig.tileSize let halfTile = tileSize / 2; - let tilePosition = { + let tilePosition = { // position in world coordinates (same as used by factorio) x: Math.floor((playerPosition.x + mousePosition.x) / tileSize), y: Math.floor((playerPosition.y + mousePosition.y) / tileSize), } // Long thing to correct for offset between playerPosition, cachePosition (tile grid) and mouse position and draw the box so it aligns with tiles. - selectionCtx.rect(mousePosition.x - mousePosition.x % tileSize - playerPosition.x % tileSize, mousePosition.y - mousePosition.y % tileSize - playerPosition.y % tileSize, tileSize, tileSize); + selectionCtx.rect(tilePosition.x * tileSize - playerPosition.x, tilePosition.y * tileSize - playerPosition.y, tileSize, tileSize); selectionCtx.stroke(); } } diff --git a/static/remoteMap/itemSelection.js b/static/remoteMap/itemSelection.js index 4e7baa13..0f74847f 100644 --- a/static/remoteMap/itemSelection.js +++ b/static/remoteMap/itemSelection.js @@ -4,6 +4,7 @@ export class itemSelector { this.container = document.querySelector(containerSelector); if(!this.container) throw new Error("Could not find container "+container); this.entities = entities; + this.direction = 0; // populate container let cont = this.container; @@ -17,6 +18,12 @@ export class itemSelector { this.entities.forEach(entity => HTML += ""); HTML += ""; HTML += ""; + // direction selector + HTML += "
"; + HTML += "

Direction:

"; + HTML += "

Press R to rotate

"; + HTML += "

0

" + HTML += "
"; // write styles let styles = { @@ -42,9 +49,18 @@ export class itemSelector { HTML += "}"; } cont.innerHTML += HTML + ""; + + // handle direction keyboard event + Mousetrap.bind('r', ()=>this.rotate()); + } + rotate(){ + this.direction = (this.direction + 2) % 8; + // update display + document.querySelector("#directionDisplay").innerHTML = this.direction; } getItem(){ let name = document.querySelector(this.containerSelector+" > .itemSelector > select").value; - return {name}; + let direction = this.direction; + return {name, direction}; } } \ No newline at end of file