Bullet3.ts 4.9 KB

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