Gun14.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.unscheduleAllCallbacks();
  79. this.scheduleOnce((k: number)=>{
  80. delay += Utils.getRandomFloat(0,0.2);
  81. if(!this.getNextAllow())return;
  82. //获取射击的精准度
  83. const isHit: boolean = this.enemy.calculateIsHit();
  84. //创建子弹
  85. this.getBulleNode(isHit);
  86. },delay * i);
  87. }
  88. }
  89. /**
  90. * 初始化一个子弹
  91. * @param isGuaranteedHit 子弹是否必中
  92. * @returns 返回一颗子弹
  93. */
  94. public getBulleNode(isHit: boolean = true): Bullet14{
  95. //从对象池获取子弹节点
  96. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  97. const bullet: Bullet14 = bulletNode.getComponent(Bullet14);
  98. bullet.init(this);
  99. bulletNode.setWorldPosition(this.muzzleNode.worldPosition);
  100. let playPos: Vec3 = Game.I.player.head.worldPosition.clone();
  101. const baseDirection = Vec3.subtract(new Vec3(), playPos,this.muzzleNode.worldPosition).normalize();
  102. //最终弹道方向
  103. let finalDirection: Vec3;
  104. if(isHit) {//100%必中(无偏移)
  105. finalDirection = baseDirection;
  106. }else{//非必中:在基准方向上添加小范围随机偏移
  107. const maxOffsetAngle = 5;
  108. const offsetX = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  109. const offsetY = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  110. //应用偏移(保持Z轴主要方向)
  111. finalDirection = new Vec3(
  112. baseDirection.x + offsetX,
  113. baseDirection.y + offsetY,
  114. baseDirection.z // 保持主要前进方向
  115. ).normalize();
  116. }
  117. //设置子弹方向
  118. bulletNode.forward = finalDirection;
  119. bullet.setBulletVector(finalDirection,isHit);
  120. return bullet;
  121. }
  122. //激活调用 持续开火
  123. protected update(dt: number): void {
  124. if(!this.getNextAllow())return;
  125. //每几秒攻击一次
  126. let m: number = this.data.atk_speed;
  127. //不是第一次激活 有延迟间隔
  128. if(!this.isFristShoot){
  129. m = this.isFristShoot ? 0 : m;
  130. if(this.diff >= this.r_delay && !this.isGo){
  131. this.diff += dt;
  132. this.isGo = true;
  133. return;
  134. }else{
  135. this.curTime += dt;
  136. }
  137. }else{//第一次激活 就直接开枪不延迟
  138. m = this.isFristShoot ? 0 : m;
  139. }
  140. //在随机延迟的时间间隔里 发射一颗子弹
  141. if(this.curTime >= m - this.r_delay) {
  142. if(this.isFire){
  143. this.isFristShoot = false;
  144. this.r_delay = Utils.getRandomFloat(0.4,0.8);
  145. this.diff = 0;
  146. this.isGo = false;
  147. audioMgr.playOneShot(Constants.audios.Tank_Attack,false);
  148. this.createBullet();
  149. this.curTime = 0;
  150. this.isCb = false;
  151. }else{
  152. if(!this.isFire && !this.isCb){
  153. this.isFire = false;
  154. this.endCb?.(this.data.type);
  155. this.isCb = true;
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * 是否可以允许操作
  162. */
  163. private getNextAllow(){
  164. let player: Player = Game.I.player;
  165. if(!player
  166. ||player.isDead
  167. ||!this.isFire
  168. ||Game.I.isPause
  169. ||Game.I.isGameOver){
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * 回收烟雾
  176. */
  177. public recycle(){
  178. this.smokes.forEach(e => {
  179. if(e && e.parent){
  180. PoolManager.putNode(e);
  181. }
  182. });
  183. this.smokes = [];
  184. }
  185. }