49 lines
987 B
JavaScript
49 lines
987 B
JavaScript
|
const MAX_SPEED = 20;
|
||
|
const MAX_JUMP = 2;
|
||
|
const G = 500;
|
||
|
|
||
|
class Box {
|
||
|
constructor() {
|
||
|
this.position = 0;
|
||
|
this.speed = 0;
|
||
|
this.size = 100;
|
||
|
this.jumpCounter = MAX_JUMP;
|
||
|
}
|
||
|
|
||
|
drawOn(canvas) {
|
||
|
let ctx = canvas.getContext('2d');
|
||
|
|
||
|
ctx.fillStyle = 'rgb(0, 0, 0)';
|
||
|
ctx.beginPath();
|
||
|
ctx.rect(
|
||
|
canvas.width / 2.0 - this.size / 2.0,
|
||
|
canvas.height - this.size - this.position,
|
||
|
this.size,
|
||
|
this.size
|
||
|
);
|
||
|
|
||
|
ctx.fill();
|
||
|
|
||
|
}
|
||
|
|
||
|
update(time = 0.002) {
|
||
|
this.speed -= G * time;
|
||
|
this.speed = Math.max(this.speed, -MAX_SPEED);
|
||
|
this.position += this.speed;
|
||
|
|
||
|
if (this.position <= 0) {
|
||
|
this.position = 0;
|
||
|
this.speed = 0;
|
||
|
this.jumpCounter = MAX_JUMP;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
jump() {
|
||
|
if (this.jumpCounter > 0) {
|
||
|
this.jumpCounter--;
|
||
|
this.speed = MAX_SPEED;
|
||
|
}
|
||
|
}
|
||
|
}
|