import FF from "./FF";

/**
 * 道具飞行到主角身上
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FObjectFly extends cc.Component {

    public ff:FF

    public move:boolean = false;

    update (dt) {
        if(this.move && this.ff.mainSprite && this.ff.mainSprite.node){
            let node = this.ff.mainSprite.node;
            let pos = node.getPosition();
            pos.y = pos.y + node.height/2;

            if(this.Genzong(pos,dt)){
                this.node.destroy();
            }

        }

    }
    //跟踪导弹计算移动以及转向角度
    private Genzong(targetPosition,dt){
        let speed = 700;
        var targetPoint = targetPosition;
        var point = cc.v2(this.node.x, this.node.y);
        var delta = targetPoint.sub(point);
        var distance = point.sub(targetPoint).mag();
        // var x1 = point.x;
        // var y1 = point.y;
        // var deltaRotation = 90 -Math.atan2(y2 - y1, x2 - x1) * 180 /Math.PI;
        // this.node.rotation=deltaRotation;
        if(distance <= 10){
            return true;
        }
        var x2 = point.x + dt*speed * delta.x / distance;
        var y2 = point.y + dt*speed * delta.y / distance;
        var newPosition = cc.v2(x2, y2);
        this.node.setPosition(newPosition);//设置跟踪导弹的位置
    }
}
