Bullet4.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult, RigidBody } from 'cc';
  2. import { PoolManager } from '../../core/manager/PoolManager';
  3. import { Game } from '../../game/Game';
  4. import { Enemy } from '../../game/Enemy';
  5. import { Gun4 } from './Gun4';
  6. import { BulletBase } from '../base/BulletBase';
  7. import { GunBase } from '../base/GunBase';
  8. const { ccclass, property } = _decorator;
  9. //玩家所用子弹 连狙子弹
  10. @ccclass('Bullet4')
  11. export class Bullet4 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. if(result.collider.getComponent(RigidBody)) {
  78. result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
  79. }
  80. let e: Enemy = result.collider.node.parent.getComponent(Enemy);
  81. if(e){
  82. e.subHP(this.gunBase.data.attack,this.gunBase.data);
  83. }
  84. //自动回收了子弹
  85. this.autoRecycle();
  86. }
  87. }
  88. /**
  89. * 加入射击中的效果
  90. * @param e 碰撞到的结果
  91. */
  92. private addImpact(e:PhysicsRayResult) {
  93. let impact: Node = PoolManager.getNode((this.gunBase as unknown as Gun4).impact);
  94. impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
  95. impact.forward = e.hitNormal.multiplyScalar(-1);
  96. impact.scale = this.node.scale;
  97. impact.setParent(e.collider.node,true);
  98. }
  99. }