FBomb.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import FF from "../../FF";
  2. import FSprite from "../../object/FSprite";
  3. const { ccclass, property } = cc._decorator;
  4. enum BoomStatus {
  5. safe, //安全阶段
  6. warning, //警告阶段
  7. boom, //爆炸阶段
  8. end, //爆炸结束
  9. }
  10. enum RoleArea {
  11. safe, // 安全
  12. hit, // 受伤
  13. }
  14. /**
  15. * 定时炸弹
  16. */
  17. @ccclass
  18. export default class FBomb extends cc.Component {
  19. @property({
  20. displayName: "怪物ID"
  21. })
  22. monsterId: number = 0;
  23. @property({
  24. displayName: '爆炸动画',
  25. type: sp.Skeleton
  26. })
  27. spine: sp.Skeleton = null;
  28. @property({
  29. displayName: "警告区域",
  30. })
  31. warningArea = new cc.Vec2();
  32. @property({
  33. displayName: "爆炸范围",
  34. })
  35. boomArea = new cc.Vec2();
  36. @property({
  37. displayName: "警告时间(秒)"
  38. })
  39. warningTime: number = 0;
  40. @property({
  41. displayName: "爆炸时间(秒)"
  42. })
  43. boomTime: number = 0;
  44. ff: FF;
  45. public spriteList: Array<FSprite> = [];
  46. boomStatus: BoomStatus = BoomStatus.safe;
  47. // roleArea: RoleArea = RoleArea.safe;
  48. tempTime: number = 0;
  49. isHit: boolean = false;
  50. isWarning: boolean = false;
  51. start() {
  52. let boxs = this.node.getComponents(cc.PhysicsBoxCollider)
  53. boxs[0].size = new cc.Size(this.warningArea.x, this.warningArea.y);
  54. boxs[1].size = new cc.Size(this.boomArea.x, this.boomArea.y);
  55. }
  56. // public onBegin(tag: number) {
  57. // // this.roleArea = tag == 1 ? RoleArea.hit : RoleArea.safe;
  58. // if (tag == 0) {
  59. // if (!this.isWarning) {
  60. // this.warning();
  61. // }
  62. // }
  63. // }
  64. // public onEnd(tag: number) {
  65. // if (tag == 1) {
  66. // // this.roleArea = RoleArea.safe;
  67. // }
  68. // }
  69. onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  70. if (other.node.group == 'A' && self.tag == 1) {
  71. let obj = other.node.getComponent(FSprite);
  72. if (this.spriteList.indexOf(obj) == -1) {
  73. this.spriteList.push(obj);
  74. }
  75. }
  76. if (other.node.group == 'A' && other.tag == 1) {
  77. let obj = other.node.getComponent(FSprite);
  78. this.ff = obj.ff;
  79. if (self.tag == 0) {
  80. if (!this.isWarning) {
  81. this.warning();
  82. }
  83. }
  84. }
  85. }
  86. onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  87. if (other.node.group == 'A' && self.tag == 1) {
  88. let obj = other.node.getComponent(FSprite);
  89. this.removeSprite(obj);
  90. }
  91. }
  92. private removeSprite(sprite: FSprite) {
  93. for (let i = 0; i < this.spriteList.length; i++) {
  94. const element = this.spriteList[i];
  95. if (element == sprite) {
  96. this.spriteList.splice(i, 1);
  97. break
  98. }
  99. }
  100. }
  101. // 警告
  102. warning() {
  103. this.isWarning = true;
  104. this.spine.setAnimation(0, "boom", true);
  105. this.boomStatus = BoomStatus.warning;
  106. let tempTime = 0;
  107. let call = () => {
  108. tempTime += 0.1;
  109. if (tempTime >= this.warningTime) {
  110. // console.log("===警告结束===");
  111. this.boom();
  112. this.unschedule(call);
  113. }
  114. }
  115. this.schedule(call, 0.1);
  116. }
  117. //爆炸
  118. boom() {
  119. this.spine.setAnimation(0, "boom2", false);
  120. this.boomStatus = BoomStatus.boom;
  121. let tempTime = 0;
  122. let call = () => {
  123. tempTime += 0.1;
  124. if (tempTime >= this.boomTime) {
  125. this.end();
  126. this.unschedule(call);
  127. } else {
  128. if (!this.isHit) {
  129. this.hit();
  130. }
  131. // if (this.roleArea == RoleArea.hit && !this.isHit) {
  132. // this.hit();
  133. // }
  134. }
  135. }
  136. this.schedule(call, 0.1);
  137. }
  138. hit() {
  139. this.isHit = true;
  140. this.spriteList.forEach(sprite => {
  141. let main = this.ff.main;
  142. let attrData = main.sManage.getMonsterData(this.monsterId);
  143. sprite.bAtkjs(attrData)
  144. })
  145. }
  146. //爆炸结束
  147. end() {
  148. this.boomStatus = BoomStatus.end;
  149. this.node.active = false;
  150. }
  151. }