123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /** @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) {}
- }
|