var reGameStates = require('GameStates'); // Rube:-1,//农夫 // Lumberjack:-1,//伐木工 // Miner:-1,//矿工 // CarrayGay:-1,//搬运工 //AI类型名称 var AI_Type = ['Rube', 'Lumberjack', 'Miner', 'CarryGay', ] //动画名字 var AI_Animation_Name = ["BankRight", "FrontRight", "FrontLeft", "BankLeft", "WorkLeft", "WorkRight", ] //切换状态时候名字带的后缀 var AI_Worker_EndStatus = [ '_Idle', '_Carry', '_CargoCarry_Empty', '_CargoCarry', '_CargoCarry_Raw', ] 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, }, RightName: { default: '', visible: false, serializable: false, }, DownName: { default: '', visible: false, serializable: false, }, LeftName: { default: '', visible: false, serializable: false, }, UpName: { default: '', visible: false, serializable: false, }, IdleName: { default: '', visible: false, serializable: false, }, workerLeftName: { default: '', visible: false, serializable: false, }, workerRightName: { default: '', visible: false, serializable: false, }, //是否循环 isLoop: false, aiTypeName: { default: '', visible: false, serializable: false, }, }, onLoad: function () { //获取 ArmatureDisplay this._armatureDisPlay = this.node.getChildByName('dragonBones').getComponent(dragonBones.ArmatureDisplay) if (this._armatureDisPlay) { // cc.log('_animationType11111==',AI_Type[this.AI_Worker_Type]); this.aiTypeName = AI_Type[this.AI_Worker_Type]; //获取 Armatrue this._armatureDisPlay.armatureName = this.aiTypeName; this.onSwitchWorkerState(this.AI_Animation_Type); this._armature = this._armatureDisPlay.armature(); //cc.log('this.AI_Worker_Type', this.AI_Worker_Type, this._armatureDisPlay, this._armature.animation.animationNames); //添加动画监听 // this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_IN_COMPLETE, this.animationEventHandler, this) // this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_OUT_COMPLETE, this.animationEventHandler, this) } else { cc.warn('this._armatureDisPlay 为空。'); } }, // 切换当前是什么工人 onSwitchWorker(_workerType) { this.AI_Worker_Type = _workerType; if (this._armatureDisPlay) { // cc.log('_animationType',AI_Type[this.AI_Worker_Type]); this.aiTypeName = AI_Type[this.AI_Worker_Type]; //获取 Armatrue this._armatureDisPlay.armatureName = this.aiTypeName; this.onSwitchWorkerState(this.AI_Animation_Type); this._armature = this._armatureDisPlay.armature(); } else { cc.warn('this._armatureDisPlay 为空。'); } }, //切换工作状态 onSwitchWorkerState(_animationType) { // cc.log('onSwitchWorkerState ==',_animationType); // Idle: -1,//站立 // Carry: -1,//运输 // CargoCarryEmpty:-1,//空的货物运输 // CargoCarry: -1,//货物运输,有车 // CargoCarryRaw: -1,//载货原料 let _AddName = ''; switch (_animationType) { case reGameStates.AI_Animation_Type.Idle: //设置站立 _AddName = AI_Worker_EndStatus[0]; break; case reGameStates.AI_Animation_Type.Carry: //动态设置名字 _AddName = AI_Worker_EndStatus[1]; break; case reGameStates.AI_Animation_Type.CargoCarryEmpty: _AddName = AI_Worker_EndStatus[2]; break; case reGameStates.AI_Animation_Type.CargoCarry: _AddName = AI_Worker_EndStatus[3]; break; case reGameStates.AI_Animation_Type.CargoCarryRaw: _AddName = AI_Worker_EndStatus[4]; break; } //动态设置名字 this.RightName = this.aiTypeName + AI_Animation_Name[0] + _AddName; this.LeftName = this.aiTypeName + AI_Animation_Name[1] + _AddName; this.DownName = this.aiTypeName + AI_Animation_Name[2] + _AddName; this.UpName = this.aiTypeName + AI_Animation_Name[3] + _AddName; this.workerLeftName = this.aiTypeName + AI_Animation_Name[4]; this.workerRightName = this.aiTypeName + AI_Animation_Name[5]; // cc.log('_animationType',this.RightName); }, // attack: function () { // //动画执行方式一 // this._armature.animation.fadeIn('attack1', -1, -1, 0, 'hit'); // }, switchAnimation: function (direction) { //动画执行方式二 // cc.log('switchAnimation', direction); let loopValue = this.isLoop ? 0 : 1; switch (direction) { case reGameStates.moveType.moveUp: if (this.UpName) this._armatureDisPlay.playAnimation(this.UpName, loopValue); break; case reGameStates.moveType.moveRight: if (this.RightName) this._armatureDisPlay.playAnimation(this.RightName, loopValue); break; case reGameStates.moveType.moveDown: if (this.DownName) this._armatureDisPlay.playAnimation(this.DownName, loopValue); break; case reGameStates.moveType.moveLeft: if (this.LeftName) this._armatureDisPlay.playAnimation(this.LeftName, loopValue); break; } }, //工作动画 playWorkerAnimation(isLeft) { if (isLeft) { this._armatureDisPlay.playAnimation(this.workerLeftName, 0); // cc.log(11111); } else { this._armatureDisPlay.playAnimation(this.workerRightName, 0); // cc.log(22222); } }, animationEventHandler: function animationEventHandler(event) { if (event.type == dragonBones.EventObject.FADE_IN_COMPLETE) { cc.log(event.detail.animationName + ' fade in complete'); } else if (event.type == dragonBones.EventObject.FADE_OUT_COMPLETE) { cc.log(event.detail.animationName + ' fade out complete'); } }, //从资源中创建 onClick_add: function () { var self = this; cc.loader.loadResAll('Dragon', function (err, assets) { if (err) { return; } if (assets.length <= 0) { return; } var newHero = new cc.Node(); self.node.addChild(newHero); newHero.setPosition(cc.p(0, 0)); newHero.setScale(0.5, 0.5); var dragonDisplay = newHero.addComponent(dragonBones.ArmatureDisplay); for (var i in assets) { if (assets[i] instanceof dragonBones.DragonBonesAsset) { dragonDisplay.dragonAsset = assets[i]; } if (assets[i] instanceof dragonBones.DragonBonesAtlasAsset) { dragonDisplay.dragonAtlasAsset = assets[i]; } } dragonDisplay.armatureName = 'Dragon'; dragonDisplay.playAnimation('stand'); }) } })