Bullet3.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { Gun3 } from './Gun3';
  6. import { BulletBase } from '../base/BulletBase';
  7. import { GunBase } from '../base/GunBase';
  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. * 初始化一个子弹
  18. */
  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. const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
  35. this.vector = v.clone().multiplyScalar(bulletSpeed);
  36. this.node.forward = v;
  37. this.rayPreCheck(bulletSpeed / 60);
  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. this.rayPreCheck(moveDelta.length());
  62. }
  63. }
  64. /**
  65. * 检测射击
  66. * @param b
  67. * @param len 检测
  68. */
  69. private rayPreCheck(len: number) {
  70. const p = this.node.worldPosition;
  71. const ray = geometry.Ray.create(p.x, p.y, p.z, this.vector.x, this.vector.y, this.vector.z);
  72. const phy:PhysicsSystem = PhysicsSystem.instance;
  73. const isHit = phy.raycast(ray, 0xffffff, len);
  74. if (isHit && phy.raycastResults.length > 0) {
  75. let result:PhysicsRayResult = phy.raycastResults[0];
  76. this.addImpact(result);
  77. const enemyNode: Node = this.findEnemyNode(result.collider.node);
  78. if(enemyNode){
  79. const name: string = result.collider.node.name;
  80. let e: Enemy = enemyNode.getComponent(Enemy);
  81. let attack: number = this.gunBase.data.attack;
  82. //爆头击杀
  83. let isHeadShot:boolean = false;
  84. if(name == EPartType.head){
  85. Game.I.player.headShotNum += 1;
  86. attack = Game.I.player.pData.headshotDmgMul * attack;
  87. isHeadShot = true;
  88. }
  89. //打到了盾兵上的盾
  90. if(name == EPartType.shield && e.shieldHp > 0){
  91. e.shieldHp -= attack;
  92. return;
  93. }
  94. //坦克是单独的碰撞体
  95. if(e.isTank()){
  96. if(name == EPartType.tank){
  97. e.subHP(attack,this.gunBase.data,false);
  98. }
  99. }else{
  100. e.subHP(attack,this.gunBase.data,isHeadShot);
  101. }
  102. }
  103. //自动回收了子弹
  104. this.autoRecycle();
  105. }
  106. }
  107. /**
  108. * 加入射击中的效果
  109. * @param e 碰撞到的结果
  110. */
  111. private addImpact(e:PhysicsRayResult) {
  112. let impact: Node = PoolManager.getNode((this.gunBase as unknown as Gun3).impact);
  113. impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
  114. impact.forward = e.hitNormal.multiplyScalar(-1);
  115. impact.scale = this.node.scale;
  116. impact.setParent(e.collider.node,true);
  117. }
  118. }