Rework of portals
* Portals now search currentPixels for suitable portals * Portals now support life through headBodyObject * Portals now use onTryMoveInto * Portals now support channels defined through floor((temp/100)) * Now depends on code_library.js and onTryMoveInto.js
This commit is contained in:
parent
c990db1740
commit
737f48d092
178
mods/portal.js
178
mods/portal.js
|
|
@ -1,66 +1,120 @@
|
||||||
function includesArray(parentArray, testArray) {
|
var modName = "mods/portal.js";
|
||||||
for (let i = 0; i < parentArray.length; i++) {
|
var onTryMoveIntoMod = "mods/onTryMoveInto.js";
|
||||||
if (parentArray[i].every(function(value, index) { return value === testArray[index]})) {
|
var libraryMod = "mods/code_library.js";
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
elements.portal_in = {
|
if(enabledMods.includes(onTryMoveIntoMod) && enabledMods.includes(libraryMod)) {
|
||||||
color: "#ee7f00",
|
//https://stackoverflow.com/a/60922255
|
||||||
tick: function(pixel) {
|
if(!enabledMods.includes("mods/mobs.js")) {
|
||||||
if(!pixel.portalArray) { pixel.portalArray = [] }
|
headBodyObject = {
|
||||||
if(!Array.isArray(pixel.portalArray)) { pixel.portalArray = [] }
|
"head": "body",
|
||||||
for (var i = 1; i < width; i++) {
|
};
|
||||||
for (var j = 1; j < height; j++) {
|
};
|
||||||
if (!isEmpty(i,j)) {
|
|
||||||
if(pixelMap[i][j].element == "portal_out") {
|
|
||||||
if(!includesArray(pixel.portalArray,[i,j])) {
|
|
||||||
pixel.portalArray.push([i,j])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isEmpty(i,j)) {
|
|
||||||
if(includesArray(pixel.portalArray,[i,j])) {
|
|
||||||
pixel.portalArray = pixel.portalArray.filter(dat => !includesArray(pixel.portalArray,[i,j]))
|
|
||||||
|
|
||||||
}
|
elements.portal_in = {
|
||||||
}
|
color: "#ee7f00",
|
||||||
}
|
properties: {
|
||||||
}
|
_channel: 0,
|
||||||
if(pixel.portalArray.length > 0) {
|
_correspondingPortals: null,
|
||||||
randomDestination = pixel.portalArray[Math.floor((Math.random() * pixel.portalArray.length))]
|
},
|
||||||
if(!isEmpty(pixel.x-1,pixel.y) && !outOfBounds(pixel.x-1,pixel.y)) {
|
insulate: true,
|
||||||
if(pixelMap[pixel.x-1][pixel.y].element != pixel.element) {
|
onTryMoveInto: function(pixel,otherPixel) {
|
||||||
tryMove(pixelMap[pixel.x-1][pixel.y],(randomDestination[0] + 1),(randomDestination[1]))
|
if(pixel._correspondingPortals == null) {
|
||||||
}
|
return;
|
||||||
}
|
};
|
||||||
if(!isEmpty(pixel.x+1,pixel.y) && !outOfBounds(pixel.x+1,pixel.y)) {
|
if(pixel._correspondingPortals.length <= 0) {
|
||||||
if(pixelMap[pixel.x+1][pixel.y].element != pixel.element) {
|
return;
|
||||||
tryMove(pixelMap[pixel.x+1][pixel.y],(randomDestination[0] - 1),(randomDestination[1]))
|
};
|
||||||
}
|
|
||||||
}
|
var portal = randomChoice(pixel._correspondingPortals);
|
||||||
if(!isEmpty(pixel.x,pixel.y-1) && !outOfBounds(pixel.x,pixel.y-1)) {
|
|
||||||
if(pixelMap[pixel.x][pixel.y-1].element != pixel.element) {
|
var offset = {x: pixel.x - otherPixel.x, y: pixel.y - otherPixel.y}; //teleport destination's offset, inverted by subtraction
|
||||||
tryMove(pixelMap[pixel.x][pixel.y-1],(randomDestination[0]),(randomDestination[1] + 1))
|
|
||||||
}
|
var destination = {x: portal.x + offset.x, y: portal.y + offset.y};
|
||||||
}
|
|
||||||
if(!isEmpty(pixel.x,pixel.y+1) && !outOfBounds(pixel.x,pixel.y+1)) {
|
var otherElement = otherPixel.element;
|
||||||
if(pixelMap[pixel.x][pixel.y+1].element != pixel.element) {
|
var isHead = (typeof(headBodyObject[otherElement]) !== "undefined");
|
||||||
tryMove(pixelMap[pixel.x][pixel.y+1],(randomDestination[0]),(randomDestination[1] - 1))
|
var isBody = (typeof(getKeyByValue(headBodyObject,otherElement)) !== "undefined");
|
||||||
}
|
var isBipartite = xor(isHead,isBody); //a head being its own body will break the code
|
||||||
}
|
|
||||||
}
|
if(isBipartite) {
|
||||||
|
if(isHead) {
|
||||||
|
var dead = otherPixel.dead;
|
||||||
|
var body = pixelMap[otherPixel.x][otherPixel.y+1];
|
||||||
|
if(body == undefined) { body = null };
|
||||||
|
if(!dead && (body !== null)) {
|
||||||
|
if(offset.y == -1) {
|
||||||
|
offset.y--;
|
||||||
|
destination.y--;
|
||||||
|
};
|
||||||
|
var headSpotIsEmpty = isEmpty(destination.x,destination.y,false);
|
||||||
|
var bodySpotIsEmpty = isEmpty(destination.x,destination.y+1,false);
|
||||||
|
if(headSpotIsEmpty && bodySpotIsEmpty) {
|
||||||
|
tryMove(otherPixel,destination.x,destination.y);
|
||||||
|
tryMove(body,destination.x,destination.y+1);
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
tryMove(otherPixel,destination.x,destination.y);
|
||||||
|
};
|
||||||
|
} else if(isBody) {
|
||||||
|
var dead = otherPixel.dead;
|
||||||
|
var head = pixelMap[otherPixel.x][otherPixel.y-1];
|
||||||
|
if(head == undefined) { head = null };
|
||||||
|
if(!dead && (head !== null)) {
|
||||||
|
if(offset.y == 1) {
|
||||||
|
offset.y++;
|
||||||
|
destination.y++;
|
||||||
|
};
|
||||||
|
var headSpotIsEmpty = isEmpty(destination.x,destination.y-1,false);
|
||||||
|
var bodySpotIsEmpty = isEmpty(destination.x,destination.y,false);
|
||||||
|
if(headSpotIsEmpty && bodySpotIsEmpty) {
|
||||||
|
tryMove(head,destination.x,destination.y-1);
|
||||||
|
tryMove(otherPixel,destination.x,destination.y);
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
tryMove(otherPixel,destination.x,destination.y);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
tryMove(otherPixel,destination.x,destination.y);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
tick: function(pixel) {
|
||||||
|
pixel._channel = Math.floor(pixel.temp / 100);
|
||||||
|
|
||||||
|
pixel._correspondingPortals = currentPixels.filter(function(pixelToCheck) {
|
||||||
|
return (
|
||||||
|
pixelToCheck.element == "portal_out" &&
|
||||||
|
pixelToCheck._channel == pixelChannel
|
||||||
|
);
|
||||||
|
},pixelChannel=pixel._channel);
|
||||||
|
|
||||||
|
for(i = 0; i < pixel._correspondingPortals.length; i++) {
|
||||||
|
pixel._correspondingPortals[i] = {x: pixel._correspondingPortals[i].x, y: pixel._correspondingPortals[i].y};
|
||||||
|
};
|
||||||
|
|
||||||
|
//pixel.tempdebug = JSON.stringify(pixel._correspondingPortals);
|
||||||
|
},
|
||||||
|
category: "machines",
|
||||||
|
state: "solid",
|
||||||
},
|
},
|
||||||
category: "machines",
|
|
||||||
state: "solid",
|
|
||||||
portalArray: [],
|
|
||||||
},
|
|
||||||
|
|
||||||
elements.portal_out = {
|
elements.portal_out = {
|
||||||
color: "#2222ee",
|
color: "#2222ee",
|
||||||
behavior: behaviors.WALL,
|
properties: {
|
||||||
category: "machines",
|
channel: 0
|
||||||
state: "solid",
|
},
|
||||||
}
|
insulate: true,
|
||||||
|
tick: function(pixel) {
|
||||||
|
pixel._channel = Math.floor(pixel.temp / 100);
|
||||||
|
},
|
||||||
|
behavior: behaviors.WALL,
|
||||||
|
category: "machines",
|
||||||
|
state: "solid",
|
||||||
|
insulate: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(!enabledMods.includes(libraryMod)) { enabledMods.splice(enabledMods.indexOf(modName),0,libraryMod) };
|
||||||
|
if(!enabledMods.includes(onTryMoveIntoMod)) { enabledMods.splice(enabledMods.indexOf(modName),0,onTryMoveIntoMod) };
|
||||||
|
localStorage.setItem("enabledMods", JSON.stringify(enabledMods));
|
||||||
|
alert(`The "${libraryMod}" and "${onTryMoveIntoMod}" mods are all required; any missing mods in this list have been automatically inserted (reload for this to take effect).`)
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue