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.

110 lines
3.4 KiB

class Rat extends Sprite {
//Attributs
public scene_ : Jeu;
private ecouteurDeplacer : any;
public px_ : number;
public py_ : number;
public ancienpx_ : number;
public ancienpy_ : number;
//Constructeur
public constructor(scene : Jeu,element : HTMLElement, py : number, px : number){
super(element);
this.scene_ = scene;
this.px_ = px;
this.py_ = py;
this.ecouteurDeplacer = (event : KeyboardEvent) => {this.deplacer(event)};
this.getElement().style.zIndex = "2";
}
async haut(){
while(this.scene_.carte_[this.py_-1][this.px_] != 1){
this.ancienpy_ = this.py_;
this.ancienpx_ = this.px_;
this.py_ = this.py_-1;
this.setXY(this.getX(),this.getY()-this.scene_.pas_);
this.getElement().style.transform = 'rotate(-90deg)';
this.manger();
console.log("oui");
await new Promise<void>(resolve => setTimeout(resolve, 20));
}
}
async bas(){
while(this.scene_.carte_[this.py_+1][this.px_] != 1){
this.ancienpy_ = this.py_;
this.ancienpx_ = this.px_;
this.py_ = this.py_+1;
this.setXY(this.getX(),this.getY()+this.scene_.pas_);
this.getElement().style.transform = 'rotate(90deg)';
this.manger();
await new Promise<void>(resolve => setTimeout(resolve, 20));
}
}
async gauche(){
while(this.scene_.carte_[this.py_][this.px_-1] != 1){
this.ancienpy_ = this.py_;
this.ancienpx_ = this.px_;
this.px_ = this.px_-1;
this.setXY(this.getX()-this.scene_.pas_,this.getY());
this.getElement().style.transform = 'rotate(90deg) scale(-1,1)';
this.manger();
await new Promise<void>(resolve => setTimeout(resolve, 20));
}
}
async droite(){
while(this.scene_.carte_[this.py_][this.px_+1] != 1){
this.ancienpy_ = this.py_;
this.ancienpx_ = this.px_;
this.px_ = this.px_+1;
this.setXY(this.getX()+this.scene_.pas_,this.getY());
this.getElement().style.transform = 'rotate(90deg) scale(1,1)';
this.manger();
await new Promise<void>(resolve => setTimeout(resolve, 20));
}
}
private deplacer(event : KeyboardEvent){
if(this.estArriver() == false){
if(event.key == "ArrowLeft"){
this.gauche();
}
if(event.key == "ArrowRight"){
this.droite();
}
if(event.key == "ArrowDown"){
this.bas();
}
if(event.key == "ArrowUp"){
this.haut();
}
}
}
public manger(){
if(this.scene_.carte_[this.py_][this.px_] == 4 || this.scene_.carte_[this.py_][this.px_] == 9 ){
this.scene_.replacerPersonnage(this.py_,this.px_,this.ancienpy_,this.ancienpx_);
console.log("j'ai bouger");
}else if(this.scene_.carte_[this.py_][this.px_] == 2){
this.scene_.retirerFromage(this.py_,this.px_);
console.log("j'ai mange");
this.scene_.carte_[this.py_][this.px_] = 8;
}else if(this.scene_.carte_[this.py_][this.px_] == 3){
this.scene_.retirerFraise(this.py_,this.px_);
console.log("j'ai mange");
this.scene_.carte_[this.py_][this.px_] = 8;
}
this.scene_.dessinerLabyrinthe();
}
public animer(){
window.addEventListener("keydown", this.ecouteurDeplacer);
}
public figer(){
window.removeEventListener("keydown", this.ecouteurDeplacer);
}
public estArriver(){
return this.scene_.carte_[this.scene_.arrivery_][this.scene_.arriverx_] == 8;
}
}