| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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');
- })
- }
- })
|