Gun3.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. 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. }
  107. /**
  108. * 初始化一个子弹
  109. * @param isGuaranteedHit 子弹是否必中(必中的时候targetNode不能为空)
  110. * @param targetNode 射击的目标对象
  111. * @returns 返回一颗子弹
  112. */
  113. public getBulleNode(isGuaranteedHit: boolean = true, targetNode: Node = null): Bullet3{
  114. //从对象池获取子弹节点
  115. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  116. const bullet: Bullet3 = bulletNode.getComponent(Bullet3);
  117. bullet.init(this);
  118. bulletNode.worldPosition = this.muzzleNode.worldPosition.clone();
  119. //计算基准方向(瞄准目标或准星位置)
  120. const shootUI:GunfightShootUI = uiMgr.getPageComponent(Constants.popUIs.gunfightShootUI);
  121. const targetPos = targetNode ? targetNode.worldPosition : shootUI.getCrossHairPos();
  122. const baseDirection = Vec3.subtract(new Vec3(), targetPos, this.muzzleNode.worldPosition).normalize();
  123. //最终弹道方向
  124. let finalDirection: Vec3;
  125. if(isGuaranteedHit) {//100%必中(无偏移)
  126. finalDirection = baseDirection;
  127. }else{//非必中:在基准方向上添加小范围随机偏移
  128. const maxOffsetAngle = 5;
  129. const offsetX = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  130. const offsetY = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  131. //应用偏移(保持Z轴主要方向)
  132. finalDirection = new Vec3(
  133. baseDirection.x + offsetX,
  134. baseDirection.y + offsetY,
  135. baseDirection.z // 保持主要前进方向
  136. ).normalize();
  137. }
  138. //设置子弹方向
  139. bulletNode.forward = finalDirection;
  140. bullet.setBulletVector(finalDirection);
  141. return bullet;
  142. }
  143. //激活调用 持续开火
  144. protected update(dt: number): void {
  145. let player: Player = Game.I.player;
  146. if(!player
  147. ||player.isDead
  148. ||!this.isFire
  149. ||Game.I.isPause
  150. ||Game.I.isGameOver){
  151. return;
  152. }
  153. if(Game.I.player.isReloadMagazine){
  154. return;
  155. }
  156. //每0.4秒攻击一次 发射一颗子弹
  157. this.curTime += dt;
  158. if(this.curTime >= 0.22) {
  159. this.burstFireCount --;
  160. this.createBullet();
  161. this.curTime = 0;
  162. if(this.burstFireCount <= 0){
  163. this.burstFireCount = 3;
  164. this.endFire();
  165. this.scheduleOnce(()=>{
  166. Game.I.player.shootUI.isScopeOpen = false;
  167. },0.5)
  168. }
  169. }
  170. }
  171. /**
  172. * 回收烟雾
  173. */
  174. public recycle(){
  175. this.smokes.forEach(e => {
  176. if(e && e.parent){
  177. PoolManager.putNode(e);
  178. }
  179. });
  180. this.smokes = [];
  181. }
  182. }