Bullet3.ts 5.1 KB

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