diff --git a/assets/ground5.svg b/assets/ground5.svg new file mode 100644 index 0000000..143bf22 --- /dev/null +++ b/assets/ground5.svg @@ -0,0 +1,39 @@ + + + + diff --git a/assets/ground6.svg b/assets/ground6.svg new file mode 100644 index 0000000..5c5559b --- /dev/null +++ b/assets/ground6.svg @@ -0,0 +1,128 @@ + + + + diff --git a/js/index.js b/js/index.js index 70d056c..7da2d4e 100644 --- a/js/index.js +++ b/js/index.js @@ -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); } diff --git a/js/motion.js b/js/motion.js index 4b4fd14..0209c93 100644 --- a/js/motion.js +++ b/js/motion.js @@ -4,7 +4,11 @@ let vel = [0, 0]; let id = ''; function down(e) { - keys[e.key.toLowerCase()] = true; + let k = e.key.toLowerCase(); + keys[k] = true; + + if (k == 'r') game.placeBlock(6); + if (k == 'f') game.placeBlock(5); }; function up(e) { @@ -13,8 +17,8 @@ function up(e) { function move() { var isShift = keys['q']; - vel[0] += ((keys['a'] ? 1 : 0) - (keys['d'] ? 1 : 0)) * (isShift ? 5 : 1); - vel[1] += ((keys['w'] ? 1 : 0) - (keys['s'] ? 1 : 0)) * (isShift ? 5 : 1); + vel[0] += ((keys['a'] ? 1 : 0) - (keys['d'] ? 1 : 0)) * (isShift ? 2.3 : 1); + vel[1] += ((keys['w'] ? 1 : 0) - (keys['s'] ? 1 : 0)) * (isShift ? 2.3 : 1); vel[0] *= 0.8; vel[1] *= 0.8;