Gun2.ts 5.8 KB

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