Skip to content

Commit

Permalink
Converted placeable multidimensional array into object
Browse files Browse the repository at this point in the history
  • Loading branch information
camerongillette committed Dec 1, 2017
1 parent d2eeded commit 081e865
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<script src="vendor/factorio-blueprint.min.js"></script>
<script src="vendor/pako.js"></script>
<script src="vendor/base64.js"></script>
<script src="js/models/entity.js"></script>
<script src="js/script.js"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions js/models/entity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Entity{
constructor(imagePath, rotation, direction,width,height){
this.imagePath = imagePath;
this.rotation = rotation;
this.direction = direction;
this.width = width;
this.height = height;
}
}
24 changes: 16 additions & 8 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function GETfromUrl(){
return object;
},{});
}
var placeable = [
var placeable = factorioEntityObjectArrayFromMultiArray([
["assembling-machine-1.png", 1, 0, 3, 3],
["assembling-machine-2.png", 1, 0, 3, 3],
["assembling-machine-3.png", 1, 0, 3, 3],
Expand Down Expand Up @@ -103,7 +103,7 @@ var placeable = [
["decider-combinator.png", 1, 2, 2, 1],
["arithmetic-combinator.png", 1, 2, 2, 1],
["programmable-speaker.png", 0, 0, 1, 1],
["power-switch.png", 0, 0, 2, 2]];
["power-switch.png", 0, 0, 2, 2]]);

function createJSON() {
var jsonstring = '{"blueprint": {"icons": [{"signal": {"type": "item","name": "express-transport-belt"},"index": 1}],"entities": [';
Expand Down Expand Up @@ -447,12 +447,12 @@ function createItems() {
for (var i = 0; i < placeable.length; i++) {
var item = document.createElement("div");
item.setAttribute("class", "item");
url = placeable[i][0];
url = placeable[i].imagePath;
item.setAttribute("data-url", url);
item.setAttribute("data-r", placeable[i][1]);
item.setAttribute("data-direction", placeable[i][2]);
item.setAttribute("data-w", placeable[i][3]);
item.setAttribute("data-h", placeable[i][4]);
item.setAttribute("data-r", placeable[i].rotation);
item.setAttribute("data-direction", placeable[i].direction);
item.setAttribute("data-w", placeable[i].width);
item.setAttribute("data-h", placeable[i].height);
item.addEventListener('click', itemClick);
insertImg(item, url);
grid.appendChild(item);
Expand Down Expand Up @@ -528,7 +528,7 @@ function isBlocked(name, x, y) {

function getPlaceableData(name) {
for (var i in placeable) {
if (placeable[i][0].startsWith(name)) {
if (placeable[i].imagePath.startsWith(name)) {
return placeable[i];
}
}
Expand Down Expand Up @@ -582,3 +582,11 @@ function encode(json) {
var bstring = "0" + base64;
return bstring;
}

function factorioEntityObjectArrayFromMultiArray(sourceArray){
var objectArray = new Array();
for(var i = 0; i < sourceArray.length; i++){
objectArray.push(new Entity(sourceArray[i][0],sourceArray[i][1],sourceArray[i][2],sourceArray[i][3],sourceArray[i][4]));
}
return objectArray;
}

0 comments on commit 081e865

Please sign in to comment.