Bullet2.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult } from 'cc';
  2. import { PoolManager } from '../../core/manager/PoolManager';
  3. import { Game } from '../../game/Game';
  4. import { Enemy, EPartType } from '../../game/Enemy';
  5. import { Gun2 } from './Gun2';
  6. import { BulletBase } from '../base/BulletBase';
  7. import { GunBase } from '../base/GunBase';
  8. import { Sundries } from '../../game/Sundries';
  9. const { ccclass, property } = _decorator;
  10. //玩家所用子弹 步枪子弹
  11. @ccclass('Bullet2')
  12. export class Bullet2 extends BulletBase {
  13. //是否必死 必死的子弹不会回收 要自己控制
  14. private isRemoveDead: boolean = false;
  15. //射击方向的位置
  16. private vector: Vec3 = null;
  17. //计算子弹停留时间
  18. private time: number = 0.6;
  19. /**
  20. * 初始化一个子弹
  21. */
  22. /**
  23. * 初始化一个子弹
  24. * @param gun 属于哪把枪
  25. * @param isRemoveDead 是否不受控制的子弹自己管理
  26. */
  27. public init(gun:GunBase,isRemoveDead:boolean = true){
  28. this.gunBase = gun;
  29. this.isDead = false;
  30. this.isRemoveDead = isRemoveDead;
  31. }
  32. /**
  33. * 设置子弹开始位置和射击方向
  34. * @param v 射向的方向
  35. */
  36. public setBulletVector(v: Vec3){
  37. this.time = this.getBulletTime();
  38. const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
  39. this.vector = v.clone().multiplyScalar(bulletSpeed);
  40. this.node.forward = v;
  41. }
  42. /**
  43. * 射击速度发生改变
  44. */
  45. public speedChange() {
  46. const bulletSpeed = this.gunBase.data.bulletSpeed;
  47. if(this.vector) {
  48. this.vector = this.vector.normalize().multiplyScalar(bulletSpeed);
  49. }
  50. }
  51. /**
  52. * 实时帧更新
  53. */
  54. public update(deltaTime: number) {
  55. if(Game.I.isGameOver
  56. || Game.I.isPause) {
  57. this.isDead = true;
  58. PoolManager.putNode(this.node);
  59. return;
  60. }
  61. if(this.vector) {
  62. const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
  63. this.node.worldPosition = this.node.worldPosition.add(moveDelta);
  64. //开始检测射击
  65. this.scheduleOnce(this.rayPreCheck,this.time);
  66. }
  67. }
  68. /**
  69. * 检测射击
  70. * @param b
  71. * @param len 检测
  72. */
  73. private rayPreCheck() {
  74. let screenPos: Vec3 = Game.I.player.shootUI.getScreenCenterPos();
  75. const r = geometry.Ray.create();
  76. Game.I.camera.screenPointToRay(screenPos.x, screenPos.y, r);
  77. const hasHit = PhysicsSystem.instance.raycast(r, 0xffffffff, 100000, true);
  78. const raycastResults:PhysicsRayResult[] = PhysicsSystem.instance.raycastResults;
  79. if(hasHit && raycastResults.length > 0){
  80. //先查找 要检测的物体 对象上带args参数的就是优先检测的
  81. let targetResult: PhysicsRayResult | null = null;
  82. for (const result of raycastResults) {
  83. if (result.collider["args"]) {
  84. targetResult = result;
  85. break;
  86. }
  87. }
  88. targetResult = targetResult ?? raycastResults[0];
  89. this.addHitImpact(targetResult);
  90. const args:any = targetResult.collider["args"];
  91. if(args){
  92. let [type,cls] = args;
  93. let attack: number = this.gunBase.data.attack;
  94. if(cls instanceof Enemy){
  95. const e: Enemy = cls as Enemy;
  96. //爆头击杀
  97. let isHeadShot:boolean = false;
  98. if(type == EPartType.head){
  99. Game.I.player.headShotNum += 1;
  100. attack = Game.I.player.pData.headshotDmgMul * attack;
  101. isHeadShot = true;
  102. }
  103. //打到了盾兵上的盾
  104. if(type == EPartType.shield
  105. && e.shieldHp > 0){
  106. e.shieldHp -= attack;
  107. return;
  108. }
  109. e.isShotHead = isHeadShot;
  110. //坦克是单独的碰撞体
  111. if(e.isTank()){
  112. if(type == EPartType.tank){
  113. e.subHP(attack,this.gunBase.data,false);
  114. }
  115. }else{
  116. e.subHP(attack,this.gunBase.data,isHeadShot);
  117. }
  118. }else if(cls instanceof Sundries){
  119. const sundrie: Sundries = cls as Sundries;
  120. sundrie.subHP(attack,this.gunBase.data);
  121. }
  122. }
  123. //自动回收了子弹
  124. this.autoRecycle();
  125. }
  126. }
  127. }