Bullet1.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult} from 'cc';
  2. import { Gun1 } from './Gun1';
  3. import { Game } from '../../game/Game';
  4. import { GunBase } from '../base/GunBase';
  5. import { BulletBase } from '../base/BulletBase';
  6. import { Enemy, EPartType } from '../../game/Enemy';
  7. import { PoolManager } from '../../core/manager/PoolManager';
  8. import MsgHints from '../../utils/MsgHints';
  9. const { ccclass, property } = _decorator;
  10. //玩家所用子弹 98k m24 awm BarrettM82A1 重狙子弹
  11. @ccclass('Bullet1')
  12. export class Bullet1 extends BulletBase {
  13. //是否必死 必死的子弹不会回收 要自己控制
  14. private isRemoveDead: boolean = false;
  15. //射击方向的位置
  16. private vector: Vec3 = null;
  17. /**
  18. * 初始化一个子弹
  19. */
  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. const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
  36. this.vector = v.clone().multiplyScalar(bulletSpeed);
  37. this.node.forward = v;
  38. this.rayPreCheck(bulletSpeed / 60);
  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. this.rayPreCheck(moveDelta.length());
  63. }
  64. }
  65. /**
  66. * 检测射击
  67. * @param b
  68. * @param len 检测
  69. */
  70. private rayPreCheck(len: number) {
  71. const p = this.node.worldPosition;
  72. const ray = geometry.Ray.create(p.x, p.y, p.z, this.vector.x, this.vector.y, this.vector.z);
  73. const phy:PhysicsSystem = PhysicsSystem.instance;
  74. const isHit = phy.raycast(ray, 0xffffff, len);
  75. if (isHit && phy.raycastResults.length > 0) {
  76. let result:PhysicsRayResult = phy.raycastResults[0];
  77. this.addImpact(result);
  78. const enemyNode: Node = this.findEnemyNode(result.collider.node);
  79. if(enemyNode){
  80. const name: string = result.collider.node.name;
  81. let e: Enemy = enemyNode.getComponent(Enemy);
  82. let attack: number = this.gunBase.data.attack;
  83. //爆头击杀
  84. let isHeadShot:boolean = false;
  85. if(name == EPartType.head){
  86. Game.I.player.headShotNum += 1;
  87. attack = Game.I.player.pData.headshotDmgMul * attack;
  88. isHeadShot = true;
  89. }
  90. //打到了盾兵上的盾
  91. if(name == EPartType.shield && e.shieldHp > 0){
  92. e.shieldHp -= attack;
  93. console.log(" 打击到盾上了 " + e.shieldHp);
  94. MsgHints.show(`扣盾${attack}`)
  95. return;
  96. }else{
  97. console.log(" 扣敌人的血了 " + attack);
  98. MsgHints.show(`扣敌人${attack}`)
  99. }
  100. //坦克是单独的碰撞体
  101. if(e.isTank()){
  102. if(name == EPartType.tank){
  103. e.subHP(attack,this.gunBase.data,false);
  104. }
  105. }else{
  106. e.subHP(attack,this.gunBase.data,isHeadShot);
  107. }
  108. }
  109. //自动回收了子弹
  110. this.autoRecycle();
  111. }
  112. }
  113. /**
  114. * 加入射击中的效果
  115. * @param e 碰撞到的结果
  116. */
  117. private addImpact(e:PhysicsRayResult) {
  118. let impact: Node = PoolManager.getNode((this.gunBase as unknown as Gun1).impact);
  119. impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
  120. impact.forward = e.hitNormal.multiplyScalar(-1);
  121. impact.scale = this.node.scale;
  122. impact.setParent(e.collider.node,true);
  123. }
  124. }
  125. /*MsgHints.show(`打中了: ${result.collider.name}`);
  126. if(result.collider.getComponent(RigidBody)) {
  127. result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
  128. }*/