slight optimizations
This commit is contained in:
parent
0fb19d45ad
commit
66d0642006
5 changed files with 77 additions and 71 deletions
60
js/core.js
60
js/core.js
|
@ -108,39 +108,36 @@ Canvas.prototype.render = function () {
|
|||
let imgData = this.ctx.getImageData(0, 0, this.width, this.height);
|
||||
let pixels = imgData.data;
|
||||
|
||||
let int = Math.ceil(this.width * this.height / 8);
|
||||
for (let j = 0; j < 8; j++) {
|
||||
let that = this;
|
||||
(async function () {
|
||||
for (let i = j * int; i < (j + 1) * int; i++) {
|
||||
if (i > that.width * that.height) break;
|
||||
let x = Math.floor(i / that.height);
|
||||
let y = i % that.height;
|
||||
let int = (this.width * this.height)
|
||||
let that = this;
|
||||
for (let i = 0; i < int; i++) {
|
||||
let x = Math.floor(i / that.height);
|
||||
let y = i % that.height;
|
||||
|
||||
let i2 = x + y * that.width;
|
||||
let i2 = x + y * that.width;
|
||||
|
||||
let block = mainTiles.tiles[that.blocks[i]];
|
||||
let block = mainTiles.tiles[that.blocks[i]];
|
||||
|
||||
let temp = that.temp[i];
|
||||
let temp = that.temp[i];
|
||||
|
||||
if (block.color[0] != -1) {
|
||||
if (block.color[0] != -1) {
|
||||
|
||||
let val = (temp + 310) / 310;
|
||||
if (val < -2.861) val = -2.861;
|
||||
let val = (temp + 310) / 310;
|
||||
if (val < -2.861) val = -2.861;
|
||||
|
||||
pixels[i2 * 4] = (block.color[0] - temp / 1e28) * val;
|
||||
pixels[i2 * 4 + 1] = (block.color[1] - temp / 1e28) * (val * 0.259 + 0.741);
|
||||
pixels[i2 * 4 + 2] = (block.color[2] - temp / 1e28) * (val * 0.023 + 0.977);
|
||||
pixels[i2 * 4 + 3] = block.color[3] * 255 + Math.abs(val - 1) * 100 || 255;
|
||||
} else {
|
||||
let lg = Math.log(temp);
|
||||
pixels[i2 * 4] = ((handler.ticks * 69) % (lg * 0.6969)) * 255 / (lg * 0.6969);
|
||||
pixels[i2 * 4 + 1] = ((handler.ticks * 69) % (lg * 0.420420)) * 255 / (lg * 0.420420);
|
||||
pixels[i2 * 4 + 2] = ((handler.ticks * 69) % (lg * 0.13371337)) * 255 / (lg * 0.13371337);
|
||||
pixels[i2 * 4 + 3] = 255;
|
||||
}
|
||||
}
|
||||
})()
|
||||
let td = temp / 1e28;
|
||||
|
||||
pixels[i2 * 4] = (block.color[0] - td) * val;
|
||||
pixels[i2 * 4 + 1] = (block.color[1] - td) * (val * 0.259 + 0.741);
|
||||
pixels[i2 * 4 + 2] = (block.color[2] - td) * (val * 0.023 + 0.977);
|
||||
pixels[i2 * 4 + 3] = block.color[3] * 255 + Math.abs(val - 1) * 100 || 255;
|
||||
} else {
|
||||
let lg = Math.log(temp);
|
||||
pixels[i2 * 4] = ((handler.ticks * 69) % (lg * 0.6969)) * 255 / (lg * 0.6969);
|
||||
pixels[i2 * 4 + 1] = ((handler.ticks * 69) % (lg * 0.420420)) * 255 / (lg * 0.420420);
|
||||
pixels[i2 * 4 + 2] = ((handler.ticks * 69) % (lg * 0.13371337)) * 255 / (lg * 0.13371337);
|
||||
pixels[i2 * 4 + 3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: clean up */
|
||||
|
@ -235,13 +232,8 @@ var canvas = new Canvas(Math.floor(window.innerWidth / 4.5), Math.floor(window.i
|
|||
var handler = new TickHandler(canvas);
|
||||
|
||||
setInterval(() => {
|
||||
if (canvas.clicked) canvas.click()
|
||||
canvas.render();
|
||||
if (canvas.stopNow) return;
|
||||
handler.tick();
|
||||
}, 1000 / 60);
|
||||
|
||||
|
||||
setInterval(() => {
|
||||
if (canvas.stopNow) return;
|
||||
if (canvas.clicked) canvas.click();
|
||||
this.canvas.render();
|
||||
}, 1000 / 60);
|
|
@ -247,6 +247,9 @@ let air = mainTiles.resolveID('Vanilla/Air', 'Air');
|
|||
|
||||
setTimeout(function () {
|
||||
for (let tile in mainTiles.tiles) {
|
||||
mainTiles.tiles[tile].hook ? mainTiles.tiles[tile].hook() : false;
|
||||
let h = mainTiles.tiles[tile].hook;
|
||||
for (let hh of h) {
|
||||
hh();
|
||||
}
|
||||
}
|
||||
}, 400)
|
||||
|
|
|
@ -5,81 +5,87 @@
|
|||
fluid: Spread of substance
|
||||
saturation: Maximum mass for gravity reactions to occur
|
||||
*/
|
||||
let gravityTable = [];
|
||||
|
||||
function gravity(event, mass, fluid, saturation) {
|
||||
if (event.type != 'tick') return;
|
||||
|
||||
|
||||
let cx = event.data[0];
|
||||
let cy = event.data[1];
|
||||
let chunks = event.canvas;
|
||||
|
||||
let dir = [0, 0];
|
||||
let force = [0,0];
|
||||
let dirX = 0;
|
||||
let dirY = 0;
|
||||
let forceX = 0;
|
||||
let forceY = 0;
|
||||
let density = 0;
|
||||
|
||||
let currBlock = chunks.getBlock(cx, cy);
|
||||
|
||||
|
||||
for (let x = -1; x < 2; x ++) {
|
||||
for (let x = -1; x < 2; x++) {
|
||||
let x2 = x / fluid;
|
||||
let x3 = x2 * x2;
|
||||
for (let y = -1; y < 2; y++) {
|
||||
let blok = chunks.getBlock(cx + x, cy + y);
|
||||
|
||||
|
||||
if (blok == -1) continue;
|
||||
|
||||
let mass2 = mainTiles.tiles[blok].attributes.mass;
|
||||
let mass2 = gravityTable[blok];
|
||||
|
||||
density += mass2;
|
||||
if (density > saturation) break;
|
||||
|
||||
if (blok == currBlock) continue;
|
||||
if (blok == currBlock || mass == mass2) continue;
|
||||
|
||||
let massDiff = (mass / mass2) - (mass2 / mass);
|
||||
let x2 = x / fluid;
|
||||
let dirDiff = (y - 1 + fluid) / (1/8 * (x2 * x2 - y * y + 8) * (x2 * x2 + y * y));
|
||||
|
||||
|
||||
if (Math.abs(massDiff) < 0.001) continue;
|
||||
let y3 = y * y;
|
||||
let dirDiff = (y - 1 + fluid);
|
||||
if (y == 0 && x == 0) dirDiff = 0;
|
||||
if (isNaN(dirDiff)) dirDiff = 0;
|
||||
if (dirDiff != 0)
|
||||
dirDiff /= (1 / 8 * (x3 - y3 + 8) * (x3 + y3));
|
||||
|
||||
force[0] += massDiff * x2;
|
||||
force[1] += massDiff * dirDiff * y;
|
||||
if (isNaN(dirDiff)) dirDiff = 0;
|
||||
|
||||
forceX += massDiff * x2;
|
||||
forceY += massDiff * dirDiff * y;
|
||||
}
|
||||
if (density > saturation) break;
|
||||
}
|
||||
|
||||
dir[0] = (Math.abs(force[0]) < .5) ? 0 : Math.sign(force[0]);
|
||||
dir[1] = (Math.abs(force[1]) < .5) ? 0 : Math.sign(force[1]);
|
||||
dirX = (Math.abs(forceX) < .5) ? 0 : Math.sign(forceX);
|
||||
dirY = (Math.abs(forceY) < .5) ? 0 : Math.sign(forceY);
|
||||
|
||||
if (density > saturation ) {
|
||||
if (density > saturation) {
|
||||
return;
|
||||
}
|
||||
|
||||
let offBlock = chunks.getBlock(cx + dir[0], cy + dir[1]);
|
||||
let offBlock = chunks.getBlock(cx + dirX, cy + dirY);
|
||||
|
||||
if (currBlock == offBlock && density <= saturation) {
|
||||
dir[0] = Math.sign(force[0]);
|
||||
offBlock = chunks.getBlock(cx + dir[0], cy + dir[1]);
|
||||
dirX = Math.sign(forceX);
|
||||
offBlock = chunks.getBlock(cx + dirX, cy + dirY);
|
||||
}
|
||||
|
||||
if (currBlock == -1 || offBlock == -1 || currBlock == offBlock ||offBlock == undefined || mainTiles.tiles[offBlock].attributes.saturation / 9 < mass || chunks.noTick[(cx+dir[0])*chunks.height + (cy+dir[1])]) return;
|
||||
if (currBlock == -1 || offBlock == -1 || currBlock == offBlock || offBlock == undefined || mainTiles.tiles[offBlock].attributes.saturation / 9 < mass || chunks.noTick[(cx + dirX) * chunks.height + (cy + dirY)]) return;
|
||||
|
||||
|
||||
|
||||
|
||||
if (!canGravity[offBlock]) return;
|
||||
|
||||
chunks.noTick[cx*chunks.height + cy] = true;
|
||||
chunks.noTick[(cx+dir[0])*chunks.height + (cy+dir[1])] = true;
|
||||
chunks.noTick[cx * chunks.height + cy] = true;
|
||||
chunks.noTick[(cx + dirX) * chunks.height + (cy + dirY)] = true;
|
||||
|
||||
chunks.setBlock(cx, cy, offBlock);
|
||||
chunks.setBlock(cx + dir[0], cy + dir[1], currBlock);
|
||||
chunks.setBlock(cx + dirX, cy + dirY, currBlock);
|
||||
|
||||
let t = chunks.getBlock(cx, cy,true);
|
||||
let t2 = chunks.getBlock(cx + dir[0], cy + dir[1],true);
|
||||
|
||||
if (t != undefined && t2 != undefined) {
|
||||
chunks.setBlock(cx, cy,t2, true);
|
||||
chunks.setBlock(cx + dir[0], cy + dir[1], t, true);
|
||||
}
|
||||
let t = chunks.getBlock(cx, cy, true);
|
||||
let t2 = chunks.getBlock(cx + dirX, cy + dirY, true);
|
||||
|
||||
chunks.setBlock(cx, cy, t2, true);
|
||||
chunks.setBlock(cx + dirX, cy + dirY, t, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -90,6 +96,10 @@ Tile.prototype.gravity = function (mass, fluid, saturation) {
|
|||
});
|
||||
this.attributes.mass = mass;
|
||||
this.attributes.saturation = saturation;
|
||||
let that = this;
|
||||
this.hook.push(function () {
|
||||
gravityTable[mainTiles.tiles.findIndex((x, i) => x.id == that.id)] = that.attributes.mass;
|
||||
})
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,15 +16,15 @@ function temperature(event, conduct, transfer = 0) {
|
|||
|
||||
for (let x = -1; x < 2; x ++) {
|
||||
for (let y = -1; y < 2; y++) {
|
||||
let blok = chunks.getBlock(cx + x, cy + y);
|
||||
let temp = chunks.getBlock(cx, cy, true);
|
||||
let temp2 = chunks.getBlock(cx + x, cy + y, true);
|
||||
|
||||
if (temp2 == undefined || temp == undefined || (x == 0 && y == 0)) {
|
||||
if (temp != undefined) chunks.setBlock(cx, cy, Math.max(temp,-296.15), true);
|
||||
if (temp2 == undefined || temp == undefined || (x == 0 && y == 0) || Math.abs(temp2 - temp) < 0.03) {
|
||||
continue;
|
||||
};
|
||||
|
||||
let blok = chunks.getBlock(cx + x, cy + y);
|
||||
|
||||
let conduct2 = (blok != -1) ? (mainTiles.tiles[blok].attributes.conduct || 0) : 0;
|
||||
|
||||
let conductSum = Math.min(conduct * conduct2 * 10, 0.5);
|
||||
|
|
|
@ -20,14 +20,15 @@ function getContrast(r, g, b) {
|
|||
}
|
||||
|
||||
function Tile(color, id, isAbstract = false, copyFrom = false, copyFromName = false) {
|
||||
this.hook = this.hook || [];
|
||||
if (copyFrom) {
|
||||
let that = this;
|
||||
this.hook = function () {
|
||||
this.hook.push(function () {
|
||||
let oldie = mainTiles.resolve(copyFrom, copyFromName);
|
||||
Object.assign(that, Object.assign({}, oldie, that));
|
||||
that.attributes = Object.assign({}, that.attributes);
|
||||
that.interactions = that.interactions.concat(oldie.interactions)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
this.color = color;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue