better placing
This commit is contained in:
parent
1952708e9f
commit
164c39e079
2 changed files with 24 additions and 25 deletions
31
js/index.js
31
js/index.js
|
@ -74,11 +74,14 @@ function Game(inSize, exp, cs) {
|
||||||
this.dead = false;
|
this.dead = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game.prototype.placeBlock = function (block, get = false) {
|
Game.prototype.placeBlock = function (block, get = false, xI = false, yI = false) {
|
||||||
let { camera, base, fac, exp, inSize } = this;
|
let { camera, base, fac, exp, inSize } = this;
|
||||||
|
|
||||||
let x = Math.round(camera[0] / tileSize);
|
if (xI === false) xI = camera[0];
|
||||||
let y = Math.round(camera[1] / tileSize);
|
if (yI === false) yI = camera[1];
|
||||||
|
|
||||||
|
let x = Math.round(xI / tileSize);
|
||||||
|
let y = Math.round(yI / tileSize);
|
||||||
|
|
||||||
let bx = x % chunkSize;
|
let bx = x % chunkSize;
|
||||||
let by = y % chunkSize;
|
let by = y % chunkSize;
|
||||||
|
@ -90,15 +93,14 @@ Game.prototype.placeBlock = function (block, get = false) {
|
||||||
let g = chunk[by * chunkSize + bx];
|
let g = chunk[by * chunkSize + bx];
|
||||||
if (g != 0) return g - 1;
|
if (g != 0) return g - 1;
|
||||||
|
|
||||||
let isMask = toPoint(camera[0], camera[1], base, fac, exp, inSize);
|
let isMask = toPoint(xI, yI, base, fac, exp, inSize);
|
||||||
|
|
||||||
let assetI = (x - y % 2) == 0 ? 0 : 1;
|
let assetI = ((x & y) % 3) == 0 ? 0 : 1;
|
||||||
|
|
||||||
if (isMask < 0.5) return 5;
|
if (isMask < 0.5) return 5;
|
||||||
if (isMask < 0.501) return 4;
|
if (isMask < 0.501) return 4;
|
||||||
if (isMask < 0.51) return 3;
|
if (isMask < 0.51) return 3;
|
||||||
|
|
||||||
|
|
||||||
return assetI;
|
return assetI;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,22 +167,7 @@ Game.prototype.main = function () {
|
||||||
let bx = Math.round(x / tileSize) % chunkSize;
|
let bx = Math.round(x / tileSize) % chunkSize;
|
||||||
let by = Math.round(y / tileSize) % chunkSize;
|
let by = Math.round(y / tileSize) % chunkSize;
|
||||||
|
|
||||||
if (chunk && chunk[by * chunkSize + bx] != 0) {
|
let assetI = this.placeBlock(0,true,x,y);
|
||||||
ctx.drawImage(this.assets[chunk[by * chunkSize + bx] - 1], x2, y2, tileSize, tileSize);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let isMask = toPoint(x, y, base, this.fac, exp, inSize);
|
|
||||||
if (x < 0 || y < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let assetI = ((Math.round(x / tileSize) - Math.round(y / tileSize)) % 2) == 0 ? 0 : 1;
|
|
||||||
|
|
||||||
if (isMask < 0.51) assetI = 3;
|
|
||||||
if (isMask < 0.501) assetI = 4;
|
|
||||||
|
|
||||||
if (isMask < 0.5) continue;
|
|
||||||
|
|
||||||
ctx.drawImage(this.assets[assetI], x2, y2, tileSize, tileSize);
|
ctx.drawImage(this.assets[assetI], x2, y2, tileSize, tileSize);
|
||||||
}
|
}
|
||||||
|
|
18
js/motion.js
18
js/motion.js
|
@ -1,5 +1,9 @@
|
||||||
let keys = [];
|
let keys = [];
|
||||||
let vel = [0, 0];
|
let vel = [0, 0];
|
||||||
|
let posM = [0,0];
|
||||||
|
let r = document.querySelector('canvas').getBoundingClientRect();
|
||||||
|
let x = r.x + r.width / 2;
|
||||||
|
let y = r.y + r.height / 2;
|
||||||
|
|
||||||
let id = '';
|
let id = '';
|
||||||
|
|
||||||
|
@ -7,8 +11,8 @@ function down(e) {
|
||||||
let k = e.key.toLowerCase();
|
let k = e.key.toLowerCase();
|
||||||
keys[k] = true;
|
keys[k] = true;
|
||||||
|
|
||||||
if (k == 'r') game.placeBlock(6);
|
if (k == 'r') game.placeBlock(6,false,posM[0],posM[1]);
|
||||||
if (k == 'f') game.placeBlock(5);
|
if (k == 'f') game.placeBlock(5,false,posM[0],posM[1]);
|
||||||
};
|
};
|
||||||
|
|
||||||
function up(e) {
|
function up(e) {
|
||||||
|
@ -27,7 +31,15 @@ function move() {
|
||||||
game.camera[1] -= vel[1] * 1.8;
|
game.camera[1] -= vel[1] * 1.8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mouse(e) {
|
||||||
|
posM[0] = game.camera[0] + (e.pageX - x) / r.width * document.querySelector('canvas').width;
|
||||||
|
posM[0] -= tileSize / 2;
|
||||||
|
posM[1] = game.camera[1] + (e.pageY - y) / r.height * document.querySelector('canvas').height;
|
||||||
|
posM[1] -= tileSize / 2;
|
||||||
|
}
|
||||||
|
|
||||||
setInterval(move, 10);
|
setInterval(move, 10);
|
||||||
|
|
||||||
window.addEventListener('keydown', down);
|
window.addEventListener('keydown', down);
|
||||||
window.addEventListener('keyup', up);
|
window.addEventListener('keyup', up);
|
||||||
|
window.addEventListener('mousemove', mouse);
|
Loading…
Reference in a new issue