Gun3.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. Game.I.player.mainCamera.shake(0.2,0,0.1);
  91. if(!this.holder
  92. ||this.holder.isDead
  93. ||!this.isFire
  94. ||Game.I.isPause
  95. ||Game.I.isGameOver){
  96. return;
  97. }
  98. Game.I.player.shootUI.crossHairStability();
  99. audioMgr.playOneShot(this.data.gun_sound);
  100. this.getBulleNode();
  101. this.shotBullets ++;
  102. //换弹夹的操作
  103. if(this.shotBullets >= this.data.magazine){
  104. this.shotBullets = 0;
  105. this.ammoCb?.(this.data);
  106. }
  107. }
  108. /**
  109. * 初始化一个子弹
  110. * @param isGuaranteedHit 子弹是否必中(必中的时候targetNode不能为空)
  111. * @param targetNode 射击的目标对象
  112. * @returns 返回一颗子弹
  113. */
  114. public getBulleNode(isGuaranteedHit: boolean = true, targetNode: Node = null): Bullet3{
  115. //从对象池获取子弹节点
  116. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  117. const bullet: Bullet3 = bulletNode.getComponent(Bullet3);
  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. protected update(dt: number): void {
  146. let player: Player = Game.I.player;
  147. if(!player
  148. ||player.isDead
  149. ||!this.isFire
  150. ||Game.I.isPause
  151. ||Game.I.isGameOver){
  152. return;
  153. }
  154. if(Game.I.player.isReloadMagazine){
  155. return;
  156. }
  157. //每0.4秒攻击一次 发射一颗子弹
  158. this.curTime += dt;
  159. if(this.curTime >= 0.22) {
  160. this.burstFireCount --;
  161. this.createBullet();
  162. this.curTime = 0;
  163. if(this.burstFireCount <= 0){
  164. this.burstFireCount = 3;
  165. this.endFire();
  166. this.scheduleOnce(()=>{
  167. Game.I.player.shootUI.isScopeOpen = false;
  168. },0.5)
  169. }
  170. }
  171. }
  172. /**
  173. * 回收烟雾
  174. */
  175. public recycle(){
  176. this.smokes.forEach(e => {
  177. if(e && e.parent){
  178. PoolManager.putNode(e);
  179. }
  180. });
  181. this.smokes = [];
  182. }
  183. }