Gun1.ts 5.2 KB

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