Gun3.ts 6.0 KB

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