unibutton/common/entity.js

23 lines
No EOL
644 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;
if (Math.abs(ent.pos.x) >= width / 2) {
ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x);
}
if (Math.abs(ent.pos.y) >= height / 2) {
ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y);
}
}
}
export default Entity;