block place / break test (WIP)

This commit is contained in:
biglyderv 2024-09-13 21:25:41 -04:00
parent 1afdddc882
commit 95adfdabdf
No known key found for this signature in database
GPG key ID: 33AC87E9ACE66954
4 changed files with 227 additions and 14 deletions

View file

@ -1,12 +1,15 @@
//const width = 2048;
//const height = 2048;
const tileSize = 128;
// todo: make names better
const assets = [
'assets/ground1.svg',
'assets/ground2.svg',
'assets/player.svg',
'assets/ground3.svg',
'assets/ground4.svg',
'assets/ground5.svg',
'assets/ground6.svg'
];
function getRGBA(data, i) {
@ -46,6 +49,15 @@ function toPoint(x2, y2, base, fac, exp, inSize) {
return isMask;
}
function getChunk(x, y) {
let cx = Math.floor(x / 16);
let cy = Math.floor(y / 16);
let id = cx + ',' + cy;
return id;
}
function Game(inSize, exp, cs) {
this.exp = exp;
this.assets = [];
@ -57,12 +69,30 @@ function Game(inSize, exp, cs) {
this.frames = 0;
this.camera = [0, 0];
this.modChunks = {};
this.dead = false;
}
Game.prototype.placeBlock = function (block) {
let {camera} = this;
let x = Math.round(camera[0] / tileSize);
let y = Math.round(camera[1] / tileSize);
let bx = x % 16;
let by = y % 16;
let id = getChunk(x, y);
let chunk = this.modChunks[id] = this.modChunks[id] || new Uint8Array(16 * 16);
chunk[by * 16 + bx] = block + 1;
}
Game.prototype.player = function () {
let { ctx, fac, base, exp, inSize, camera, cs, assets } = this;
ctx.drawImage(assets[2],cs / 2 - tileSize / 1.8, cs / 2 - tileSize / 1.8, tileSize / 0.9, tileSize / 0.9);
ctx.drawImage(assets[2], cs / 2 - tileSize / 1.8, cs / 2 - tileSize / 1.8, tileSize / 0.9, tileSize / 0.9);
if (toPoint(camera[0], camera[1], base, fac, exp, inSize) < 0.5) {
this.dead = true;
@ -77,7 +107,7 @@ Game.prototype.player = function () {
sum += base[i]
}
sum *= (fac ** (1/exp));
sum *= (fac ** (1 / exp));
let dim = Math.log(sum) / (Math.log(base.length) / 2);
@ -92,22 +122,34 @@ Game.prototype.main = function () {
} else {
}
ctx.clearRect(0,0,cs,cs);
ctx.clearRect(0, 0, cs, cs);
this.frames++;
this.fac = 0.99999 ** frames;
for (let i = 0; i < (cs/tileSize) * (cs/tileSize); i++) {
let x = i % (cs/tileSize)
for (let i = 0; i < (cs / tileSize) * (cs / tileSize); i++) {
let x = i % (cs / tileSize)
x *= tileSize;
let y = Math.floor(i / (cs/tileSize));
let y = Math.floor(i / (cs / tileSize));
y *= tileSize;
x = x + Math.round((camera[0] - cs / 2) / tileSize)*tileSize;
y = y + Math.round((camera[1] - cs / 2) / tileSize)*tileSize;
x = x + Math.round((camera[0] - cs / 2) / tileSize) * tileSize;
y = y + Math.round((camera[1] - cs / 2) / tileSize) * tileSize;
let x2 = x - camera[0] + cs/2;
let y2 = y - camera[1] + cs/2;
let x2 = x - camera[0] + cs / 2;
let y2 = y - camera[1] + cs / 2;
let id = getChunk(x / tileSize, y / tileSize);
let chunk = this.modChunks[id];
let bx = Math.round(x / tileSize) % 16;
let by = Math.round(y / tileSize)% 16;
if (chunk && chunk[by * 16 + bx] != 0) {
ctx.drawImage(this.assets[chunk[by * 16 + bx] - 1], x2, y2, tileSize, tileSize);
continue;
}
let isMask = toPoint(x, y, base, this.fac, exp, inSize);
if (x < 0 || y < 0) {
@ -121,7 +163,7 @@ Game.prototype.main = function () {
if (isMask < 0.5) continue;
ctx.drawImage(this.assets[assetI],x2,y2,tileSize,tileSize);
ctx.drawImage(this.assets[assetI], x2, y2, tileSize, tileSize);
}