Gun3.ts 5.4 KB

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