import { getRegion, setRegion } from "./display";
import { Figure } from "./figure";
import { PLAYER_SPRITE } from "./images";
import type { IRegion } from "./types";

export class Sprite extends Figure {
    private prevX: number;
    private prevY: number;
    private prevImage: IRegion;

    public skipNextBackgroundRestore = false;

    constructor(
        x: number,
        y: number,
    ) {
        super(x, y, PLAYER_SPRITE);

        this.prevX = x | 0;
        this.prevY = y | 0;
        this.prevImage = getRegion(x, y, this.width, this.height);
    }

    override doDraw() {
        if (this.skipNextBackgroundRestore) {
            this.skipNextBackgroundRestore = false;
        } else {
            setRegion(this.prevX, this.prevY, this.width, this.height, this.prevImage);
        }
        this.prevImage = getRegion(this.x, this.y, this.width, this.height);

        super.doDraw();

        this.prevX = this.x;
        this.prevY = this.y;
    }
}