Bullet1.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. const { ccclass, property } = _decorator;
  9. //玩家所用子弹 重句狙子弹
  10. @ccclass('Bullet1')
  11. export class Bullet1 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. /*MsgHints.show(`打中了: ${result.collider.name}`);
  78. if(result.collider.getComponent(RigidBody)) {
  79. result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
  80. }*/
  81. const name: string = result.collider.name;
  82. let e: Enemy = result.collider.node.parent.getComponent(Enemy);
  83. if(e){
  84. let attack: number = this.gunBase.data.attack;
  85. //爆头击杀
  86. if(name == EPartType.head){
  87. Game.I.player.headShotNum += 1;
  88. attack = Game.I.player.pData.headshotDmgMul * attack;
  89. }
  90. //坦克是单独的碰撞体
  91. if(e.isTank()){
  92. if(name == EPartType.tank){
  93. e.subHP(attack,this.gunBase.data);
  94. }
  95. }else{
  96. e.subHP(attack,this.gunBase.data);
  97. }
  98. }
  99. //自动回收了子弹
  100. this.autoRecycle();
  101. }
  102. }
  103. /**
  104. * 加入射击中的效果
  105. * @param e 碰撞到的结果
  106. */
  107. private addImpact(e:PhysicsRayResult) {
  108. let impact: Node = PoolManager.getNode((this.gunBase as unknown as Gun1).impact);
  109. impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
  110. impact.forward = e.hitNormal.multiplyScalar(-1);
  111. impact.scale = this.node.scale;
  112. impact.setParent(e.collider.node,true);
  113. }
  114. }