Thorn.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import FF from "../../FF";
  2. import FSprite from "../../object/FSprite";
  3. import BaseEvent from "../base/BaseEvent";
  4. /**
  5. * 地刺
  6. */
  7. const { ccclass, property } = cc._decorator;
  8. interface ThornSprite {
  9. isAtk: boolean,
  10. sprite: FSprite
  11. }
  12. @ccclass
  13. export default class Thorn extends BaseEvent {
  14. @property({
  15. displayName: '数值(怪物id)'
  16. })
  17. public monsterId = 1001;
  18. @property({
  19. displayName: '地刺出现间隔时间'
  20. })
  21. public interval = 2;
  22. @property({
  23. type: sp.Skeleton,
  24. displayName: '地刺动画'
  25. })
  26. public spine: sp.Skeleton = null;
  27. private isAtk = false;
  28. private isPause = false;
  29. //当前处于碰撞区域的对象
  30. private spriteList: Array<ThornSprite> = [];
  31. start() {
  32. this.run();
  33. }
  34. update(dt) {
  35. if (!this.ff.lockCamera) {
  36. if (!this.ff.lockCamera != this.isPause) {
  37. this.isPause = true;
  38. this.spine.paused = true;
  39. return
  40. }
  41. } else {
  42. if (!this.ff.lockCamera != this.isPause) {
  43. this.isPause = false;
  44. this.spine.paused = false;
  45. }
  46. }
  47. if (this.isAtk) {
  48. this.spriteList.forEach(element => {
  49. this.rmHP(element);
  50. });
  51. }
  52. }
  53. private rmHP(thornSprite: ThornSprite) {
  54. if (thornSprite.isAtk) {
  55. return;
  56. }
  57. if (thornSprite.sprite.hp <= 0) {
  58. return;
  59. }
  60. thornSprite.isAtk = true;
  61. let main = this.ff.main;
  62. let attrData = main.sManage.getMonsterData(this.monsterId);
  63. thornSprite.sprite.bAtkjs(attrData);
  64. }
  65. private run() {
  66. this.spriteList.forEach(element => {
  67. element.isAtk = false;
  68. });
  69. cc.tween(this).sequence(
  70. cc.delayTime(this.interval),
  71. cc.callFunc(() => {
  72. this.playAtk();
  73. })
  74. ).start()
  75. }
  76. private playAtk() {
  77. this.isAtk = true;
  78. this.spine.setAnimation(0, 'atk', false);
  79. this.spine.setCompleteListener(() => {
  80. this.isAtk = false;
  81. this.spine.setCompleteListener(null);
  82. this.spine.setAnimation(0, 'idle', true);
  83. this.run()
  84. });
  85. }
  86. onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  87. if (other.node.group == 'A') {
  88. let obj = other.node.getComponent(FSprite);
  89. this.ff = obj.ff;
  90. this.pushSprite(obj);
  91. }
  92. }
  93. onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  94. if (other.node.group == 'A') {
  95. let obj = other.node.getComponent(FSprite);
  96. this.removeSprite(obj);
  97. }
  98. }
  99. private pushSprite(sprite: FSprite) {
  100. for (let i = 0; i < this.spriteList.length; i++) {
  101. const element = this.spriteList[i];
  102. if (element.sprite == sprite) {
  103. return
  104. }
  105. }
  106. this.spriteList.push({
  107. isAtk: false,
  108. sprite: sprite
  109. })
  110. }
  111. private removeSprite(sprite: FSprite) {
  112. for (let i = 0; i < this.spriteList.length; i++) {
  113. const element = this.spriteList[i];
  114. if (element.sprite == sprite) {
  115. this.spriteList.splice(i, 1);
  116. break
  117. }
  118. }
  119. }
  120. }