Bullet3.ts 5.0 KB

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