Gun14.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { _decorator, Node, Vec3, director} from 'cc';
  2. import { PoolManager } from '../../core/manager/PoolManager';
  3. import { Game } from '../../game/Game';
  4. import { GunBase } from '../base/GunBase';
  5. import { Bullet14 } from './Bullet14';
  6. import { Utils } from '../../utils/Utils';
  7. import { Enemy } from '../../game/Enemy';
  8. import { Player } from '../../game/Player';
  9. import { audioMgr } from '../../core/manager/AudioManager';
  10. import { Constants } from '../../data/Constants';
  11. const { ccclass, property } = _decorator;
  12. //1006 坦克
  13. @ccclass('Gun14')
  14. export class Gun14 extends GunBase {
  15. @property({type: Node,tooltip:"子弹节点"})
  16. public bulletNode: Node = null!;
  17. @property({type: Node,tooltip:"开火特效节点"})
  18. fireEffect: Node = null;
  19. private isCb: boolean = false;
  20. //枪的拥有者
  21. public enemy: Enemy = null;
  22. //开枪结束回调
  23. public endCb: Function = null!;
  24. //换弹匣的回调
  25. public ammoCb: Function = null;
  26. //触发间隔时间统计
  27. private curTime: number = 0;
  28. //是否是第一次开火
  29. private isFristShot: boolean = false;
  30. //所有的烟雾
  31. public smokes: Array<Node> = []!
  32. //是否是第一次激活开枪
  33. public isFristShoot: boolean = false;
  34. //为了戳开子弹产生的时间间隔统计
  35. public diff: number = 0;
  36. //随机延迟的时间
  37. public r_delay: number = 0;
  38. //继续正常统计时间
  39. public isGo: boolean = true;
  40. onLoad() {
  41. this.bulletNode.active = false;
  42. this.muzzleNode.active = false;
  43. }
  44. /**
  45. * 设置枪的数据
  46. * @param data 枪的数据
  47. * @param endCB 结束回调
  48. * @param ammoCb 换弹匣回调
  49. */
  50. public init(data: any,enemy: any,endCb?: Function,ammoCb?: Function){
  51. this.data = data;
  52. this.curTime = 0;
  53. this.isFire = false;
  54. this.isFristShot = true;
  55. this.enemy = enemy;
  56. this.endCb = endCb;
  57. this.ammoCb = ammoCb;
  58. this.node.eulerAngles = new Vec3(0, 180, 0);
  59. }
  60. //开火
  61. public fire() {
  62. this.isFire = true;
  63. this.playParticle(this.fireEffect);
  64. }
  65. //结束开火
  66. public endFire(){
  67. this.isFire = false;
  68. this.endCb?.(this.data);
  69. this.recycle();
  70. }
  71. /**
  72. * 展示子弹
  73. */
  74. public createBullet() {
  75. if(!this.getNextAllow())return;
  76. let delay: number = 0.1;
  77. for(let i = 0; i < this.data.bullet_number; i++){
  78. this.scheduleOnce((k: number)=>{
  79. delay += Utils.getRandomFloat(0,0.2);
  80. if(!this.getNextAllow())return;
  81. //获取射击的精准度
  82. const isHit: boolean = this.enemy.calculateIsHit();
  83. //创建子弹
  84. this.getBulleNode(isHit);
  85. },delay * i);
  86. }
  87. }
  88. /**
  89. * 初始化一个子弹
  90. * @param isGuaranteedHit 子弹是否必中
  91. * @returns 返回一颗子弹
  92. */
  93. public getBulleNode(isHit: boolean = true): Bullet14{
  94. //从对象池获取子弹节点
  95. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  96. const bullet: Bullet14 = bulletNode.getComponent(Bullet14);
  97. bullet.init(this);
  98. bulletNode.setWorldPosition(this.muzzleNode.worldPosition);
  99. let playPos: Vec3 = Game.I.player.head.worldPosition.clone();
  100. const baseDirection = Vec3.subtract(new Vec3(), playPos,this.muzzleNode.worldPosition).normalize();
  101. //最终弹道方向
  102. let finalDirection: Vec3;
  103. if(isHit) {//100%必中(无偏移)
  104. finalDirection = baseDirection;
  105. }else{//非必中:在基准方向上添加小范围随机偏移
  106. const maxOffsetAngle = 5;
  107. const offsetX = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  108. const offsetY = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  109. //应用偏移(保持Z轴主要方向)
  110. finalDirection = new Vec3(
  111. baseDirection.x + offsetX,
  112. baseDirection.y + offsetY,
  113. baseDirection.z // 保持主要前进方向
  114. ).normalize();
  115. }
  116. //设置子弹方向
  117. bulletNode.forward = finalDirection;
  118. bullet.setBulletVector(finalDirection,isHit);
  119. return bullet;
  120. }
  121. //激活调用 持续开火
  122. protected update(dt: number): void {
  123. if(!this.getNextAllow())return;
  124. //每几秒攻击一次
  125. let m: number = this.data.atk_speed;
  126. //不是第一次激活 有延迟间隔
  127. if(!this.isFristShoot){
  128. m = this.isFristShoot ? 0 : m;
  129. if(this.diff >= this.r_delay && !this.isGo){
  130. this.diff += dt;
  131. this.isGo = true;
  132. return;
  133. }else{
  134. this.curTime += dt;
  135. }
  136. }else{//第一次激活 就直接开枪不延迟
  137. m = this.isFristShoot ? 0 : m;
  138. }
  139. //在随机延迟的时间间隔里 发射一颗子弹
  140. if(this.curTime >= m - this.r_delay) {
  141. if(this.isFire){
  142. this.isFristShoot = false;
  143. this.r_delay = Utils.getRandomFloat(0.4,0.8);
  144. this.diff = 0;
  145. this.isGo = false;
  146. audioMgr.playOneShot(Constants.audios.Tank_Attack,false);
  147. this.createBullet();
  148. this.curTime = 0;
  149. this.isCb = false;
  150. }else{
  151. if(!this.isFire && !this.isCb){
  152. this.isFire = false;
  153. this.endCb?.(this.data.type);
  154. this.isCb = true;
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * 是否可以允许操作
  161. */
  162. private getNextAllow(){
  163. let player: Player = Game.I.player;
  164. if(!player
  165. ||player.isDead
  166. ||!this.isFire
  167. ||Game.I.isPause
  168. ||Game.I.isGameOver){
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * 回收烟雾
  175. */
  176. public recycle(){
  177. this.smokes.forEach(e => {
  178. if(e && e.parent){
  179. PoolManager.putNode(e);
  180. }
  181. });
  182. this.smokes = [];
  183. }
  184. }