Gun1.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { _decorator, Node, Vec3, tween, director} from 'cc';
  2. import { PoolManager } from '../../core/manager/PoolManager';
  3. import { Game } from '../../game/Game';
  4. import { uiMgr } from '../../core/manager/UIManager';
  5. import { Constants } from '../../data/Constants';
  6. import { GunfightShootUI } from '../../ui/GunfightShootUI';
  7. import { Bullet1 } from './Bullet1';
  8. import { GunBase } from '../base/GunBase';
  9. import { Enemy } from '../../game/Enemy';
  10. import { Player } from '../../game/Player';
  11. import { audioMgr } from '../../core/manager/AudioManager';
  12. const { ccclass, property } = _decorator;
  13. //玩家所使用的枪 开一枪然后回缩镜头 (98k m24 awm BarrettM82A1)
  14. @ccclass('Gun1')
  15. export class Gun1 extends GunBase {
  16. @property({type: Node,tooltip:"子弹节点"})
  17. public bulletNode: Node = null!;
  18. @property({type: Node,tooltip:"烟雾特效"})
  19. public impact: Node = null!
  20. @property({type: Node,tooltip:"开火特效节点"})
  21. fireEffect: Node = null;
  22. //枪的拥有者
  23. public holder: Player|Enemy = null;
  24. //开枪结束回调
  25. public endCb: Function = null!;
  26. //换弹匣的回调
  27. public ammoCb: Function = null;
  28. //触发间隔时间统计
  29. private curTime: number = 0;
  30. //是否是第一次开火
  31. private isFristShot: boolean = false;
  32. //所有的烟雾
  33. public smokes: Array<Node> = []!
  34. onLoad() {
  35. this.bulletNode.active = false;
  36. this.impact.active = false;
  37. this.muzzleNode.active = false;
  38. }
  39. /**
  40. * 设置枪的数据
  41. * @param data 枪的数据
  42. * @param endCB 结束回调
  43. * @param ammoCb 换弹匣回调
  44. */
  45. public init(data: any,holder: any,endCb?: Function,ammoCb?: Function){
  46. this.data = data;
  47. this.shotBullets = 0;
  48. this.isFire = false;
  49. this.isFristShot = true;
  50. this.holder = holder;
  51. this.endCb = endCb;
  52. this.ammoCb = ammoCb;
  53. }
  54. //开火
  55. public fire() {
  56. this.isFire = true;
  57. this.createBullet();
  58. this.playParticle(this.fireEffect);
  59. }
  60. //结束开火
  61. public endFire(){
  62. this.isFire = false;
  63. this.endCb?.(this.data);
  64. this.recycle();
  65. }
  66. /**
  67. * 直接开抢射杀敌人
  68. */
  69. public killEnemy(e: Enemy){
  70. if(e.isDead
  71. ||Game.I.isPause){
  72. return;
  73. }
  74. const destPos: Vec3 = e.node.worldPosition.clone();
  75. const bullet: Bullet1 = this.getBulleNode();
  76. bullet.init(this,true);
  77. tween(bullet.node)
  78. .to(0.3, {worldPosition:destPos})
  79. .call(() => {
  80. bullet.isDead = true;
  81. PoolManager.putNode(bullet.node);
  82. //直接打死 最大血量乘以2倍
  83. e.subHP(e.data.hp * 2,this.data);
  84. })
  85. .start();
  86. }
  87. /**
  88. * 展示子弹
  89. */
  90. public createBullet() {
  91. if(!this.holder
  92. ||this.holder.isDead
  93. ||!this.isFire
  94. ||Game.I.isPause
  95. ||Game.I.isGameOver){
  96. return;
  97. }
  98. audioMgr.playOneShot(this.data.gun_sound);
  99. this.getBulleNode();
  100. this.shotBullets ++;
  101. //换弹夹的操作
  102. if(this.shotBullets >= this.data.magazine){
  103. this.shotBullets = 0;
  104. this.ammoCb?.(this.data);
  105. }
  106. this.endFire();
  107. }
  108. /**
  109. * 初始化一个子弹
  110. * @param isGuaranteedHit 子弹是否必中(必中的时候targetNode不能为空)
  111. * @param targetNode 射击的目标对象
  112. * @returns 返回一颗子弹
  113. */
  114. public getBulleNode(isGuaranteedHit: boolean = true, targetNode: Node = null): Bullet1{
  115. //从对象池获取子弹节点
  116. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  117. const bullet: Bullet1 = bulletNode.getComponent(Bullet1);
  118. bullet.init(this);
  119. bulletNode.worldPosition = this.muzzleNode.worldPosition.clone();
  120. //计算基准方向(瞄准目标或准星位置)
  121. const shootUI:GunfightShootUI = uiMgr.getPageComponent(Constants.popUIs.gunfightShootUI);
  122. const targetPos = targetNode ? targetNode.worldPosition : shootUI.getCrossHairPos();
  123. const baseDirection = Vec3.subtract(new Vec3(), targetPos, this.muzzleNode.worldPosition).normalize();
  124. //最终弹道方向
  125. let finalDirection: Vec3;
  126. if(isGuaranteedHit) {//100%必中(无偏移)
  127. finalDirection = baseDirection;
  128. }else{//非必中:在基准方向上添加小范围随机偏移
  129. const maxOffsetAngle = 5;
  130. const offsetX = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  131. const offsetY = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  132. //应用偏移(保持Z轴主要方向)
  133. finalDirection = new Vec3(
  134. baseDirection.x + offsetX,
  135. baseDirection.y + offsetY,
  136. baseDirection.z // 保持主要前进方向
  137. ).normalize();
  138. }
  139. //设置子弹方向
  140. bulletNode.forward = finalDirection;
  141. bullet.setBulletVector(finalDirection);
  142. return bullet;
  143. }
  144. /**
  145. * 回收烟雾
  146. */
  147. public recycle(){
  148. this.smokes.forEach(e => {
  149. if(e && e.parent){
  150. PoolManager.putNode(e);
  151. }
  152. });
  153. this.smokes = [];
  154. }
  155. }