import worldentity from 'worldEntity';
var KBEngine = require("kbengine");


cc.Class({
    extends: worldentity,

    properties: {

        spine: {
            default: null,
            visible: false,
        },
        statusArr_redyrun: {
            default: [],
            visible: false,
            type: [cc.String]
        },
        statusArr_run: {
            default: [],
            visible: false,
            type: [cc.String]
        },
        skinName: {
            default: [],
            visible: false,
            type: [cc.String]
        },
        //速度
        speed: {
            default: 1,
            type: cc.Integer
        },
        cameraNode: {
            default: null,
            type: cc.Node
        },

        //需要隐藏的节点
        HideNode: {
            default: [],
            type: [cc.Node]
        },

        //最后一个节点
        isEndAction: {
            default: false,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.cameraNode = cc.find("Canvas/Main Camera");
        this.spine = this.node.getChildByName('skeleton').getComponent('sp.Skeleton');
        this.initData();
    },

    start() {

        if (!this.isControlled)
            this.onHideNode();
    },

    initData() {
        //todo 准备跑步动作
        this.statusArr_redyrun = [
            //0
            "idle",
            //1
            "idle-run",

        ];
        //todo 跑步动作
        this.statusArr_run = [
            //0
            "Run1",
            //1
            "Run2",
            //2
            "Run3",
            //3
            "Run4",
            //4
            "Run5",
        ];

        //todo 皮肤名字
        this.skinName = [
            //0
            "Yuanban",
            //1
            "Baozhatou",
        ];
    },

    //隐藏节点
    onHideNode() {
        let length = this.HideNode.length;
        for (let i = 0; i < length; i++) {
            this.HideNode[i].active = false;
        }
    },
    //根据index设置皮肤
    setSkinFromIndex: function (index) {
        this.spine.setSkin(this.skinName[index]);
    },
    //设置皮肤
    setSkins: function (skinName) {
        this.spine.setSkin(skinName);
    },
    setAvatarSpeed: function (speed) {
        this.speed = speed;
        console.log('设置人物速度：', this.speed);
        if (!this.isControlled) {
            //设置游戏当前全局速度
            GLOBAL_DATA.GAME.setGlobalSpeed(speed);
        }
    },
    moveToPosition: function (position) {
        this._moveToPosition(position);
    },

    stopMoveAction() {
        // console.log('停止action');
        this.node.stopAllActions();

        if (!this.isControlled)
            return;
        this.cameraNode.stopAllActions();
    },

    _moveToPosition: function (position) {
        // console.log(position,this.node.position);
        // if(!this.isControlled){
        //     console.log(position,this.node.position);
        // }
        this.stopMoveAction();

        let x = position.x - this.node.x;
        let y = position.y - this.node.y;
        let t = (Math.sqrt(x * x + y * y) / CONVERSION_DISTANCE_UNIT)
            / (this.speed / CONVERSION_SPEED_UNIT);
        // console.log( position.x,'==', this.node.x,',t:',t);
        // var playerX = new cc.Vec2(position.x,0);
        // var act1 = cc.speed(cc.moveTo(t, position),1);
        let act1 = cc.moveTo(t, position);
        let delay = cc.delayTime(0.1);
        let act2 = cc.callFunc(() => {
            if (this.isEndAction) {
                console.log('move end');
                // KBEngine.app.player().actionEnd();
            }
        });
        let actFun = cc.sequence(act1, delay, act2);
        // this.node.runAction(actFun);
        this.node.setPosition(position);
        if (!this.isControlled)
            return;
    
        //只移动
        // var moveX = new cc.Vec2(position.x,0);
        // let cameraAct1 = cc.moveTo(t, position);
        // this.cameraNode.runAction(cameraAct1);
        this.cameraNode.setPosition(position);
    },


    idle: function () {
        this.spine.timeScale = 1.0;
        this.spine.setAnimation(0, this.statusArr_redyrun[0], true);
    },
    run: function () {
        this.spine.setAnimation(0, this.statusArr_run[0], true);
        //奔跑等级
        this.walkLv = 0;
    },
    // update (dt) {},
});
