Buff.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { _decorator, Component, Node, Prefab, instantiate } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  4. import { BuffData } from './BuffData';
  5. import { CommandSpine } from './CommandSpine';
  6. import { Hero } from './Hero';
  7. import { HitData } from './HitData';
  8. import { Monster } from './Monster';
  9. import { TakeAWalk } from './TakeAWalk';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('Buff')
  12. export class Buff extends Component {
  13. @property({ type: Node, tooltip: "特效层" }) layer: Node;
  14. /**当前buff */
  15. private curBuff: Map<number, { startTime: number, buff: any }> = new Map();
  16. /**当前buff特效 */
  17. private buffEffect: Map<number, CommandSpine> = new Map();
  18. update() {
  19. this.checkBuff();
  20. }
  21. /**移除buff */
  22. public removeBuff(id?: number): void {
  23. if (id) {
  24. this.cancelBuff(this.curBuff.get(id));
  25. this.curBuff.delete(id);
  26. let index = DataSystem.getData(BuffData).curBuffs.indexOf(id);
  27. if (index != -1) {
  28. DataSystem.getData(BuffData).curBuffs.splice(index, 1);
  29. }
  30. } else {
  31. this.curBuff.forEach((v, k) => {
  32. this.cancelBuff(this.curBuff.get(k));
  33. this.curBuff.delete(k);
  34. });
  35. DataSystem.getData(BuffData).curBuffs = [];
  36. }
  37. }
  38. /**添加buff */
  39. public addBuff(buffs: any): void {
  40. for (let i = 0; i < buffs.length; i++) {
  41. this.useBuff(buffs[i]);
  42. }
  43. }
  44. /**检查buff */
  45. private checkBuff(): void {
  46. if (this.curBuff.size > 0) {
  47. this.curBuff.forEach((v, k) => {
  48. let delay = this.curBuff.get(k).buff["time"];
  49. if (delay != -1) {
  50. let endTime = this.curBuff.get(k).startTime + delay * 1000;
  51. if (Date.now() > endTime) {
  52. this.cancelBuff(this.curBuff.get(k));
  53. this.curBuff.delete(k);
  54. }
  55. }
  56. });
  57. }
  58. }
  59. /**buff生效 */
  60. private useBuff(buff: any): void {
  61. let types = (buff["type"] + "").split(",");
  62. for (let i = 0; i < types.length; i++) {
  63. switch (parseInt(types[i])) {
  64. case 0:
  65. this.createCommandEffect("gongji", 0, true);
  66. this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttack = buff["attackPer"] / 10000);
  67. break;
  68. case 1:
  69. this.createCommandEffect("baoji", 1, true);
  70. this.getComponent(Hero) && (this.getComponent(Hero).data._dyCri = buff["cri"]);
  71. break;
  72. case 2:
  73. this.getComponent(Hero) && (this.getComponent(Hero).data._dyMiss = buff["acc"]);
  74. this.createCommandEffect("mingzhong", 2, true);
  75. break;
  76. case 3:
  77. this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttackSpeed = buff["attSpeed"]) / 10000;
  78. this.createCommandEffect("gongsu", 3, true);
  79. break;
  80. case 4:
  81. this.createCommandEffect("animation", 4, false, "spine/buff&die/tengman");
  82. this.getComponent(TakeAWalk) && (this.getComponent(TakeAWalk).dySpeed = ((10000 - buff["slow"]) / 10000));
  83. break;
  84. case 5:
  85. let hit = new HitData();
  86. hit.from = "闪电buff";
  87. hit.isSkill = true;
  88. hit.hit = Math.floor(buff["hit"] * (buff["flash"] / 10000));
  89. this.createFlashEffect("animation", "animation", hit);
  90. break;
  91. }
  92. DataSystem.getData(BuffData).curBuffs.indexOf(buff["id"]) == -1 && DataSystem.getData(BuffData).curBuffs.push(buff["id"]);
  93. }
  94. this.curBuff.set(buff["id"], { startTime: Date.now(), buff: buff });
  95. }
  96. /**buff失效 */
  97. private cancelBuff(buff): void {
  98. let types = (buff.buff["type"] + "").split(",");
  99. for (let i = 0; i < types.length; i++) {
  100. switch (parseInt(types[i])) {
  101. case 0:
  102. this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttack = 0);
  103. break;
  104. case 1:
  105. this.getComponent(Hero) && (this.getComponent(Hero).data._dyCri = 0);
  106. break;
  107. case 2:
  108. this.getComponent(Hero) && (this.getComponent(Hero).data._dyMiss = 0);
  109. break;
  110. case 3:
  111. this.getComponent(Hero) && (this.getComponent(Hero).data._dyAttackSpeed = 0);
  112. break;
  113. case 4:
  114. this.buffEffect.get(4).clean();
  115. this.getComponent(TakeAWalk) && (this.getComponent(TakeAWalk).dySpeed = 1);
  116. break;
  117. case 5:
  118. break;
  119. }
  120. let index = DataSystem.getData(BuffData).curBuffs.indexOf(buff.buff["id"]);
  121. if (index != -1) {
  122. DataSystem.getData(BuffData).curBuffs.splice(index, 1);
  123. }
  124. }
  125. }
  126. /**创建持续型spine特效 */
  127. private async createCommandEffect(animation: string, key?: number, autoDestory = false, path = "spine/buff&die/fight_buff") {
  128. let res = this.node.getComponent(ResourceLoader);
  129. if (res && this.layer) {
  130. let prefab = await res.load<Prefab>("prefabs/battle/commandSpine", Prefab);
  131. let node = instantiate(prefab);
  132. node.getComponent(CommandSpine).autoDestory = autoDestory;
  133. node.getComponent(CommandSpine).setData(path, false, animation);
  134. node.setParent(this.layer);
  135. if (this.buffEffect.has(key)) {
  136. this.buffEffect.get(key).clean();
  137. this.buffEffect.delete(key);
  138. }
  139. this.buffEffect.set(key, node.getComponent(CommandSpine));
  140. }
  141. }
  142. /**创建快速型特效 */
  143. private async createFlashEffect(animation: string, animation2: string, hit: HitData, path = "spine/buff&die/fight_skill_dian_1", path2 = "spine/buff&die/fight_skill_dian_2") {
  144. let res = this.node.getComponent(ResourceLoader);
  145. let self = this;
  146. if (res && this.layer) {
  147. let prefab = await res.load<Prefab>("prefabs/battle/commandSpine", Prefab);
  148. let node = instantiate(prefab);
  149. let component = node.getComponent(CommandSpine);
  150. component.setData(path, false, animation);
  151. component.autoDestory = true;
  152. component.completehandle = async () => {
  153. let prefab = await res.load<Prefab>("prefabs/battle/commandSpine", Prefab);
  154. let node = instantiate(prefab);
  155. let component = node.getComponent(CommandSpine);
  156. component.setData(path2, false, animation2);
  157. component.autoDestory = true;
  158. component.completehandle = async () => {
  159. self.getComponent(Monster) && this.getComponent(Monster).data._hit.push(hit);
  160. }
  161. }
  162. node.setParent(this.layer);
  163. }
  164. }
  165. }