Gun14.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. public impact: 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.impact.active = false;
  43. this.muzzleNode.active = false;
  44. }
  45. /**
  46. * 设置枪的数据
  47. * @param data 枪的数据
  48. * @param endCB 结束回调
  49. * @param ammoCb 换弹匣回调
  50. */
  51. public init(data: any,enemy: any,endCb?: Function,ammoCb?: Function){
  52. this.data = data;
  53. this.curTime = 0;
  54. this.isFire = false;
  55. this.isFristShot = true;
  56. this.enemy = enemy;
  57. this.endCb = endCb;
  58. this.ammoCb = ammoCb;
  59. this.node.eulerAngles = new Vec3(0, 180, 0);
  60. }
  61. //开火
  62. public fire() {
  63. this.isFire = true;
  64. this.playParticle(this.fireEffect);
  65. }
  66. //结束开火
  67. public endFire(){
  68. this.isFire = false;
  69. this.endCb?.(this.data);
  70. this.recycle();
  71. }
  72. /**
  73. * 展示子弹
  74. */
  75. public createBullet() {
  76. if(!this.getNextAllow())return;
  77. let delay: number = 0.1;
  78. for(let i = 0; i < this.data.bullet_number; i++){
  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 子弹是否必中(必中的时候targetNode不能为空)
  92. * @param targetNode 射击的目标对象
  93. * @returns 返回一颗子弹
  94. */
  95. public getBulleNode(isGuaranteedHit: boolean = true): Bullet14{
  96. //从对象池获取子弹节点
  97. const bulletNode: Node = PoolManager.getNode(this.bulletNode, director.getScene());
  98. const bullet: Bullet14 = bulletNode.getComponent(Bullet14);
  99. bullet.init(this);
  100. bulletNode.setWorldPosition(this.muzzleNode.worldPosition);
  101. let playPos: Vec3 = Game.I.player.head.worldPosition.clone();
  102. const baseDirection = Vec3.subtract(new Vec3(), playPos,this.muzzleNode.worldPosition).normalize();
  103. //最终弹道方向
  104. let finalDirection: Vec3;
  105. if(isGuaranteedHit) {//100%必中(无偏移)
  106. finalDirection = baseDirection;
  107. }else{//非必中:在基准方向上添加小范围随机偏移
  108. const maxOffsetAngle = 5;
  109. const offsetX = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  110. const offsetY = (Math.random() - 0.5) * maxOffsetAngle * Math.PI / 180;
  111. //应用偏移(保持Z轴主要方向)
  112. finalDirection = new Vec3(
  113. baseDirection.x + offsetX,
  114. baseDirection.y + offsetY,
  115. baseDirection.z // 保持主要前进方向
  116. ).normalize();
  117. }
  118. //设置子弹方向
  119. bulletNode.forward = finalDirection;
  120. bullet.setBulletVector(finalDirection);
  121. return bullet;
  122. }
  123. //激活调用 持续开火
  124. protected update(dt: number): void {
  125. if(!this.getNextAllow())return;
  126. //每几秒攻击一次
  127. let m: number = this.data.atk_speed;
  128. //不是第一次激活 有延迟间隔
  129. if(!this.isFristShoot){
  130. m = this.isFristShoot ? 0 : m;
  131. if(this.diff >= this.r_delay && !this.isGo){
  132. this.diff += dt;
  133. this.isGo = true;
  134. return;
  135. }else{
  136. this.curTime += dt;
  137. }
  138. }else{//第一次激活 就直接开枪不延迟
  139. m = this.isFristShoot ? 0 : m;
  140. }
  141. //在随机延迟的时间间隔里 发射一颗子弹
  142. if(this.curTime >= m - this.r_delay) {
  143. if(this.isFire){
  144. this.isFristShoot = false;
  145. this.r_delay = Utils.getRandomFloat(0,0.12);
  146. this.diff = 0;
  147. this.isGo = false;
  148. //audioMgr.playOneShot(this.data.gun_sound);
  149. //播放射击帧动画
  150. //this.playShootAnim();
  151. this.createBullet();
  152. this.curTime = 0;
  153. this.isCb = false;
  154. }else{
  155. if(!this.isFire && !this.isCb){
  156. this.isFire = false;
  157. this.endCb?.(this.data.type);
  158. this.isCb = true;
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * 是否可以允许操作
  165. */
  166. private getNextAllow(){
  167. let player: Player = Game.I.player;
  168. if(!player
  169. ||player.isDead
  170. ||!this.isFire
  171. ||Game.I.isPause
  172. ||Game.I.isGameOver){
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * 回收烟雾
  179. */
  180. public recycle(){
  181. this.smokes.forEach(e => {
  182. if(e && e.parent){
  183. PoolManager.putNode(e);
  184. }
  185. });
  186. this.smokes = [];
  187. }
  188. }