import { component } from "@common/rpg/utils/decorators";
import { Component, Entity, World } from "@common/rpg/core/world";
import { Position } from "@common/rpg/components/position";
import { Sprite } from "@common/rpg/components/sprite";
import { PLAYER_SPRITE } from "./images";

@component
export class Player extends Component<{}> {
    constructor() {
        super({});
    }

    get x() { 
        const pos = this.entity.get(Position);
        return pos ? pos.state.x : 0;
    }

    get y() { 
        const pos = this.entity.get(Position);
        return pos ? pos.state.y : 0;
    }

    set x(x: number) {
        const pos = this.entity.get(Position);
        if (pos) pos.state.x = x;
    }

    set y(y: number) {
        const pos = this.entity.get(Position);
        if (pos) pos.state.y = y;
    }
}

export const createPlayer = (world: World, x: number, y: number): Entity => {
    const player = world.createEntity('player');

    player.add(new Position(x, y, 999));
    player.add(new Position(), 'world');
    player.add(new Sprite(PLAYER_SPRITE));
    player.add(new Player());

    return player;
}