import { Component, Entity } from "../core/world";
import { component } from "../utils/decorators";

@component({ variables: ['x', 'y', 'z'] })
export class Position extends Component<{ x: number, y: number, z: number, absolute: boolean }> {
    constructor(x = 0, y = 0, z = 0, absolute = false) {
        super({ x, y, z, absolute });
    }

    get x() {
        return this.state.x;
    }
    get y() {
        return this.state.y;
    }
    get z() {
        return this.state.z;
    }
    set x(x: number) {
        this.state.x = x;
    }
    set y(y: number) {
        this.state.y = y;
    }
    set z(z: number) {
        this.state.z = z;
    }
    get absolute() {
        return this.state.absolute;
    }
    set absolute(absolute: boolean) {
        this.state.absolute = absolute;
    }
}

export const getPosition = (entity?: Entity, key?: string) => entity?.get(Position, key)?.state;
export const move = (entity: Entity, dx: number, dy: number, dz = 0) => {
    const pos = getPosition(entity);
    if (!pos) return;
    pos.x += dx;
    pos.y += dy;
    pos.z += dz;
};