This repository has been archived on 2025-04-27. You can view files and clone it, but cannot push or open issues or pull requests.
Servers-Under-Maintenance-G.../classes/Player.js

137 lines
3.0 KiB
JavaScript

class Player extends GameObject {
constructor(x, y) {
super(x, y);
this.walking = false;
this.facing = "d";
this.clickable = true;
this.acceptsItem = true;
this.hitbox = new Hitbox(x + 4, y + 10, Game.TILESIZE - 8, Game.TILESIZE - 10);
this.walkCycle = 0;
this.direction_keys = [false, false, false, false];
this.inventory = [];
this.inventorySlots = 1;
this.maxEnergy = Game.PLAYER_MAX_ENERGY;
this.energy = Game.PLAYER_MAX_ENERGY;
}
get speed() {
return this.energy > 0 ? Game.PLAYER_SPEED : Game.PLAYER_OOE_SPEED;
}
keydown(e) {
if (e.code == "KeyW" || e.code == "ArrowUp") {
this.facing = "u";
this.walking = true;
this.direction_keys[0] = true;
}
if (e.code == "KeyS" || e.code == "ArrowDown") {
this.facing = "d";
this.walking = true;
this.direction_keys[1] = true;
}
if (e.code == "KeyA" || e.code == "ArrowLeft") {
this.facing = "l";
this.walking = true;
this.direction_keys[2] = true;
}
if (e.code == "KeyD" || e.code == "ArrowRight") {
this.facing = "r";
this.walking = true;
this.direction_keys[3] = true;
}
}
keyup(e) {
if (e.code == "KeyW" || e.code == "ArrowUp") {
this.direction_keys[0] = false;
}
if (e.code == "KeyS" || e.code == "ArrowDown") {
this.direction_keys[1] = false;
}
if (e.code == "KeyA" || e.code == "ArrowLeft") {
this.direction_keys[2] = false;
}
if (e.code == "KeyD" || e.code == "ArrowRight") {
this.direction_keys[3] = false;
}
this.walking = false;
for (let i = 0; i < this.direction_keys.length; i++) {
if (this.direction_keys[i] == true) {
this.walking = true;
this.facing = "udlr"[i];
break;
}
}
}
move(x, y) {
this.x += x;
this.y += y;
this.hitbox.x += x;
this.hitbox.y += y;
}
update() {
if (this.walking) {
if (this.facing == "u") {
this.move(0, - this.speed);
}
if (this.facing == "d") {
this.move(0, this.speed);
}
if (this.facing == "l") {
this.move(- this.speed, 0);
}
if (this.facing == "r") {
this.move(this.speed, 0);
}
if (this.energy > 0 && (Game.gamestate != "tutorial" || Game.tutorialStep >= 12)) {
this.energy--;
}
}
while (this.hitbox.collidesAny()) {
if (this.facing == "u") {
this.move(0, 1);
}
if (this.facing == "d") {
this.move(0, -1);
}
if (this.facing == "l") {
this.move(1, 0);
}
if (this.facing == "r") {
this.move(-1, 0);
}
}
}
draw() {
let sprite;
if (this.walking) {
sprite = Assets.player.walking[this.facing][this.walkCycle];
this.walkCycle = (this.walkCycle + 1) % 3;
Assets.sounds.player.step.play();
} else {
sprite = Assets.player.idle[this.facing];
this.walkCycle = 0;
}
c.drawImage(sprite, this.x, this.y, Game.TILESIZE, Game.TILESIZE);
}
passItem(item) {
if (item == "coffee") {
this.energy = this.maxEnergy;
Assets.sounds.player.drink.play();
if (Game.gamestate == "tutorial" && Game.tutorialStep == 9) {
tutorialStep();
}
return true;
}
return false;
}
}