Gun10.ts 6.5 KB

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