xxxxxxxxxx
81
class Player {
constructor(x, y, w) {
this.x = x;
this.y = y;
this.w = w;
this.direction = 0;
this.layout = 1;
}
//draw the player
drawing() {
push();
fill("rgb(21,86,21)");
stroke("black");
rectMode(CENTER);
if (this.layout) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 2; j++) {
rect(this.x + this.w * i, this.y + this.w * j, this.w, this.w);
}
}
} else {
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 3; j++) {
rect(this.x + this.w * i, this.y + this.w * j, this.w, this.w);
}
}
}
if (this.direction == 0) {
rect(this.x + this.w, this.y - this.w, this.w, this.w);
} else if (this.direction == 1) {
rect(this.x + 2 * this.w, this.y + this.w, this.w, this.w);
} else if (this.direction == 2) {
rect(this.x + this.w, this.y + 2 * this.w, this.w, this.w);
} else if (this.direction == 3) {
rect(this.x - this.w, this.y + this.w, this.w, this.w);
}
pop();
}
//update the position of the player
move() {
if (keyIsPressed) {
if (keyCode == UP_ARROW && this.y > 0) {
this.y -= 1;
this.layout = 1;
this.direction = 0;
}
if (keyCode == DOWN_ARROW && this.y < height) {
this.y += 1;
this.layout = 1;
this.direction = 2;
}
if (keyCode == LEFT_ARROW && this.x > 0) {
this.x -= 1;
this.layout = 0;
this.direction = 3;
}
if (keyCode == RIGHT_ARROW && this.x < width) {
this.x += 1;
this.layout = 0;
this.direction = 1;
}
}
}
//check if the player is hit
hit(obj) {
if (this.layout) {
if (dist(obj.x, obj.y, this.x + this.w, this.y) < 2 * this.w) {
return true;
}
} else {
if (dist(obj.x, obj.y, this.x, this.y + this.w) < 2 * this.w) {
return true;
}
}
}
}