Bullet1.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. import { Sundries } from '../../game/Sundries';
  10. const { ccclass, property } = _decorator;
  11. //玩家所用子弹 98k m24 awm BarrettM82A1 重狙子弹
  12. @ccclass('Bullet1')
  13. export class Bullet1 extends BulletBase {
  14. //是否必死 必死的子弹不会回收 要自己控制
  15. private isRemoveDead: boolean = false;
  16. //射击方向的位置
  17. private vector: Vec3 = null;
  18. /**
  19. * 初始化一个子弹
  20. */
  21. /**
  22. * 初始化一个子弹
  23. * @param gun 属于哪把枪
  24. * @param isRemoveDead 是否不受控制的子弹自己管理
  25. */
  26. public init(gun:GunBase,isRemoveDead:boolean = true){
  27. this.gunBase = gun;
  28. this.isDead = false;
  29. this.isRemoveDead = isRemoveDead;
  30. }
  31. /**
  32. * 设置子弹开始位置和射击方向
  33. * @param v 射向的方向
  34. */
  35. public setBulletVector(v: Vec3){
  36. const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
  37. this.vector = v.clone().multiplyScalar(bulletSpeed);
  38. this.node.forward = v;
  39. this.rayPreCheck(bulletSpeed / 60);
  40. }
  41. /**
  42. * 射击速度发生改变
  43. */
  44. public speedChange() {
  45. const bulletSpeed = this.gunBase.data.bulletSpeed;
  46. if(this.vector) {
  47. this.vector = this.vector.normalize().multiplyScalar(bulletSpeed);
  48. }
  49. }
  50. /**
  51. * 实时帧更新
  52. */
  53. public update(deltaTime: number) {
  54. if(Game.I.isGameOver
  55. || Game.I.isPause) {
  56. this.isDead = true;
  57. PoolManager.putNode(this.node);
  58. return;
  59. }
  60. if(this.vector) {
  61. const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
  62. this.node.worldPosition = this.node.worldPosition.add(moveDelta);
  63. this.rayPreCheck(moveDelta.length());
  64. }
  65. }
  66. /**
  67. * 检测射击
  68. * @param b
  69. * @param len 检测
  70. */
  71. private rayPreCheck(len: number) {
  72. const p = this.node.worldPosition;
  73. const ray = geometry.Ray.create(p.x, p.y, p.z, this.vector.x, this.vector.y, this.vector.z);
  74. const phy:PhysicsSystem = PhysicsSystem.instance;
  75. const isHit = phy.raycast(ray, 0xffffff, len);
  76. if (isHit && phy.raycastResults.length > 0) {
  77. let result:PhysicsRayResult = phy.raycastResults[0];
  78. this.addImpact(result);
  79. let attack: number = this.gunBase.data.attack;
  80. const name: string = result.collider.node.name;
  81. //查询是否打到的是敌人
  82. const enemyNode: Node = this.findEnemyNode(result.collider.node,Enemy);
  83. if(enemyNode){
  84. let e: Enemy = enemyNode.getComponent(Enemy);
  85. //爆头击杀
  86. let isHeadShot:boolean = false;
  87. if(name == EPartType.head){
  88. Game.I.player.headShotNum += 1;
  89. attack = Game.I.player.pData.headshotDmgMul * attack;
  90. isHeadShot = true;
  91. }
  92. //打到了盾兵上的盾
  93. if(name == EPartType.shield && e.shieldHp > 0){
  94. e.shieldHp -= attack;
  95. console.log(" 打击到盾上了 " + e.shieldHp);
  96. return;
  97. }else{
  98. console.log(" 扣敌人的血了 " + 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. }else{//查询是否打的是杂物
  109. const sundrieNode: Node = this.findEnemyNode(result.collider.node,Sundries);
  110. if(sundrieNode){
  111. let sundrie: Sundries = sundrieNode.getComponent(Sundries);
  112. sundrie.subHP(attack,this.gunBase.data);
  113. }
  114. }
  115. //自动回收了子弹
  116. this.autoRecycle();
  117. }
  118. }
  119. /**
  120. * 加入射击中的效果
  121. * @param e 碰撞到的结果
  122. */
  123. private addImpact(e:PhysicsRayResult) {
  124. let impact: Node = PoolManager.getNode((this.gunBase as unknown as Gun1).impact);
  125. impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
  126. impact.forward = e.hitNormal.multiplyScalar(-1);
  127. impact.scale = this.node.scale;
  128. impact.setParent(e.collider.node,true);
  129. }
  130. }
  131. /*MsgHints.show(`打中了: ${result.collider.name}`);
  132. if(result.collider.getComponent(RigidBody)) {
  133. result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
  134. }*/