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.

46 lines
1.8 KiB

"use strict";
class Objet extends Sprite {
constructor(element, scene) {
super(element);
this.scene_ = scene;
this.listeObjets_ = [];
this.widthX = document.documentElement.clientWidth;
this.heightY = document.documentElement.clientHeight;
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_);
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_);
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_);
}
apparition() {
const randomIndex = Math.floor(Math.random() * this.listeObjets_.length);
const objet = this.listeObjets_[randomIndex];
const clone = objet.getElement().cloneNode(true);
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);
}
randomX(width) {
return Math.floor(Math.random() * width);
}
randomY(height) {
return Math.floor(Math.random() * height);
}
positionnement() {
this.listeObjets_.forEach((objet) => {
objet.setXY(this.randomX(this.widthX), this.randomY(this.heightY));
});
}
}