xxxxxxxxxx
57
function Player(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.update = function() {
// Gravity
this.vel.y += 0.1;
this.controls();
this.pos.add(this.vel);
this.handleCollisions();
};
this.rectOverlap = function(l1, r1, l2, r2) {
if (l1.x > r2.x || l2.x > r1.x)
return false;
if (l1.y < r2.y || l2.y < r1.y)
return false;
return true;
}
this.handleCollisions = function() {
// for (let c = 0; c < chunks.length; c++) {
// for (let i = 0; i < 16; i++) {
// for (let j = 0; j < 32; j++) {
// const x = i * gs + c * 16 * gs;
// const y = j * gs;
// const l1 = createVector(x, y);
// const r1 = createVector(x + gs, y + gs);
// const l2 = this.pos.copy();
// const r2 = createVector(l2.x + gs, l2.y + 2 * gs);
// if (chunks[c][i][j].id == 0 && this.rectOverlap(l1, r1, l2, r2)) {
// this.pos.y = y-1;
// }
// }
// }
// }
if (this.pos.y >= 14 * gs) {
this.pos.y = 14 * gs;
}
};
this.controls = function() {
if (keyIsPressed) {
if (key == "d" || key == "D") {
this.vel.x = 5;
}
}
};
this.show = function() {
push();
fill(100);
rect(this.pos.x, this.pos.y, gs, gs * 2);
pop();
};
}