Gun3.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. onLoad() {
  34. this.bulletNode.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.isFristShot = true;
  48. this.holder = holder;
  49. this.endCb = endCb;
  50. this.ammoCb = ammoCb;
  51. }
  52. //开火
  53. public fire() {
  54. this.isFire = true;
  55. this.createBullet();
  56. this.playParticle(this.fireEffect);
  57. }
  58. //结束开火
  59. public endFire(){
  60. this.isFire = false;
  61. this.endCb?.(this.data);
  62. this.recycle();
  63. }
  64. /**
  65. * 直接开抢射杀敌人
  66. */
  67. public killEnemy(e: Enemy){
  68. if(e.isDead
  69. ||Game.I.isPause){
  70. return;
  71. }
  72. const destPos: Vec3 = e.node.worldPosition.clone();
  73. const bullet: Bullet3 = this.getBulleNode();
  74. bullet.init(this,true);
  75. tween(bullet.node)
  76. .to(0.3, {worldPosition:destPos})
  77. .call(() => {
  78. bullet.isDead = true;
  79. PoolManager.putNode(bullet.node);
  80. //直接打死 最大血量乘以2倍
  81. e.subHP(e.data.hp * 2,this.data);
  82. })
  83. .start();
  84. }
  85. /**
  86. * 展示子弹
  87. */
  88. public createBullet() {
  89. if(!this.holder
  90. ||this.holder.isDead
  91. ||!this.isFire
  92. ||Game.I.isPause
  93. ||Game.I.isGameOver){
  94. return;
  95. }
  96. audioMgr.playOneShot(this.data.gun_sound);
  97. this.getBulleNode();
  98. this.shotBullets ++;
  99. //换弹夹的操作
  100. if(this.shotBullets >= this.data.magazine){
  101. this.shotBullets = 0;
  102. this.ammoCb?.(this.data);
  103. }
  104. this.endFire();
  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. * 回收烟雾
  144. */
  145. public recycle(){
  146. this.smokes.forEach(e => {
  147. if(e && e.parent){
  148. PoolManager.putNode(e);
  149. }
  150. });
  151. this.smokes = [];
  152. }
  153. }