RoleSpineProcess.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /** @format */
  2. import {EventProcess} from './EventProcess'
  3. import {EventAttack, EventBase, EventHPChange, EventHurtOrAdd, EventSkill, EventSlowDown, EventType} from './NodeEvent'
  4. import {IRoleConfig} from '../../config/RoleConfig'
  5. import {Data} from '../../GameControl'
  6. import {GAME_SCALE_TYPE} from '../../enums/Enum'
  7. import {FlashColor} from './FlashColor'
  8. const {ccclass, property} = cc._decorator
  9. @ccclass
  10. export class RoleSpineProcess extends EventProcess {
  11. public roleConfig: IRoleConfig
  12. public isFriend: boolean
  13. spine: sp.Skeleton = null
  14. private spineScale: number
  15. private attackScale: number
  16. private curSkillLoop: number
  17. private flashColor: FlashColor
  18. onAttach(): void {
  19. this.spine = this.node.getComponent(sp.Skeleton)
  20. this.flashColor = this.node.getComponent(FlashColor)
  21. this.flashColor.renderCom = this.spine
  22. if (Data.main.roleSpineMap.get(this.roleConfig.url)) {
  23. this.spine.setStartListener(trackEntry => {
  24. let animationName = trackEntry.animation ? trackEntry.animation.name : ''
  25. if (animationName.includes('attack')) {
  26. this.spine.timeScale = (Data.game.spineTimeRate / this.attackScale) * Data.game.gameSpeed
  27. } else {
  28. this.spine.timeScale = Data.game.spineTimeRate * Data.game.gameSpeed
  29. }
  30. })
  31. this.spine.setCompleteListener(trackEntry => {
  32. let animationName = trackEntry.animation ? trackEntry.animation.name : ''
  33. if (animationName.includes('attack') || animationName == 'skill_end' || animationName == 'enter') {
  34. this.spine.setAnimation(0, 'stand', true)
  35. } else if (animationName == 'skill_start') {
  36. this.spine.setAnimation(0, 'skill_loop', true)
  37. } else if (animationName == 'skill_loop') {
  38. this.curSkillLoop -= 1
  39. this.spine.setAnimation(0, this.curSkillLoop <= 0 ? 'skill_end' : 'skill_loop', true)
  40. }
  41. })
  42. this.node.active = true
  43. this.spine.skeletonData = Data.main.roleSpineMap.get(this.roleConfig.url)
  44. this.spine.setAnimation(
  45. 0,
  46. this.spine.skeletonData.getRuntimeData().findAnimation('enter') ? 'enter' : 'stand',
  47. true,
  48. )
  49. this.curSkillLoop = 1
  50. } else {
  51. this.spine.skeletonData = null
  52. this.node.active = false
  53. }
  54. }
  55. onDetach(): void {}
  56. init(cfg: IRoleConfig, isFriend: boolean) {
  57. this.spineScale = cfg.gameScale[GAME_SCALE_TYPE.spine] / 100
  58. this.isFriend = isFriend
  59. this.node.scaleX = this.isFriend ? this.spineScale : -this.spineScale
  60. this.node.scaleY = this.spineScale
  61. this.roleConfig = cfg
  62. this.node.name = this.roleConfig.url
  63. }
  64. public syncDir(dir: cc.Vec2) {
  65. this.node.scaleX = dir.x >= 0 ? this.spineScale : -this.spineScale
  66. }
  67. processEvent(event: EventBase): void {
  68. if (!cc.isValid(this.node)) return
  69. if (this.roleConfig?.ID == 12001) console.log('event.type', EventType[event.type])
  70. switch (event.type) {
  71. case EventType.Stand:
  72. this._spineStand()
  73. break
  74. case EventType.Run:
  75. this._spineRun()
  76. break
  77. case EventType.Attack:
  78. let attackEvent: EventAttack = event as EventAttack
  79. this.syncDir(attackEvent.dir)
  80. this.attackScale = attackEvent.attackScale
  81. this._spineAttack(attackEvent)
  82. break
  83. case EventType.Skill:
  84. let skillEvent: EventSkill = event as EventSkill
  85. if (skillEvent.castTime > 0) {
  86. this._spineSkill(skillEvent)
  87. }
  88. break
  89. case EventType.StopSkill:
  90. this.curSkillLoop = 0
  91. this._spineStand()
  92. break
  93. case EventType.Die:
  94. this._spineDie()
  95. break
  96. case EventType.HurtOrAdd:
  97. let eventHPChange = event as EventHurtOrAdd
  98. if (eventHPChange.changeHP < 0) this.flashColor.hurt()
  99. break
  100. case EventType.SlowDown:
  101. let eventSlowDown = event as EventSlowDown
  102. this.flashColor.isSlow = eventSlowDown.isSlow
  103. break
  104. }
  105. }
  106. _spineStand() {
  107. if (!this.node.active) return
  108. let oldAnim = this.spine.animation
  109. if (oldAnim != 'stand') this.spine.setAnimation(0, 'stand', true)
  110. }
  111. _spineRun() {
  112. if (!this.node.active) return
  113. this.spine.setAnimation(0, 'run', true)
  114. }
  115. _spineAttack(attackEvent: EventAttack) {
  116. if (!this.node.active) return
  117. this.spine.setAnimation(0, `attack${attackEvent.attackIndex ? attackEvent.attackIndex : ''}`, false)
  118. //this.spine.addAnimation(0, 'stand', true)
  119. if (attackEvent.isShoot) {
  120. } else {
  121. }
  122. }
  123. _spineDie() {
  124. if (!this.node.active) return
  125. this.spine.setAnimation(0, 'die', false)
  126. }
  127. _spineSkill(eventSkill: EventSkill) {
  128. if (!this.node.active) return
  129. if (
  130. this.spine.findAnimation('skill_start') ||
  131. this.spine.findAnimation(`skill${eventSkill.skillIndex}_start`)
  132. ) {
  133. let preName = this.spine.findAnimation('skill_start') ? 'skill' : `skill${eventSkill.skillIndex}`
  134. let startTime = this.spine.findAnimation(`${preName}_start`).duration / Data.game.spineTimeRate
  135. let loopTime = this.spine.findAnimation(`${preName}_loop`).duration / Data.game.spineTimeRate
  136. let endTime = this.spine.findAnimation(`${preName}_end`).duration / Data.game.spineTimeRate
  137. let loopTimes = Math.ceil((eventSkill.castTime - startTime - endTime) / loopTime)
  138. //console.log('time', startTime, loopTime, endTime, loopTimes)
  139. this.curSkillLoop = loopTimes
  140. this.spine.setAnimation(0, `${preName}_start`, false)
  141. } else {
  142. let hasIndex = this.spine.findAnimation(`skill${eventSkill.skillIndex}`)
  143. this.spine.setAnimation(0, hasIndex ? `skill${eventSkill.skillIndex}` : 'skill', false)
  144. }
  145. }
  146. // update (dt) {}
  147. }