/** @format */ import {EventProcess} from './EventProcess' import {EventAttack, EventBase, EventHPChange, EventHurtOrAdd, EventSkill, EventSlowDown, EventType} from './NodeEvent' import {IRoleConfig} from '../../config/RoleConfig' import {Data} from '../../GameControl' import {GAME_SCALE_TYPE} from '../../enums/Enum' import {FlashColor} from './FlashColor' const {ccclass, property} = cc._decorator @ccclass export class RoleSpineProcess extends EventProcess { public roleConfig: IRoleConfig public isFriend: boolean spine: sp.Skeleton = null private spineScale: number private attackScale: number private curSkillLoop: number private flashColor: FlashColor onAttach(): void { this.spine = this.node.getComponent(sp.Skeleton) this.flashColor = this.node.getComponent(FlashColor) this.flashColor.renderCom = this.spine if (Data.main.roleSpineMap.get(this.roleConfig.url)) { this.spine.setStartListener(trackEntry => { let animationName = trackEntry.animation ? trackEntry.animation.name : '' if (animationName.includes('attack')) { this.spine.timeScale = (Data.game.spineTimeRate / this.attackScale) * Data.game.gameSpeed } else { this.spine.timeScale = Data.game.spineTimeRate * Data.game.gameSpeed } }) this.spine.setCompleteListener(trackEntry => { let animationName = trackEntry.animation ? trackEntry.animation.name : '' if (animationName.includes('attack') || animationName == 'skill_end' || animationName == 'enter') { this.spine.setAnimation(0, 'stand', true) } else if (animationName == 'skill_start') { this.spine.setAnimation(0, 'skill_loop', true) } else if (animationName == 'skill_loop') { this.curSkillLoop -= 1 this.spine.setAnimation(0, this.curSkillLoop <= 0 ? 'skill_end' : 'skill_loop', true) } }) this.node.active = true this.spine.skeletonData = Data.main.roleSpineMap.get(this.roleConfig.url) this.spine.setAnimation( 0, this.spine.skeletonData.getRuntimeData().findAnimation('enter') ? 'enter' : 'stand', true, ) this.curSkillLoop = 1 } else { this.spine.skeletonData = null this.node.active = false } } onDetach(): void {} init(cfg: IRoleConfig, isFriend: boolean) { this.spineScale = cfg.gameScale[GAME_SCALE_TYPE.spine] / 100 this.isFriend = isFriend this.node.scaleX = this.isFriend ? this.spineScale : -this.spineScale this.node.scaleY = this.spineScale this.roleConfig = cfg this.node.name = this.roleConfig.url } public syncDir(dir: cc.Vec2) { this.node.scaleX = dir.x >= 0 ? this.spineScale : -this.spineScale } processEvent(event: EventBase): void { if (!cc.isValid(this.node)) return //console.log('event.type', EventType[event.type]) switch (event.type) { case EventType.Stand: this._spineStand() break case EventType.Run: this._spineRun() break case EventType.Attack: let attackEvent: EventAttack = event as EventAttack this.syncDir(attackEvent.dir) this.attackScale = attackEvent.attackScale this._spineAttack(attackEvent) break case EventType.Skill: let skillEvent: EventSkill = event as EventSkill if (skillEvent.castTime > 0) { this._spineSkill(skillEvent) } break case EventType.StopSkill: this.curSkillLoop = 0 this._spineStand() break case EventType.Die: this._spineDie() break case EventType.HurtOrAdd: let eventHPChange = event as EventHurtOrAdd if (eventHPChange.changeHP < 0) this.flashColor.hurt() break case EventType.SlowDown: let eventSlowDown = event as EventSlowDown this.flashColor.isSlow = eventSlowDown.isSlow break } } _spineStand() { if (!this.node.active) return let oldAnim = this.spine.animation if (oldAnim != 'stand') this.spine.setAnimation(0, 'stand', true) } _spineRun() { if (!this.node.active) return this.spine.setAnimation(0, 'run', true) } _spineAttack(attackEvent: EventAttack) { if (!this.node.active) return this.spine.setAnimation(0, `attack${attackEvent.attackIndex ? attackEvent.attackIndex : ''}`, false) //this.spine.addAnimation(0, 'stand', true) if (attackEvent.isShoot) { } else { } } _spineDie() { if (!this.node.active) return this.spine.setAnimation(0, 'die', false) } _spineSkill(eventSkill: EventSkill) { if (!this.node.active) return if ( this.spine.findAnimation('skill_start') || this.spine.findAnimation(`skill${eventSkill.skillIndex}_start`) ) { let preName = this.spine.findAnimation('skill_start') ? 'skill' : `skill${eventSkill.skillIndex}` let startTime = this.spine.findAnimation(`${preName}_start`).duration / Data.game.spineTimeRate let loopTime = this.spine.findAnimation(`${preName}_loop`).duration / Data.game.spineTimeRate let endTime = this.spine.findAnimation(`${preName}_end`).duration / Data.game.spineTimeRate let loopTimes = Math.ceil((eventSkill.castTime - startTime - endTime) / loopTime) //console.log('time', startTime, loopTime, endTime, loopTimes) this.curSkillLoop = loopTimes this.spine.setAnimation(0, `${preName}_start`, false) } else { let hasIndex = this.spine.findAnimation(`skill${eventSkill.skillIndex}`) this.spine.setAnimation(0, hasIndex ? `skill${eventSkill.skillIndex}` : 'skill', false) } } // update (dt) {} }