Gun10.ts 6.3 KB

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