Bullet14.ts 2.6 KB

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