RoleEventProcess.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /** @format */
  2. import {
  3. EventAttack,
  4. EventBase,
  5. EventDie,
  6. EventGraphicsDraw,
  7. EventHPChange,
  8. EventRoleTip,
  9. EventSkill,
  10. EventType,
  11. } from './NodeEvent'
  12. import {ccUtils} from '../../utils/ccUtils'
  13. import FrameAnimation from '../../uiutils/FrameAnimation'
  14. import {Data} from '../../GameControl'
  15. import {IRoleConfig} from '../../config/RoleConfig'
  16. import {GAME_ROLE_TIP, MONSTER_TYPE, ROLE_TYPE} from '../../enums/Enum'
  17. import {EventProcess} from './EventProcess'
  18. import {IBuff} from './GameInterface'
  19. import {FlashColor} from './FlashColor'
  20. const {ccclass, property} = cc._decorator
  21. @ccclass
  22. export class RoleEventProcess extends EventProcess {
  23. public roleConfig: IRoleConfig
  24. public isFriend: boolean
  25. private eventDeath: EventDie
  26. private eventRoleTipMap: Map<string, EventRoleTip[]>
  27. private frameAniNode: cc.Node
  28. private roleTipParents: Map<string, cc.Node>
  29. private roleHeight: number = 100
  30. private roleWidth: number = 60
  31. private HPNode: cc.Node
  32. private HP: cc.Node
  33. private HPS: cc.Node
  34. private HPShield: cc.Node
  35. protected onLoad() {
  36. this.HPNode = cc.find('hpNode', this.node)
  37. this.HP = cc.find('hpNode/HP', this.node)
  38. this.HPShield = cc.find('hpNode/HPshield', this.node)
  39. this.HPS = cc.find('hpNode/HPS', this.node)
  40. this.frameAniNode = cc.find('fa', this.node)
  41. this.roleTipParents = new Map()
  42. this.eventRoleTipMap = new Map()
  43. for (let key in GAME_ROLE_TIP) {
  44. let parent = cc.find(`tips/${key}Parent`, this.node)
  45. if (!parent) {
  46. console.error('role预制体tips下缺少' + key + '父节点')
  47. } else {
  48. this.roleTipParents.set(key, parent)
  49. this.eventRoleTipMap.set(key, [])
  50. }
  51. }
  52. }
  53. onAttach(): void {
  54. this.frameAnimation = this.frameAniNode.getComponent(FrameAnimation)
  55. this._initHPNode()
  56. }
  57. onDetach(): void {}
  58. init(cfg: IRoleConfig, isFriend: boolean, width: number, height: number) {
  59. this.isFriend = isFriend
  60. this.roleConfig = cfg
  61. this.roleWidth = width
  62. this.roleHeight = height
  63. this.eventRoleTipMap.forEach((value, key) => {
  64. value.length = 0
  65. })
  66. this.node.name = this.roleConfig.url
  67. let entityLb = cc.find('entity', this.node)
  68. if (entityLb) entityLb.color = this.isFriend ? cc.Color.GREEN : cc.Color.RED
  69. this.HPNode.active = false
  70. }
  71. public syncDir(dir: cc.Vec2) {
  72. let scaleRate = 1
  73. this.frameAniNode.scaleX = dir.x >= 0 ? scaleRate : -scaleRate
  74. }
  75. processEvent(event: EventBase): void {
  76. if (!cc.isValid(this.node)) return
  77. switch (event.type) {
  78. case EventType.Enter:
  79. break
  80. case EventType.Stand:
  81. break
  82. case EventType.Run:
  83. break
  84. case EventType.Attack:
  85. let attackEvent: EventAttack = event as EventAttack
  86. this.syncDir(attackEvent.dir)
  87. break
  88. case EventType.Skill:
  89. break
  90. case EventType.RoleTip:
  91. let eventRoleTip: EventRoleTip = event as EventRoleTip
  92. // let roleTips = this.eventRoleTipMap.get(eventRoleTip.tipType)
  93. // roleTips.push(eventRoleTip)
  94. this._showNextRoleTip(eventRoleTip)
  95. break
  96. case EventType.Die:
  97. this.eventDeath = event as EventDie
  98. break
  99. case EventType.HPChange:
  100. this._changeHP(event as EventHPChange)
  101. break
  102. // case EventType.BuffChange:
  103. // let eventBuff = event as EventBuffChange
  104. // this.initBuff(eventBuff.buffs)
  105. // break
  106. case EventType.GraphicsDraw:
  107. if (cc.debug.isDisplayStats()) {
  108. this.graphicsDraw(event as EventGraphicsDraw)
  109. } else {
  110. this.graphicsDraw(null)
  111. }
  112. break
  113. }
  114. }
  115. private _initHPNode() {
  116. if (!this.roleConfig) {
  117. console.warn('没有角色配置')
  118. return
  119. }
  120. let hpNode = this.HPNode
  121. let isBase = this.roleConfig.type == ROLE_TYPE.base
  122. //let hpWidth = isBase ? 200 : 100
  123. //修改为配置
  124. let hpWidth = this.roleConfig.hplength
  125. let hpHeight = isBase ? 20 : 10
  126. let roleHeight = this.roleHeight
  127. let baseHeight = 454
  128. let baseDurX = 820 - Data.game.fightSizeMaxX
  129. let baseDurY = 88 - (Data.game.fightSizeMaxY + Data.game.fightSizeMinY) / 2
  130. hpNode.x = isBase ? (this.isFriend ? -1 : 1) * baseDurX : 0
  131. hpNode.y = isBase ? baseHeight / 2 + baseDurY - hpHeight : roleHeight + hpHeight / 2
  132. cc.find('green', this.HP).active = this.isFriend
  133. cc.find('red', this.HP).active = !this.isFriend
  134. this.HP.getComponent(cc.ProgressBar).barSprite = cc
  135. .find(this.isFriend ? 'green' : 'red', this.HP)
  136. .getComponent(cc.Sprite)
  137. this.setHPPbSize(this.HP, hpWidth, hpHeight)
  138. this.setHPPbSize(this.HPShield, hpWidth, hpHeight)
  139. let baseHPbg = cc.find('castle_blood_bottom', hpNode)
  140. baseHPbg.active = isBase
  141. baseHPbg.width = hpWidth + 8
  142. baseHPbg.height = hpHeight + 8
  143. this.HPS.width = hpWidth
  144. this.HPS.x = -hpWidth / 2
  145. this.HPS.height = hpHeight
  146. this.HPS.children.forEach(v => (v.height = hpHeight))
  147. }
  148. private setHPPbSize(node: cc.Node, width: number, height: number) {
  149. node.width = width
  150. node.height = height
  151. let pb = node.getComponent(cc.ProgressBar)
  152. pb.totalLength = width
  153. pb.barSprite.node.x = -width / 2
  154. node.children.forEach(v => (v.height = height))
  155. }
  156. private _changeHP(event: EventHPChange) {
  157. //let lastAllHp = event.HP
  158. let isFriend =
  159. this.roleConfig.type == ROLE_TYPE.normal ||
  160. this.roleConfig.type == ROLE_TYPE.only ||
  161. this.roleConfig.type == ROLE_TYPE.totem
  162. let isBase = this.roleConfig.type == ROLE_TYPE.base
  163. let isBoss = this.roleConfig.type == MONSTER_TYPE.boss
  164. if (!this.HPNode.active && !isBoss && !isBase && (event.HP != event.nowHP || event.shieldHP > 0) && !isFriend)
  165. this.HPNode.active = true
  166. if (!this.HPNode.active) return
  167. let allHp = event.HP
  168. //if (event.lastHP + event.lastShieldHP > lastAllHp) lastAllHp = event.lastHP + event.lastShieldHP
  169. if (event.nowHP + event.shieldHP > allHp) allHp = event.nowHP + event.shieldHP
  170. //let from = event.lastHP / lastAllHp
  171. this.HP.getComponent(cc.ProgressBar).progress = event.nowHP / allHp
  172. //let from = (event.lastHP + event.lastShieldHP) / lastAllHp
  173. this.HPShield.getComponent(cc.ProgressBar).progress = (event.nowHP + event.shieldHP) / allHp
  174. let hpCellNum = Math.ceil(allHp / 100)
  175. //if (hpCellNum > 10) hpCellNum = 10
  176. //修改为固定10格血
  177. if (hpCellNum > 10) hpCellNum = 10
  178. if (hpCellNum != this.HPS.children.length) {
  179. ccUtils.instantChildren(cc.find('hp', this.HPS), hpCellNum)
  180. }
  181. }
  182. private _showNextRoleTip(roleTip: EventRoleTip) {
  183. let gameUI = this.world.fightCore
  184. //roleTips.length > 0 &&
  185. if (this.node.isValid && gameUI.node.isValid) {
  186. //let roleTip: EventRoleTip = roleTips.shift() // 取出下一个提示对象
  187. let tipNode = null
  188. let key = roleTip.tipType
  189. let pool = gameUI.roleTipPool.get(key)
  190. if (pool.size() > 0 && pool.size() < 100) {
  191. tipNode = pool.get()
  192. } else {
  193. tipNode = cc.instantiate(Data.game.gameAssetMap.get(`prefab/fight/roleTip/${key}`))
  194. }
  195. let t = cc.tween
  196. let tipX: number, tipY: number, tipOpacity: number, moveTime: number, delayTime: number, tween: cc.Tween
  197. let scale = 1
  198. let actionCallback = () => {
  199. tipNode.removeFromParent()
  200. gameUI.node.isValid && pool.put(tipNode)
  201. }
  202. let attackLbDur = 50
  203. switch (roleTip.tipType) {
  204. case GAME_ROLE_TIP.buffTip:
  205. tipX = 0
  206. tipY = this.roleHeight / 2
  207. tipOpacity = 255
  208. moveTime = 1
  209. tween = t().by(moveTime, {y: this.roleHeight / 2})
  210. let upNode = cc.find('up', tipNode)
  211. upNode.active = roleTip.num == 1
  212. let downNode = cc.find('down', tipNode)
  213. downNode.active = roleTip.num == 2
  214. let showNode = upNode.active ? upNode : downNode
  215. ccUtils.setLabel(roleTip.tip, showNode, 'lb')
  216. break
  217. case GAME_ROLE_TIP.hpTip:
  218. tipX = 0
  219. tipY = 0
  220. tipOpacity = 255
  221. moveTime = 0.5
  222. tween = t().by(moveTime, {y: this.roleHeight / 2})
  223. break
  224. case GAME_ROLE_TIP.critTip:
  225. tipX = Math.randomRangeInt(-this.roleWidth / 4, this.roleWidth / 4)
  226. let left = tipX < 0 ? -1 : 1
  227. tipY = this.roleHeight / 2 + Math.randomRangeInt(0, 20)
  228. scale = 1
  229. tipOpacity = 255
  230. moveTime = 1
  231. tween = t().by(moveTime * 0.4, {
  232. x: (left * this.roleWidth) / 8,
  233. y: this.HP.height + tipNode.height + 30,
  234. })
  235. tween = t().parallel(
  236. tween,
  237. t()
  238. .to(moveTime * 0.1, {scale: 3})
  239. .to(moveTime * 0.3, {scale: 1})
  240. .union(),
  241. )
  242. tween = t().sequence(tween, t().to(moveTime, {opacity: 0}).by(moveTime, {y: 10}).union())
  243. break
  244. case GAME_ROLE_TIP.damageTip:
  245. tipX = this.isFriend ? -attackLbDur : attackLbDur
  246. tipY = this.HP.parent.y - this.HP.height
  247. scale = 2
  248. tipOpacity = 0
  249. moveTime = 0.5
  250. delayTime = 0.5
  251. tween = t().by(moveTime, {y: 30})
  252. tween = t().parallel(tween, t().to(moveTime, {opacity: 255, scale: 1.2}).delay(delayTime))
  253. break
  254. case GAME_ROLE_TIP.missTip:
  255. tipX = this.isFriend ? -attackLbDur : attackLbDur
  256. tipY = this.HP.parent.y - this.HP.height
  257. tipOpacity = 0
  258. moveTime = 0.5
  259. delayTime = 0.5
  260. tween = t().by(moveTime, {y: 30})
  261. tween = t().parallel(tween, t().to(moveTime, {opacity: 255, scale: 1.2}).delay(delayTime))
  262. break
  263. }
  264. if (roleTip.tipType != GAME_ROLE_TIP.buffTip) ccUtils.setLabel(roleTip.tip, tipNode)
  265. this.roleTipParents.get(key).addChild(tipNode)
  266. tipNode.stopAllActions()
  267. tipNode.scale = scale
  268. tipNode.x = tipX
  269. tipNode.y = tipY
  270. tipNode.opacity = tipOpacity
  271. // 执行飘出动画效果
  272. t(tipNode).then(tween).call(actionCallback).start()
  273. // // 0.1秒后显示下一个提示
  274. // this.scheduleOnce(() => {
  275. // this._showNextRoleTip(roleTips)
  276. // }, 0.1)
  277. }
  278. }
  279. // update (dt) {}
  280. }