You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

"use strict";
class Balle extends Anime {
constructor(element, scene) {
super(element);
this.setImage("balle.png", 50, 50);
this.vitesseinitiale = 5;
this.initVitesse(this.vitesseinitiale);
this.scene_ = scene;
this.coller = true;
this.ecouteurSouris = (evt) => (this.coller = false);
}
animer() {
this.timerAnimation = setInterval(() => { this.bouger(); }, 1000 / 60);
window.addEventListener("mousedown", this.ecouteurSouris);
}
figer() {
clearInterval(this.timerAnimation);
window.removeEventListener("mousedown", this.ecouteurSouris);
}
bouger() {
if (this.coller) {
this.nx_ = this.scene_.palet_.getX() + this.scene_.palet_.getWidth() / 2 - this.getWidth() / 2;
this.ny_ = this.scene_.palet_.getY() - this.getHeight();
this.setXY(this.nx_, this.ny_);
}
else {
this.nx_ = this.getX() + this.vx;
this.ny_ = this.getY() + this.vy;
this.rebondirBordure();
this.rebondirPalet();
this.rebondirBriques();
this.setXY(this.nx_, this.ny_);
}
}
vitesse() {
this.vitesseMax = 15;
this.multiplicateur = 1.05;
this.vx *= this.multiplicateur;
this.vy *= this.multiplicateur;
if (this.getVitesse() >= this.vitesseMax) {
this.vx = this.vitesseMax;
this.vy = this.vitesseMax;
}
}
rebondirBordure() {
if (this.nx_ >= this.xmax_) {
this.nx_ = this.xmax_;
this.vx *= -1;
}
else if (this.nx_ <= this.xmin_) {
this.nx_ = this.xmin_;
this.vx *= -1;
}
if (this.ny_ >= this.ymax_) {
this.figer();
this.scene_.palet_.figer();
}
else if (this.ny_ <= this.ymin_) {
this.ny_ = this.ymin_;
this.vy *= -1;
}
}
rebondirPalet() {
if (Balle.collision(this.getCircle(), this.scene_.palet_.getRectangle())) {
this.ny_ = this.scene_.palet_.getY() - this.getHeight();
this.vy *= -1;
this.vitesse();
}
;
}
rebondirBriques() {
for (let i = 0; i < this.scene_.brique_.length; i++) {
if (Balle.collision(this.getCircle(), this.scene_.brique_[i].getRectangle())) {
this.vy *= -1;
this.scene_.removeChild(this.scene_.brique_[i]);
}
}
}
initVitesse(v) {
this.vx = 4 * Math.random() - 2;
this.vy = -2;
let vr = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
this.vx = this.vx / vr * v;
this.vy = this.vy / vr * v;
console.log(this.vitesse());
}
getVitesse() {
return Math.sqrt(this.vx * this.vx + this.vy * this.vy);
}
}