import { TILE_SIZE } from "../constants.js"; import { GameObject } from "./game-object.js"; export class Player extends GameObject { constructor({ gameObjects = [], animations = {}, defaultAnimation = { idle: "bottom" }, x = 0, y = 0, speed = 1, width = TILE_SIZE, height = TILE_SIZE, }) { super({ x, y, gameObjects, width, height }); this.speed = speed; this.keys = [ { key: "ArrowUp", pressed: false, value: [0, -1] }, { key: "ArrowDown", pressed: false, value: [0, 1] }, { key: "ArrowLeft", pressed: false, value: [-1, 0] }, { key: "ArrowRight", pressed: false, value: [1, 0] }, ]; this.availableKeys = this.keys.reduce( (acc, item) => ({ ...acc, [item.key]: item }), {} ); this.eventEmitter.on("levelChanged", (...args) => { this.x = x; this.y = y; }); // TODO: Decouple animation into animation class this.animations = animations; this.defaultAnimation = defaultAnimation; this.currentAnimation = defaultAnimation; this.currentAnimationFrame = 0; } update(delta) { let idle = true; this.keys.forEach((item) => { if (item.pressed) { this.moveCharacter(delta, ...item.value); this.updateAnimation(delta, "walk", ...item.value); idle = false; } }); if (idle) { this.updateAnimation(delta, "idle"); } } onKeyPressed(key) { if (!this.availableKeys[key]) return; this.availableKeys[key].pressed = true; } onKeyReleased(key) { if (!this.availableKeys[key]) return; this.availableKeys[key].pressed = false; } render(ctx, ...args) { // TODO: Decouple animation into animation class const [x, y] = args; const [spriteSheet] = this.gameObjects; if (this.debug) { spriteSheet.sprites.forEach((item, index) => { item.render(ctx, this.x - x + index * TILE_SIZE, this.y - y); }); } else { const [currentAnimationKey] = Object.keys(this.currentAnimation); const currentAnimationDirection = this.currentAnimation[currentAnimationKey]; const frames = this.animations[currentAnimationKey][currentAnimationDirection]; if (!frames) { throw Error("No animation defined for " + this.currentAnimation); } const item = frames.map((frame) => spriteSheet.sprites[frame])[ Math.floor(this.currentAnimationFrame) % frames.length ]; item.render(ctx, this.x - x, this.y - y); } } moveCharacter(delta, dx, dy) { const speed = Math.floor(delta * this.speed * 100); this.x = this.x + dx * speed; this.y = this.y + dy * speed; // TODO: Check for collisions and stop movement if needed } // TODO: Decouple animation into animation class updateAnimation(delta, animation, dx, dy) { if (animation === "idle") { const [value] = Object.values(this.currentAnimation); this.currentAnimation = { [animation]: value }; return; } if (dx !== 0) { this.currentAnimation = { [animation]: dx > 0 ? "right" : "left" }; } else if (dy !== 0) { this.currentAnimation = { [animation]: dy > 0 ? "bottom" : "top" }; } this.currentAnimationFrame += 10 * delta; } }