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