RoleOptCell.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /** @format */
  2. import GameUI from '../ui/GameUI'
  3. import {CardSkillConfig, ICardSkillConfig} from '../config/CardSkillConfig'
  4. import {Data, Mgr} from '../GameControl'
  5. import {EntryConfig, IEntryConfig} from '../config/EntryConfig'
  6. import {ENTRY, ROLE_TYPE} from '../enums/Enum'
  7. import {IRole} from '../interface/GlobalInterface'
  8. const {ccclass, property} = cc._decorator
  9. @ccclass
  10. export class RoleOptCell extends cc.Component {
  11. get canCreate(): boolean {
  12. return this._canCreate
  13. }
  14. set canCreate(value: boolean) {
  15. this._canCreate = value
  16. cc.find('gray', this.node).active = !value
  17. if (value && this.countDown <= 0 && this.gameUI.cost >= this.iRole.cfg.cost) {
  18. this.showCreateAni()
  19. }
  20. }
  21. mask: cc.Sprite = null //遮罩
  22. countDown: number = 0
  23. isStop: boolean = true //是否停止
  24. iRole: IRole = null
  25. gameUI: GameUI
  26. time: number = 1 //冷却时间
  27. createBackCostCfg: IEntryConfig
  28. private _canCreate: boolean = false
  29. onLoad() {
  30. this.mask = cc.find('mask', this.node).getComponent(cc.Sprite)
  31. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchStart, this)
  32. }
  33. update(dt: number) {
  34. dt *= Data.game.gameSpeed
  35. if (!this.isStop && !this.gameUI.isStop) {
  36. this.countDown -= dt
  37. if (this.countDown <= 0) {
  38. this.countDown = 0
  39. this.isStop = true
  40. if (this.canCreate && this.gameUI.cost >= this.iRole.cfg.cost) {
  41. this.showCreateAni()
  42. }
  43. }
  44. this.mask.fillRange = this.time > 0 ? this.countDown / this.time : 0 //恢复技能
  45. }
  46. }
  47. init(iRole: IRole, gameUI: GameUI) {
  48. this.gameUI = gameUI
  49. this.iRole = iRole
  50. this.time = this.iRole.cfg.sumcd
  51. this.mask.fillRange = 1
  52. this.countDown = this.time
  53. this.isStop = false
  54. this.canCreate = true
  55. this.createBackCostCfg = null
  56. this.resetNode()
  57. for (let iEquip of this.iRole.equips) {
  58. if (iEquip && iEquip.cfg.entry) {
  59. let cfg = EntryConfig[iEquip.cfg.entry]
  60. if (cfg.type == ENTRY.createBackCost) {
  61. this.createBackCostCfg = cfg
  62. }
  63. }
  64. }
  65. }
  66. onTouchStart(e: cc.Event.EventTouch): void {
  67. if (!this.iRole || !this.isStop) return
  68. if (!this.canCreate) {
  69. //Mgr.ui.tip('唯一英雄只能存在一个')
  70. return
  71. }
  72. if (this.gameUI.cost >= this.iRole.cfg.cost) {
  73. this.countDown = this.time
  74. this.isStop = false
  75. this.gameUI.castCost += this.iRole.cfg.cost
  76. this.gameUI.cost -= this.iRole.cfg.cost
  77. //召唤返费
  78. let cfg = this.createBackCostCfg
  79. if (cfg && Math.getProb(cfg.rateArr[1] / Data.game.rateNum)) this.gameUI.cost += cfg.rateArr[0]
  80. this.gameUI.world.createRoleEntity(this.iRole.cfg.ID, false, null, this.iRole)
  81. //唯一卡第一次召唤后,死亡前不能再召唤
  82. this.canCreate = this.iRole.cfg.type != ROLE_TYPE.only
  83. this.resetNode()
  84. }
  85. }
  86. resetNode() {
  87. this.node.stopAllActions()
  88. this.node.y = 0
  89. this.node.scale = 1
  90. cc.find('light', this.node).active = false
  91. }
  92. showCreateAni() {
  93. cc.find('light', this.node).active = true
  94. let t = cc.tween
  95. t(this.node)
  96. .then(
  97. t().sequence(
  98. t().to(0.5, {y: 30}),
  99. t().to(0.8, {scale: 1.05}).to(0.8, {scale: 1}).delay(0.5).union().repeat(1),
  100. ),
  101. )
  102. .start()
  103. }
  104. }