Gun10.ts 6.4 KB

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