unibutton/common/entity.js
2024-10-18 19:43:40 -04:00

29 lines
No EOL
755 B
JavaScript

class Entity {
constructor(game) {
let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y: (Math.random() - 0.5) * (game ? game.height : 1000) };
let vel = { x: 0, y: 0 };
this.pos = pos;
this.vel = vel;
}
bounce(game) {
let ent = this;
let { width, height } = game;
let bounced = false;
if (Math.abs(ent.pos.x) >= width / 2) {
ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x);
bounced = true;
}
if (Math.abs(ent.pos.y) >= height / 2) {
ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y);
bounced = true;
}
return bounced;
}
}
export default Entity;