Gun2.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 { Bullet2 } from './Bullet2';
  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. //玩家所使用的枪 步枪 可以按住镜头一直开枪(M416 AKM)
  14. @ccclass('Gun2')
  15. export class Gun2 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. public smokes: Array<Node> = []!
  32. onLoad() {
  33. this.bulletNode.active = false;
  34. this.impact.active = false;
  35. this.muzzleNode.active = false;
  36. }
  37. /**
  38. * 设置枪的数据
  39. * @param data 枪的数据
  40. * @param endCB 结束回调
  41. * @param ammoCb 换弹匣回调
  42. */
  43. public init(data: any,holder: any,endCb?: Function,ammoCb?: Function){
  44. this.data = data;
  45. this.shotBullets = 0;
  46. this.isFire = false;
  47. this.holder = holder;
  48. this.endCb = endCb;
  49. this.ammoCb = ammoCb;
  50. }
  51. //开火
  52. public fire() {
  53. this.isFire = true;
  54. this.curTime = 0.0;
  55. }
  56. //结束开火
  57. public endFire(){
  58. this.isFire = false;
  59. this.endCb?.(this.data);
  60. this.recycle();
  61. }
  62. /**
  63. * 直接开抢射杀敌人
  64. */
  65. public killEnemy(e: Enemy){
  66. if(e.isDead
  67. ||Game.I.isPause){
  68. return;
  69. }
  70. const destPos: Vec3 = e.node.worldPosition.clone();
  71. const bullet: Bullet2 = this.getBulleNode();
  72. bullet.init(this,true);
  73. tween(bullet.node)
  74. .to(0.3, {worldPosition:destPos})
  75. .call(() => {
  76. bullet.isDead = true;
  77. PoolManager.putNode(bullet.node);
  78. //直接打死 最大血量乘以2倍
  79. e.subHP(e.data.hp * 2,this.data);
  80. })
  81. .start();
  82. }
  83. /**
  84. * 展示子弹
  85. */
  86. public createBullet() {
  87. if(!this.holder
  88. ||this.holder.isDead
  89. ||!this.isFire
  90. ||Game.I.isPause
  91. ||Game.I.isGameOver){
  92. return;
  93. }
  94. audioMgr.playOneShot(this.data.gun_sound);
  95. this.playParticle(this.fireEffect);
  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. }
  104. /**
  105. * 初始化一个子弹
  106. * @param isGuaranteedHit 子弹是否必中(必中的时候targetNode不能为空)
  107. * @param targetNode 射击的目标对象
  108. * @returns 返回一颗子弹
  109. */
  110. public getBulleNode(isGuaranteedHit: boolean = true, targetNode: Node = null): Bullet2{
  111. //从对象池获取子弹节点
  112. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  113. const bullet: Bullet2 = bulletNode.getComponent(Bullet2);
  114. bullet.init(this);
  115. bulletNode.worldPosition = this.muzzleNode.worldPosition.clone();
  116. //计算基准方向(瞄准目标或准星位置)
  117. const shootUI:GunfightShootUI = uiMgr.getPageComponent(Constants.popUIs.gunfightShootUI);
  118. const targetPos = targetNode ? targetNode.worldPosition : shootUI.getCrossHairPos();
  119. const baseDirection = Vec3.subtract(new Vec3(), targetPos, this.muzzleNode.worldPosition).normalize();
  120. //最终弹道方向
  121. let finalDirection: Vec3;
  122. if(isGuaranteedHit) {//100%必中(无偏移)
  123. finalDirection = baseDirection;
  124. }else{//非必中:在基准方向上添加小范围随机偏移
  125. const maxOffsetAngle = 5;
  126. const offsetX = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  127. const offsetY = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  128. //应用偏移(保持Z轴主要方向)
  129. finalDirection = new Vec3(
  130. baseDirection.x + offsetX,
  131. baseDirection.y + offsetY,
  132. baseDirection.z // 保持主要前进方向
  133. ).normalize();
  134. }
  135. //设置子弹方向
  136. bulletNode.forward = finalDirection;
  137. bullet.setBulletVector(finalDirection);
  138. return bullet;
  139. }
  140. //激活调用 持续开火
  141. protected update(dt: number): void {
  142. let player: Player = Game.I.player;
  143. if(!player
  144. ||player.isDead
  145. ||!this.isFire
  146. ||Game.I.isPause
  147. ||Game.I.isGameOver){
  148. return;
  149. }
  150. if(Game.I.player.isReloadMagazine){
  151. return;
  152. }
  153. if(!Game.I.player.shootUI.isScopeOpen){
  154. this.endFire();
  155. return;
  156. }
  157. //每0.4秒攻击一次 发射一颗子弹
  158. this.curTime += dt;
  159. if(this.curTime >= 0.2) {
  160. //audioMgr.playOneShot(this.data.gun_sound);
  161. //播放射击帧动画
  162. this.createBullet();
  163. this.curTime = 0;
  164. }
  165. }
  166. /**
  167. * 回收烟雾
  168. */
  169. public recycle(){
  170. this.smokes.forEach(e => {
  171. if(e && e.parent){
  172. PoolManager.putNode(e);
  173. }
  174. });
  175. this.smokes = [];
  176. }
  177. }