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.
80 lines
2.4 KiB
80 lines
2.4 KiB
class Objet extends Sprite {
|
|
public scene_: Jeu;
|
|
public listeObjets_ : Sprite[];
|
|
|
|
public fruit_ : Sprite;
|
|
public legume_ : Sprite;
|
|
public plante_ : Sprite;
|
|
|
|
|
|
|
|
public widthX : number;
|
|
public heightY : number;
|
|
|
|
public constructor(element: HTMLElement, scene: Jeu) {
|
|
super(element);
|
|
this.scene_ = scene;
|
|
this.listeObjets_ = [];
|
|
|
|
this.widthX = document.documentElement.clientWidth;
|
|
this.heightY = document.documentElement.clientHeight;
|
|
|
|
//Création des fruits
|
|
this.fruit_ = new Sprite (document.createElement("img"));
|
|
this.fruit_.setImage("img/objet/fruit.png",100,100);
|
|
this.fruit_.disparitionDelay = 1500;
|
|
this.listeObjets_.push(this.fruit_);
|
|
|
|
|
|
//Création des légumes
|
|
this.legume_ = new Sprite (document.createElement("img"));
|
|
this.legume_.setImage("img/objet/legume.png",100,100);
|
|
this.legume_.disparitionDelay = 2500;
|
|
this.listeObjets_.push(this.legume_);
|
|
|
|
|
|
|
|
//Création des plantes
|
|
this.plante_ = new Sprite (document.createElement("img"));
|
|
this.plante_.setImage("img/objet/plante.png",100,100);
|
|
this.plante_.disparitionDelay = 3000;
|
|
this.listeObjets_.push(this.plante_);
|
|
|
|
|
|
}
|
|
//Ici ChatGpt m'a été utile pour modifier ma fonction apparition. En effet je ne trouvais pas le moyen de faire apparaître plusieurs mêmes éléments du tableau. Dans mon ancienne fonction, il ne pouvait pas y avoir plusieurs plantes ou plusieurs fruits, sinon il ne voulait pas rester. La fonction choisi aléatoirement un objet du tableau à chaque appel, puis le positionne et la fait disparaitre.
|
|
|
|
public apparition() {
|
|
let randomIndex = Math.floor(Math.random() * this.listeObjets_.length);
|
|
let objet = this.listeObjets_[randomIndex];
|
|
let clone = objet.getElement().cloneNode(true) as HTMLElement;
|
|
|
|
clone.style.position = "absolute";
|
|
clone.style.left = this.randomX(this.widthX) + "px";
|
|
clone.style.top = this.randomY(this.heightY) + "px";
|
|
|
|
this.scene_.appendChild(clone);
|
|
|
|
setTimeout(() => {
|
|
this.scene_.removeChild(clone);
|
|
}, objet.disparitionDelay || 2000);
|
|
}
|
|
|
|
|
|
|
|
private randomX(width : number) {
|
|
return Math.floor(Math.random() * width);
|
|
}
|
|
|
|
private randomY(height : number) {
|
|
return Math.floor(Math.random() * height);
|
|
}
|
|
|
|
//Positionnement aléatoires des objet
|
|
public positionnement() {
|
|
|
|
this.listeObjets_.forEach((objet) => {
|
|
objet.setXY(this.randomX(this.widthX), this.randomY(this.heightY));
|
|
});
|
|
}
|
|
} |