escalator-web/src/box.js

52 lines
992 B
JavaScript

const MAX_SPEED = 20;
const MAX_JUMP = 2;
const G = 500;
class Box {
constructor() {
this.size = 0.1;
this.reset();
}
reset() {
// All these values are in percentage of the screen
this.x = 0.5;
this.y = 0;
this.speedX = 0;
this.speedY = 0;
this.jumpCounter = MAX_JUMP;
}
update(time = 0.002) {
this.speedX *= 0.95;
this.speedY -= G * time;
this.speedY = Math.max(this.speedY, -MAX_SPEED);
this.x += this.speedX * time;
this.y += this.speedY * time;
}
collide(y) {
this.y = y;
this.speedX = 0;
this.speedY = 0;
this.jumpCounter = MAX_JUMP;
}
jump(_x, _y) {
// Put x between -1 and 1
let x = (_x - this.x) * 2;
let y = _y;
if (this.jumpCounter > 0) {
this.jumpCounter--;
this.speedX = MAX_SPEED * x;
this.speedY = MAX_SPEED;
}
}
}