var reGameStates = require('GameStates');

cc.Class({
    extends: cc.Component,
    properties: {
        //人物类型
        AI_Worker_Type: {
            default: reGameStates.AI_Worker_Type.Rube,
            type: cc.Enum(reGameStates.AI_Worker_Type),
            serializable: true,
        },
        AI_Animation_Type: {
            default: reGameStates.AI_Animation_Type.None,
            type: cc.Enum(reGameStates.AI_Animation_Type),
            serializable: true,
        },
        FrontName: {
            default: '',
            visible: false,
            serializable: false,
        },
        BackName: {
            default: '',
            visible: false,
            serializable: false,
        },
        workerFrontName: {
            default: '',
            visible: false,
            serializable: false,
        },

        //是否循环
        isLoop: false,

        aiTypeName: {
            default: '',
            visible: false,
            serializable: false,
        },

        dragonBones: {
            default: null,
            type: cc.Node,
            serializable: false,
        }
    },

    onLoad: function () {
        this.dragonBones = this.node.getChildByName('dragonBones');
        //获取 ArmatureDisplay
        this._armatureDisPlay = this.dragonBones.getComponent(dragonBones.ArmatureDisplay)

        if (this._armatureDisPlay) {
            this.onSwitchWorkerState(this.AI_Animation_Type);
            // this._armature = this._armatureDisPlay.armature();
        } else {
            cc.warn('this._armatureDisPlay 为空。');
        }
    },

    //切换骨架
    onSwitchArmature() {

        switch (this.AI_Worker_Type) {
            case reGameStates.AI_Worker_Type.CarryGay:
                //运输工人时候骨架名称
                this._armatureDisPlay.armatureName = "WithTricycle";
                break;
            case reGameStates.AI_Worker_Type.Rube:
                //其他三种工作状态骨架的名称
                this._armatureDisPlay.armatureName = "WithoutTricycl";
                this.workerFrontName = 'poke_front';
                //动态设置名字
                this.FrontName = 'empty_walk_front';
                this.BackName = 'empty_walk_back';
                break;
            case reGameStates.AI_Worker_Type.Lumberjack:
                //其他三种工作状态骨架的名称
                this._armatureDisPlay.armatureName = "WithoutTricycl";
                this.workerFrontName = 'hatchet_cut_front';
                //动态设置名字
                this.FrontName = 'hatchet_walk_front';
                this.BackName = 'hatchet_walk_back';
                break;
            case reGameStates.AI_Worker_Type.Miner:
                //其他三种工作状态骨架的名称
                this._armatureDisPlay.armatureName = "WithoutTricycl";
                //矿工没有工作状态的
                this.workerFrontName = '';
                //动态设置名字
                this.FrontName = 'pickaxe_walk_front';
                this.BackName = 'pickaxe_walk_back';
                break;
            case reGameStates.AI_Worker_Type.None:
                //切换
                this._armatureDisPlay.armatureName = "WithoutTricycl";
                //没有工作状态的
                this.workerFrontName = '';
                break;
        }

    },
    // 切换动作流程：1.先切换骨架；2.再设置状态
    // 切换当前是什么工人
    onSwitchWorker(_workerType) {
        this.AI_Worker_Type = _workerType;
        switch (this.AI_Worker_Type) {
            case reGameStates.AI_Worker_Type.CarryGay:
                //运输工人时候骨架名称
                this._armatureDisPlay.armatureName = "WithTricycle";
                break;
            case reGameStates.AI_Worker_Type.Rube:
            case reGameStates.AI_Worker_Type.Lumberjack:
            case reGameStates.AI_Worker_Type.Miner:
            case reGameStates.AI_Worker_Type.Nothing:
                this._armatureDisPlay.armatureName = "WithoutTricycl";
                break;
        }
    },
    //切换工作状态
    onSwitchWorkerState(_animationType) {
        this.AI_Animation_Type = _animationType;
        //  None: -1,//没有状态
        //  Walk: -1,//目前Run 
        //  Idle: -1,//站立
        //  Hatchet: -1,//砍树
        //  Pickaxe: -1,//挖矿
        //  Poke: -1,//摘果实
        //  CargoCarryBox: -1,//货物运输，有车
        //  CargoCarryRaw: -1,//载货原料
        switch (_animationType) {
            case reGameStates.AI_Animation_Type.None:
            case reGameStates.AI_Animation_Type.Idle:
                //动态设置名字
                this.FrontName = 'empty_idle_front';
                this.BackName = 'empty_idle_back';
                break;
            case reGameStates.AI_Animation_Type.Walk:
                //动态设置名字
                this.FrontName = 'empty_walk_front';
                this.BackName = 'empty_walk_back';
                break;
            case reGameStates.AI_Animation_Type.Hatchet:
                this.FrontName = 'hatchet_walk_front';
                this.BackName = 'hatchet_walk_back';

                this.workerFrontName = 'hatchet_cut_front';
                break;
            case reGameStates.AI_Animation_Type.Pickaxe:
                this.FrontName = 'pickaxe_walk_front';
                this.BackName = 'pickaxe_walk_back';

                break;
            case reGameStates.AI_Animation_Type.Poke:
                this.workerFrontName = 'poke_front';
                break;


            case reGameStates.AI_Animation_Type.CargoCarryBox:
                this.FrontName = 'box_front';
                this.BackName = 'box_back';
                break;
            case reGameStates.AI_Animation_Type.CargoCarryRaw:
                this.FrontName = 'bag_front';
                this.BackName = 'bag_back';
                break;
        }
    },
    //播放对应的动画
    switchAnimation: function (direction) {
        let loopValue = this.isLoop ? 0 : 1;
        switch (direction) {
            case reGameStates.moveType.moveUp:
                this.dragonBones.scaleX = 1;
                if (this.BackName)
                    this._armatureDisPlay.playAnimation(this.BackName, loopValue);
                break;
            case reGameStates.moveType.moveRight:
                this.dragonBones.scaleX = -1;
                if (this.BackName)
                    this._armatureDisPlay.playAnimation(this.BackName, loopValue);
                break;
            case reGameStates.moveType.moveDown:
                this.dragonBones.scaleX = 1;
                if (this.FrontName)
                    this._armatureDisPlay.playAnimation(this.FrontName, loopValue);
                break;
            case reGameStates.moveType.moveLeft:
                this.dragonBones.scaleX = -1;
                if (this.FrontName)
                    this._armatureDisPlay.playAnimation(this.FrontName, loopValue);
                break;

        }
    },
    //工作动画
    playWorkerAnimation(isLeft) {
        if (isLeft) {
            this.dragonBones.scaleX = 1;
        } else {
            this.dragonBones.scaleX = -1;
        }
        this._armatureDisPlay.playAnimation(this.workerFrontName, 0);
    },

})
