Bullet.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { _decorator, Animation, AnimationClip, BoxCollider2D, Collider2D, Component, Contact2DType, ICollisionEvent, IPhysics2DContact, Node, PhysicsGroup, PhysicsSystem2D, SpriteFrame, UITransform, Vec2, Vec3 } from 'cc';
  2. import { Tools } from '../../Tools/Tools';
  3. import { LifeBar } from './LifeBar';
  4. import { Role, RoleState } from '../Role';
  5. const { ccclass, property } = _decorator;
  6. /*
  7. @ccclass('Bullet')
  8. export class Bullet extends Component {
  9. explodeframes: SpriteFrame[] = [];
  10. bulletFrames: SpriteFrame[] = [];
  11. explodeAni: Animation = null;
  12. bulletAni: Animation = null;
  13. //是否爆炸 true -> 爆炸
  14. isExplode: boolean = false;
  15. //子弹速度
  16. bulletSpeed: number = 100;
  17. //子弹方向
  18. direction: number = 1;
  19. //攻击力
  20. atk: number = null;
  21. //被攻击的节点
  22. targetNode: Node = null;
  23. //是否碰撞
  24. private _hasCollided: boolean = false;
  25. //private _initPos: Vec3 = null;
  26. protected onLoad(): void {
  27. const collider = this.getComponent(Collider2D);
  28. if (collider) {
  29. //collider.on(Contact2DType.BEGIN_CONTACT, this._onBeginContact, this)
  30. }
  31. }
  32. start() {
  33. this.explodeAni = Tools.createAnimation(this.explodeframes, 5, this.node, AnimationClip.WrapMode.Normal);
  34. this.bulletAni = Tools.createAnimation(this.bulletFrames, 5, this.node, AnimationClip.WrapMode.Loop);
  35. //this.node.setPosition(0, 0, 0);
  36. //this._initPos = this.node.position;
  37. this.setState(false);
  38. }
  39. setState(isExplode: boolean) {
  40. if (isExplode) {
  41. this.bulletSpeed = 0;
  42. this.explodeAni.play();
  43. this.explodeAni.once(Animation.EventType.FINISHED, () => {
  44. this.node.destroy()
  45. },
  46. this.node)
  47. } else {
  48. this.bulletAni.play();
  49. }
  50. }
  51. _onBeginContact(otherCollider: BoxCollider2D) {
  52. const lifeBar = this.targetNode.getComponent(LifeBar);
  53. lifeBar.updateProgressBar(lifeBar._curHp - this.atk);
  54. this.setState(true);
  55. }
  56. update(deltaTime: number) {
  57. this.move(deltaTime);
  58. this.onBulletCollision(this.targetNode);
  59. //this._checkDistance();
  60. }
  61. // private _checkDistance(){
  62. // const currentPos: Vec3 = this.node.position;
  63. // const distance = currentPos.subtract(this._initPos).length();
  64. // if(distance >= 180){
  65. // this.node.destroy;
  66. // }
  67. // }
  68. move(dt: number) {
  69. let x = this.node.getWorldPosition().x;
  70. let y = this.node.getWorldPosition().y;
  71. let z = this.node.getWorldPosition().z;
  72. // if (this.direction) {
  73. // x = x + this.bulletSpeed * dt;
  74. // } else {
  75. // x = x - this.bulletSpeed * dt;
  76. // }
  77. x = x + this.bulletSpeed * this.direction * dt;
  78. this.node.setWorldPosition(x, y, z)
  79. }
  80. // 处理子弹碰撞
  81. onBulletCollision(targetNode: Node) {
  82. if(this._hasCollided) return;
  83. if(!targetNode.isValid) return;
  84. const boxBullet = this.node.getComponent(UITransform).getBoundingBoxToWorld();
  85. const boxTarget = targetNode.getComponent(UITransform).getBoundingBoxToWorld();
  86. if (boxTarget.containsRect(boxBullet)) {
  87. const targetLifeBar = targetNode.getComponent(LifeBar);
  88. if((targetLifeBar._curHp - this.atk) <= 0){
  89. this.targetNode.destroy();
  90. this.node.destroy();
  91. }
  92. targetLifeBar.updateProgressBar(targetLifeBar._curHp - this.atk);
  93. this._hasCollided = true;
  94. this.setState(true);
  95. }
  96. }
  97. }
  98. */
  99. @ccclass('Bullet')
  100. export class Bullet extends Component {
  101. explodeframes: SpriteFrame[] = [];
  102. bulletFrames: SpriteFrame[] = [];
  103. explodeAni: Animation = null;
  104. bulletAni: Animation = null;
  105. //是否爆炸 true -> 爆炸
  106. isExplode: boolean = false;
  107. //子弹速度
  108. bulletSpeed: number = 100;
  109. //子弹方向
  110. direction: number = 1;
  111. //攻击力
  112. atk: number = null;
  113. //被攻击的节点
  114. targetNode: Node = null;
  115. //是否碰撞
  116. private _hasCollided: boolean = false;
  117. private _bulletColl: BoxCollider2D = null;
  118. private _initPos: Vec3 = null;
  119. protected onLoad(): void {
  120. this._bulletColl = this.getComponent(BoxCollider2D);
  121. }
  122. start() {
  123. //this._bulletColl.on("onCollisionEnter",this._onCollStart,this);
  124. this.explodeAni = Tools.createAnimation(this.explodeframes, 20, this.node, AnimationClip.WrapMode.Normal);
  125. this.bulletAni = Tools.createAnimation(this.bulletFrames, 5, this.node, AnimationClip.WrapMode.Loop);
  126. this._initPos = this.node.position;
  127. this.setState(false);
  128. }
  129. setState(isExplode: boolean) {
  130. if (isExplode) {
  131. this.bulletSpeed = 0;
  132. this.bulletAni.stop();
  133. this.explodeAni.play();
  134. this.explodeAni.once(Animation.EventType.FINISHED, () => {
  135. this.node.destroy()
  136. },
  137. this)
  138. } else {
  139. this.bulletAni.play();
  140. }
  141. }
  142. update(deltaTime: number) {
  143. this.move(deltaTime);
  144. this.onBulletCollision(this.targetNode);
  145. this._checkDistance();
  146. }
  147. move(dt: number) {
  148. let x = this.node.getWorldPosition().x;
  149. let y = this.node.getWorldPosition().y;
  150. let z = this.node.getWorldPosition().z;
  151. x = x + this.bulletSpeed * this.direction * dt;
  152. this.node.setWorldPosition(x, y, z)
  153. }
  154. private _onCollStart(event: ICollisionEvent){
  155. if(event.otherCollider.node.uuid === this.targetNode.uuid){
  156. }
  157. }
  158. // 处理子弹碰撞
  159. private onBulletCollision(targetNode: Node) {
  160. if (this._hasCollided) return;
  161. if (!targetNode.isValid) return;
  162. const boxBullet = this.node.getComponent(UITransform).getBoundingBoxToWorld();
  163. const boxTarget = targetNode.getComponent(UITransform).getBoundingBoxToWorld();
  164. if (boxTarget.intersects(boxBullet)) {
  165. const targetLifeBar = targetNode.getComponent(LifeBar);
  166. const curHp: number = targetLifeBar._curHp - this.atk;
  167. targetLifeBar.updateProgressBar(curHp);
  168. if (curHp <= 0) {
  169. //this.targetNode.destroy();
  170. this.node.destroy();
  171. }
  172. this._hasCollided = true;
  173. this.setState(true);
  174. }
  175. }
  176. private _checkDistance() {
  177. const currentPos: Vec3 = this.node.position;
  178. const distance: number = currentPos.subtract(this._initPos).length();
  179. if (distance >= 180) {
  180. this.node.destroy;
  181. }
  182. }
  183. }