import { Rectangle } from "../../../pixi/pixi.mjs";
import { SceneObject } from "../../SceneObjects/SceneObject";
import { Point2D, interpolate } from "../../Utils/Math.utils";
import { getSpriteFromAtlas } from "../../Utils/Sprites.utils";
import { getNavigationGridTile } from "../../World/NavigationGrid/NavigationGrid";
import { NPCController } from "../NPCController/NPCController";

export class NPCProto extends SceneObject
{
    /**
     * NPCController ref
     * @type NPCController
     */
    controller = null;

    _positionInterpolationSpeed = 30;
    _basicPositionInterpolationSpeed = 30;

    /**
     * path to NPC spritesheet
     */
    spriteSheetPath = "";

    /**
     * frame to cut from spritesheet
     */
    frame = new Rectangle();

    isMoving = false;

    /**
     * creates new NPC object
     * @param {Boolean} tickAble 
     * @param {NPCController} controller 
     */
    constructor(tickAble = true, controller)
    {
        super(tickAble);
        this.controller = controller;
    };

    onInit()
    {
        super.onInit();
        this.drawObject = getSpriteFromAtlas(this.spriteSheetPath, this.frame);
    };

    tick(ticker)
    {
        if(this.isMoving)
        {
            let gridTile = getNavigationGridTile(this.worldPosition.getX(), this.worldPosition.getY());
            if(gridTile && !gridTile.isObstacle)
            {
                this._positionInterpolationSpeed = this._basicPositionInterpolationSpeed / gridTile.movementCost;
            }
            this._positionInterpolation(ticker.deltaMS / 1000 * this._positionInterpolationSpeed);
        }
    };

    _positionInterpolation(delta)
    {
        let {x, y} = this.drawObject.position;
        this.drawObject.position.set(interpolate(x, this.worldPosition.getX(), delta), interpolate(y, this.worldPosition.getY(), delta));
    };

};