Bullet10.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { BulletBase } from '../base/BulletBase';
  5. import { GunBase } from '../base/GunBase';
  6. import { Gun10 } from './Gun10';
  7. const { ccclass, property } = _decorator;
  8. //1001敌人所使用的枪大兵手枪 OR 1007将军手枪所用的子弹
  9. @ccclass('Bullet10')
  10. export class Bullet10 extends BulletBase {
  11. //是否必死 必死的子弹不会回收 要自己控制
  12. private isRemoveDead: boolean = false;
  13. //射击方向的位置
  14. private vector: Vec3 = null;
  15. /**
  16. * 初始化一个子弹
  17. */
  18. /**
  19. * 初始化一个子弹
  20. * @param gun 属于哪把枪
  21. * @param isRemoveDead 是否不受控制的子弹自己管理
  22. */
  23. public init(gun:GunBase,isRemoveDead:boolean = false){
  24. this.gunBase = gun;
  25. this.isDead = false;
  26. this.isRemoveDead = isRemoveDead;
  27. }
  28. /**
  29. * 设置子弹开始位置和射击方向
  30. * @param v 射向的方向
  31. */
  32. public setBulletVector(v: Vec3){
  33. const bulletSpeed = this.gunBase.data.bulletSpeed;
  34. this.vector = v.clone().multiplyScalar(bulletSpeed);
  35. this.node.forward = v;
  36. this.rayPreCheck(bulletSpeed / 60);
  37. }
  38. /**
  39. * 射击速度发生改变
  40. */
  41. public speedChange() {
  42. const bulletSpeed = this.gunBase.data.bulletSpeed;
  43. if(this.vector) {
  44. this.vector = this.vector.normalize().multiplyScalar(bulletSpeed);
  45. }
  46. }
  47. /**
  48. * 实时帧更新
  49. */
  50. public update(deltaTime: number) {
  51. if(Game.I.isGameOver
  52. || Game.I.isPause
  53. ||this.isRemoveDead) {
  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. if(result.collider.getComponent(RigidBody)) {
  77. result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
  78. }
  79. let name:string = result.collider.node.name;
  80. if(name == Game.I.player.head.name){//敌人打玩家爆头
  81. Game.I.player.subHP(this.gunBase.data.attack,this.gunBase.data);
  82. }else if(name == Game.I.player.body.name){//敌人打玩家身体
  83. Game.I.player.subHP(this.gunBase.data.attack,this.gunBase.data);
  84. }
  85. //自动回收了子弹
  86. this.autoRecycle();
  87. }
  88. }
  89. }