Bullet.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { _decorator, Animation, AnimationClip, BoxCollider2D, Collider2D, Component, Contact2DType, Node, PhysicsSystem2D, SpriteFrame, UITransform } from 'cc';
  2. import { Tools } from '../../Tools/Tools';
  3. import { Role } from '../Role';
  4. import { LifeBar } from './LifeBar';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('Bullet')
  7. export class Bullet extends Component {
  8. explodeframes: SpriteFrame[] = [];
  9. bulletFrames: SpriteFrame[] = [];
  10. explodeAni: Animation = null;
  11. bulletAni: Animation = null;
  12. //是否爆炸 true -> 爆炸
  13. isExplode: boolean = false;
  14. //子弹速度
  15. bulletSpeed: number = 100;
  16. direction: number = 1;
  17. atkLength: number = null;
  18. atk: number = null;
  19. targetNode: Node = null;
  20. otherCollider: BoxCollider2D = null;
  21. //role: Role = null;
  22. protected onLoad(): void {
  23. const collider = this.getComponent(BoxCollider2D);
  24. if (collider) {
  25. collider.on(Contact2DType.BEGIN_CONTACT, this._onBeginContact, this)
  26. }
  27. //this.otherCollider = this.targetNode.getComponent(BoxCollider2D);
  28. //this.role = this.node.parent.getComponent(Role);
  29. }
  30. start() {
  31. this.explodeAni = Tools.createAnimation(this.explodeframes, 5, this.node, AnimationClip.WrapMode.Normal);
  32. this.bulletAni = Tools.createAnimation(this.bulletFrames, 5, this.node, AnimationClip.WrapMode.Loop);
  33. this.node.setPosition(0, 0, 0);
  34. this.setState(false);
  35. }
  36. setState(isExplode: boolean) {
  37. if (isExplode) {
  38. this.explodeAni.play();
  39. // this.explodeAni.once(Animation.EventType.FINISHED, () => {
  40. // if(this.node.isValid){
  41. // }
  42. // this.node.destroy()
  43. // },
  44. // this.node)
  45. } else {
  46. this.bulletAni.play();
  47. }
  48. }
  49. _onBeginContact(selfCollider: BoxCollider2D, otherCollider: BoxCollider2D) {
  50. //selfCollider = this.node.getComponent(BoxCollider2D);
  51. otherCollider = this.otherCollider;
  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.stop(this.atkLength);
  60. }
  61. move(dt: number) {
  62. let x = this.node.getWorldPosition().x;
  63. let y = this.node.getWorldPosition().y;
  64. let z = this.node.getWorldPosition().z;
  65. if (this.direction) {
  66. x = x + this.bulletSpeed * dt;
  67. } else {
  68. x = x - this.bulletSpeed * dt;
  69. }
  70. this.node.setWorldPosition(x, y, z)
  71. }
  72. stop(atkLength: number) {
  73. if (this.direction) {
  74. if (this.node.position.x >= atkLength) {
  75. this.setState(true);
  76. this.node.destroy();
  77. }
  78. } else {
  79. if ((this.node.position.x + atkLength) <= 0) {
  80. console.log(this.node.position.x + atkLength)
  81. this.setState(true);
  82. this.node.destroy();
  83. }
  84. }
  85. }
  86. // 处理子弹碰撞
  87. onBulletCollision(targetNode: Node) {
  88. const boxBullet = this.node.getComponent(UITransform).getBoundingBoxToWorld();
  89. const boxTarget = targetNode.getComponent(UITransform).getBoundingBoxToWorld();
  90. if (boxTarget.containsRect(boxBullet)) {
  91. const targetLifeBar = targetNode.getComponent(LifeBar);
  92. targetLifeBar.updateProgressBar(targetLifeBar._totalHp - this.atk);
  93. this.setState(true);
  94. this.node.destroy();
  95. }
  96. }
  97. //targetNode为目标节点,子弹需要攻击的对象
  98. //如果子弹与targetNode碰撞,targetNode的hp(血量)将会减少子弹发射者的atk(攻击力);
  99. //同时子弹播放爆炸动画,播放结束后,子弹销毁
  100. }