import { _decorator, tween, Vec3 } from 'cc';
import { GameMgr } from './GameFrameWork/GameMgr';
import { ModulerBase } from './GameFrameWork/ModulerBase';
import { MapMgr } from './GameFrameWork/MapMgr';
const { ccclass, property } = _decorator;

@ccclass('Monster')
export class Monster extends ModulerBase {
    //private _gameMgr: GameMgr = null;
    private _speed: number = 0;
    private _points: Vec3[] = null;
    protected onLoad(): void {
        //this._gameMgr = this.node.parent.parent.getComponent(GameMgr);
        //this._points = this._gameMgr.getPath();
        this._points = GameMgr.Instance.getModuler(MapMgr).getPath();
    }
    start() {
        const point = this._points[0];
        this.node.setPosition(point.x, point.y);
        this._speed = 100;
        this.move(this._points, 1)

    }

    move(points: Vec3[],index: number){
        if(index >= points.length - 1){
            return;
        }
        const endPos = points[index];
        const curPos = points[index - 1];
        //计算向量差
        const dis: number = endPos.clone().subtract(curPos).length();
        const t: number = dis / this._speed;
        tween(this.node)
            .to(t,{position:points[index]})
            .call(()=>{
                this.move(points, index + 1)
            })
            .start();
    }

    private _destroyMonster(points: Vec3[]) {
        if(this.node.getPosition().x === points[points.length - 2].x
        && this.node.getPosition().y === points[points.length - 2].y){
            this.schedule(()=>{
                this.node.destroy();
            },1)
        }
    }

    update(deltaTime: number) {
        this._destroyMonster(this._points)
    }
}


