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.

73 lines
2.5 KiB

"use strict";
class Joueur extends Sprite {
constructor(element, scene) {
super(element);
this.scene_ = scene;
this.timerAnimation = null;
this.vitesse_ = 6;
this.vitesseMax_ = 15;
this.touchesEnfoncees = {};
this.ecouteurClavier_ = (evt) => this.deplacer(evt);
this.ecouteurClavierStop_ = (evt) => this.stopperDeplacement(evt);
}
animer() {
window.addEventListener("keydown", this.ecouteurClavier_);
window.addEventListener("keyup", this.ecouteurClavierStop_);
}
deplacementGauche() {
this.setX(this.getX() - this.vitesse_);
}
deplacementDroite() {
this.setX(this.getX() + this.vitesse_);
}
deplacementHaut() {
this.setY(this.getY() - this.vitesse_);
}
deplacementBas() {
this.setY(this.getY() + this.vitesse_);
}
mettreAJourDeplacement() {
if (this.touchesEnfoncees["ArrowLeft"] && this.touchesEnfoncees["ArrowUp"]) {
this.deplacementGauche();
this.deplacementHaut();
}
else if (this.touchesEnfoncees["ArrowLeft"] && this.touchesEnfoncees["ArrowDown"]) {
this.deplacementGauche();
this.deplacementBas();
}
else if (this.touchesEnfoncees["ArrowRight"] && this.touchesEnfoncees["ArrowUp"]) {
this.deplacementDroite();
this.deplacementHaut();
}
else if (this.touchesEnfoncees["ArrowRight"] && this.touchesEnfoncees["ArrowDown"]) {
this.deplacementDroite();
this.deplacementBas();
}
else if (this.touchesEnfoncees["ArrowLeft"]) {
this.deplacementGauche();
}
else if (this.touchesEnfoncees["ArrowRight"]) {
this.deplacementDroite();
}
else if (this.touchesEnfoncees["ArrowUp"]) {
this.deplacementHaut();
}
else if (this.touchesEnfoncees["ArrowDown"]) {
this.deplacementBas();
}
}
deplacer(evt) {
this.touchesEnfoncees[evt.key] = true;
if (this.timerAnimation === null) {
this.timerAnimation = setInterval(() => this.mettreAJourDeplacement(), 1000 / 120);
}
}
stopperDeplacement(evt) {
delete this.touchesEnfoncees[evt.key];
if (Object.keys(this.touchesEnfoncees).length === 0 && this.timerAnimation !== null) {
clearInterval(this.timerAnimation);
this.timerAnimation = null;
}
}
}