parent
f8ffc1825e
commit
d3a295dcea
@ -0,0 +1,45 @@
|
||||
"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));
|
||||
});
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 240 KiB |
After Width: | Height: | Size: 183 KiB |
After Width: | Height: | Size: 231 KiB |
@ -0,0 +1,80 @@
|
||||
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));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue