unibutton/common/entity.js

29 lines
755 B
JavaScript
Raw Normal View History

2024-10-18 19:30:00 -04:00
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;
2024-10-18 19:43:40 -04:00
let bounced = false;
2024-10-18 19:30:00 -04:00
if (Math.abs(ent.pos.x) >= width / 2) {
ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x);
2024-10-18 19:43:40 -04:00
bounced = true;
2024-10-18 19:30:00 -04:00
}
if (Math.abs(ent.pos.y) >= height / 2) {
ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y);
2024-10-18 19:43:40 -04:00
bounced = true;
2024-10-18 19:30:00 -04:00
}
2024-10-18 19:43:40 -04:00
return bounced;
2024-10-18 19:30:00 -04:00
}
}
export default Entity;