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:
Laetitia (O-01-67) 2022-12-16 21:23:15 -05:00 committed by GitHub
parent c990db1740
commit 737f48d092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 116 additions and 62 deletions

View File

@ -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;
} if(enabledMods.includes(onTryMoveIntoMod) && enabledMods.includes(libraryMod)) {
} //https://stackoverflow.com/a/60922255
return false; if(!enabledMods.includes("mods/mobs.js")) {
} headBodyObject = {
"head": "body",
};
};
elements.portal_in = { elements.portal_in = {
color: "#ee7f00", color: "#ee7f00",
tick: function(pixel) { properties: {
if(!pixel.portalArray) { pixel.portalArray = [] } _channel: 0,
if(!Array.isArray(pixel.portalArray)) { pixel.portalArray = [] } _correspondingPortals: null,
for (var i = 1; i < width; i++) { },
for (var j = 1; j < height; j++) { insulate: true,
if (!isEmpty(i,j)) { onTryMoveInto: function(pixel,otherPixel) {
if(pixelMap[i][j].element == "portal_out") { if(pixel._correspondingPortals == null) {
if(!includesArray(pixel.portalArray,[i,j])) { return;
pixel.portalArray.push([i,j]) };
} if(pixel._correspondingPortals.length <= 0) {
} return;
} };
if (isEmpty(i,j)) {
if(includesArray(pixel.portalArray,[i,j])) {
pixel.portalArray = pixel.portalArray.filter(dat => !includesArray(pixel.portalArray,[i,j]))
} var portal = randomChoice(pixel._correspondingPortals);
}
} var offset = {x: pixel.x - otherPixel.x, y: pixel.y - otherPixel.y}; //teleport destination's offset, inverted by subtraction
}
if(pixel.portalArray.length > 0) { var destination = {x: portal.x + offset.x, y: portal.y + offset.y};
randomDestination = pixel.portalArray[Math.floor((Math.random() * pixel.portalArray.length))]
if(!isEmpty(pixel.x-1,pixel.y) && !outOfBounds(pixel.x-1,pixel.y)) { var otherElement = otherPixel.element;
if(pixelMap[pixel.x-1][pixel.y].element != pixel.element) { var isHead = (typeof(headBodyObject[otherElement]) !== "undefined");
tryMove(pixelMap[pixel.x-1][pixel.y],(randomDestination[0] + 1),(randomDestination[1])) var isBody = (typeof(getKeyByValue(headBodyObject,otherElement)) !== "undefined");
} var isBipartite = xor(isHead,isBody); //a head being its own body will break the code
}
if(!isEmpty(pixel.x+1,pixel.y) && !outOfBounds(pixel.x+1,pixel.y)) { if(isBipartite) {
if(pixelMap[pixel.x+1][pixel.y].element != pixel.element) { if(isHead) {
tryMove(pixelMap[pixel.x+1][pixel.y],(randomDestination[0] - 1),(randomDestination[1])) var dead = otherPixel.dead;
} var body = pixelMap[otherPixel.x][otherPixel.y+1];
} if(body == undefined) { body = null };
if(!isEmpty(pixel.x,pixel.y-1) && !outOfBounds(pixel.x,pixel.y-1)) { if(!dead && (body !== null)) {
if(pixelMap[pixel.x][pixel.y-1].element != pixel.element) { if(offset.y == -1) {
tryMove(pixelMap[pixel.x][pixel.y-1],(randomDestination[0]),(randomDestination[1] + 1)) offset.y--;
} destination.y--;
} };
if(!isEmpty(pixel.x,pixel.y+1) && !outOfBounds(pixel.x,pixel.y+1)) { var headSpotIsEmpty = isEmpty(destination.x,destination.y,false);
if(pixelMap[pixel.x][pixel.y+1].element != pixel.element) { var bodySpotIsEmpty = isEmpty(destination.x,destination.y+1,false);
tryMove(pixelMap[pixel.x][pixel.y+1],(randomDestination[0]),(randomDestination[1] - 1)) 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", category: "machines",
state: "solid", state: "solid",
portalArray: [],
}, },
elements.portal_out = { elements.portal_out = {
color: "#2222ee", color: "#2222ee",
properties: {
channel: 0
},
insulate: true,
tick: function(pixel) {
pixel._channel = Math.floor(pixel.temp / 100);
},
behavior: behaviors.WALL, behavior: behaviors.WALL,
category: "machines", category: "machines",
state: "solid", 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).`)
};