import CMath from "../../../../util/CMath";
import FF from "../../FF";
import FSprite from "../../object/FSprite";

/**
 * 巡逻兵
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FPatrol extends cc.Component {

    @property({
        displayName: '起点到终点时间(秒)'
    })
    public time = 2;

    @property({
        displayName: '每次巡逻停留时间(秒)'
    })
    public interval = 0.5;

    @property({
        type:cc.Node,
        displayName: '巡逻目的地'
    })
    public target:cc.Node = null;

    @property({
        type:cc.Node,
        displayName: '警示区域'
    })
    public warn:cc.Node = null;

    @property({
        type:sp.Skeleton,
        displayName: '精灵动画'
    })
    public spine:sp.Skeleton = null;

    private initPos:cc.Vec2;
    private targetPos:cc.Vec2;

    public ff:FF;

    onLoad () {
        this.initPos = this.node.getPosition();
        this.targetPos = this.target.getPosition();

        this.moveToB();
    }
    private moveToB(){
        cc.tween(this.node).sequence(
            cc.delayTime(this.interval),
            cc.callFunc(()=>{
                // if(this.targetPos.x > 0){
                //     this.node.scaleX = Math.abs(this.node.scaleX);
                // }else{
                //     this.node.scaleX = -Math.abs(this.node.scaleX);
                // }
            }),
            cc.moveBy(this.time,this.targetPos),
            cc.callFunc(()=>{
                this.moveToA();
            })
        ).start();

        cc.tween(this.warn).sequence(
            cc.delayTime(this.interval),
            cc.callFunc(()=>{
                let angle = CMath.getAngle(cc.Vec2.ZERO_R,this.targetPos)*180/Math.PI;
                this.warn.angle = angle;
            }),
            cc.moveBy(this.time,this.targetPos),
            cc.callFunc(()=>{
            })
        ).start();
    }
    private moveToA(){
        let pos = this.targetPos.clone();
        pos.x = -pos.x;
        pos.y = -pos.y;

        cc.tween(this.node).sequence(
            cc.delayTime(this.interval),
            cc.callFunc(()=>{
                // if(pos.x > 0){
                //     this.node.scaleX = Math.abs(this.node.scaleX);
                // }else{
                //     this.node.scaleX = -Math.abs(this.node.scaleX);
                // }
            }),
            cc.moveBy(this.time,pos),
            cc.callFunc(()=>{
                this.moveToB();
            })
        ).start();

        cc.tween(this.warn).sequence(
            cc.delayTime(this.interval),
            cc.callFunc(()=>{
                let angle = CMath.getAngle(cc.Vec2.ZERO_R,pos)*180/Math.PI;
                this.warn.angle = angle;
            }),
            cc.moveBy(this.time,pos),
            cc.callFunc(()=>{
            })
        ).start();
    }

}