Bullet10.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { _decorator, Vec3,Node, TERRAIN_SOUTH_INDEX} 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. const { ccclass, property } = _decorator;
  7. //1001敌人所使用的枪大兵手枪 OR 1007将军手枪所用的子弹
  8. @ccclass('Bullet10')
  9. export class Bullet10 extends BulletBase {
  10. //射击方向的位置
  11. private vector: Vec3 = null;
  12. //是否击中玩家
  13. private isHit: boolean = false;
  14. //根据子弹速度计算出移动的时间
  15. private moveTime: number = 0;
  16. //当前时间
  17. private curTime: number = 0;
  18. /**
  19. * 初始化一个子弹
  20. */
  21. /**
  22. * 初始化一个子弹
  23. * @param gun 属于哪把枪
  24. */
  25. public init(gun:GunBase){
  26. //13.6 距离大概在13.6
  27. const factor: number = 50;
  28. const distance:number = Vec3.distance(gun.muzzleNode.worldPosition.clone(), Game.I.player.head.worldPosition.clone());
  29. //这儿是为了保障子弹最短运行时间 不会小于自动回收子弹的时间 就能保证如果要命中的时候 100%命中 并且扣扣血
  30. this.moveTime = Math.min(distance * factor / gun.data.bulletSpeed, this.recycleTime - 0.01);
  31. this.curTime = 0.0;
  32. this.gunBase = gun;
  33. this.isDead = false;
  34. }
  35. /**
  36. * 设置子弹开始位置和射击方向
  37. * @param v 射向的方向
  38. */
  39. public setBulletVector(v: Vec3,isHit: boolean){
  40. this.isHit = isHit;
  41. const bulletSpeed = this.gunBase.data.bulletSpeed;
  42. this.vector = v.clone().multiplyScalar(bulletSpeed);
  43. this.node.forward = v;
  44. }
  45. /**
  46. * 射击速度发生改变
  47. */
  48. public speedChange() {
  49. const bulletSpeed = this.gunBase.data.bulletSpeed;
  50. if(this.vector) {
  51. this.vector = this.vector.normalize().multiplyScalar(bulletSpeed);
  52. }
  53. }
  54. /**
  55. * 实时帧更新
  56. */
  57. public update(deltaTime: number) {
  58. if(Game.I.isGameOver
  59. || Game.I.isPause) {
  60. this.isDead = true;
  61. PoolManager.putNode(this.node);
  62. return;
  63. }
  64. if(this.vector) {
  65. this.curTime += deltaTime;
  66. const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
  67. this.node.worldPosition = this.node.worldPosition.add(moveDelta);
  68. //如果时间超过了移动时间 就回收
  69. if(this.curTime >= this.moveTime && this.isHit) {
  70. Game.I.player.subHP(this.gunBase.data.attack, this.gunBase.data);
  71. this.autoRecycle();
  72. this.curTime = 0.0;
  73. }
  74. }
  75. }
  76. }