gravity_test.js
This commit is contained in:
parent
6860344777
commit
cf3b95910e
14
index.html
14
index.html
|
|
@ -13048,6 +13048,10 @@ onAddElementList = [];
|
|||
function onAddElement(callback) {
|
||||
onAddElementList.push(callback);
|
||||
}
|
||||
validateMovesList = [];
|
||||
function validateMoves(callback) {
|
||||
validateMovesList.push(callback);
|
||||
}
|
||||
canvasLayers = {
|
||||
// bg: document.createElement("canvas"),
|
||||
pixels: document.createElement("canvas"),
|
||||
|
|
@ -13764,6 +13768,16 @@ function langKey(key,fallback,template) {
|
|||
function tryMove(pixel,nx,ny,leaveBehind,force) {
|
||||
if (pixel.drag && force !== true) { return true; }
|
||||
if (pixel.del) { return false }
|
||||
if (validateMovesList.length !== 0 && force !== true) {
|
||||
for (let i = 0; i < validateMovesList.length; i++) {
|
||||
const result = validateMovesList[i](pixel,nx,ny);
|
||||
if (result === false) return false;
|
||||
else if (Array.isArray(result)) {
|
||||
nx = result[0];
|
||||
ny = result[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
var info = elements[pixel.element];
|
||||
var oob = outOfBounds(nx,ny);
|
||||
if (isEmpty(nx,ny,false,oob)) { // If coords is empty, move to coords
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
// validateMoves((pixel,nx,ny) => {
|
||||
// if (pixel.y-ny < 0) { //goes down usually
|
||||
// return false;
|
||||
// }
|
||||
// })
|
||||
|
||||
validateMoves((pixel,nx,ny) => {
|
||||
if (elements[pixel.element].isGas) return true;
|
||||
if (true || pixel.y-ny < 0) { //goes down usually
|
||||
|
||||
nx = 0;
|
||||
ny = 0;
|
||||
|
||||
const centerX = Math.floor(width/2);
|
||||
const centerY = Math.floor(height/2);
|
||||
|
||||
let diffX = centerX-pixel.x;
|
||||
let diffY = centerY-pixel.y;
|
||||
|
||||
let dirX = 0;
|
||||
let dirY = 0;
|
||||
if (Math.abs(diffX) > Math.abs(diffY)) {
|
||||
dirX = Math.sign(diffX);
|
||||
}
|
||||
else {
|
||||
dirY = Math.sign(diffY);
|
||||
}
|
||||
|
||||
if (Math.random() < Math.abs(diffX)/100) diffX = Math.sign(diffX);
|
||||
else diffX = 0;
|
||||
if (Math.random() < Math.abs(diffY)/100) diffY = Math.sign(diffY);
|
||||
else diffY = 0;
|
||||
|
||||
if ((diffX || diffY) && !isEmpty(pixel.x+diffX,pixel.y+diffY)) {
|
||||
if (dirX !== 0) {
|
||||
diffY = Math.random() < 0.5 ? 1 : -1;
|
||||
}
|
||||
else if (dirY !== 0) {
|
||||
diffX = Math.random() < 0.5 ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
// if (!(pixel.y-ny)) {
|
||||
// diffX += pixel.y-ny;
|
||||
// diffY += pixel.x-nx;
|
||||
// }
|
||||
|
||||
return [pixel.x+diffX,pixel.y+diffY];
|
||||
|
||||
}
|
||||
})
|
||||
Loading…
Reference in New Issue