BulletBase.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { _decorator, Component, js,Node } from 'cc';
  2. import { GunBase } from './GunBase';
  3. import { PoolManager } from '../../core/manager/PoolManager';
  4. import { Enemy } from '../../game/Enemy';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('BulletBase')
  7. export class BulletBase extends Component {
  8. public isDead: boolean = false;
  9. //枪的数据 在gunBase中
  10. public gunBase: GunBase = null!;
  11. //子弹自动回收的时间4s
  12. public recycleTime: number = 4;
  13. constructor(){
  14. super();
  15. this.isDead = false;
  16. }
  17. start() {
  18. this.scheduleOnce(this.autoRecycle.bind(this),this.recycleTime);
  19. }
  20. /**
  21. * 递归查找父节点直到找到名字为 "enemy" 的节点
  22. * @param startNode 起始查找的节点
  23. * @returns 找到的 Enemy 节点(未找到返回 null)
  24. */
  25. public findEnemyNode(startNode: Node): Node | null {
  26. let currentNode: Node | null = startNode;
  27. const clsName: string = js.getClassName(Enemy);
  28. let nodeName: string = clsName.charAt(0).toLowerCase() + clsName.slice(1);
  29. while (currentNode) {//匹配节点名字为enemy
  30. if (currentNode.name == nodeName) {
  31. return currentNode;
  32. }
  33. currentNode = currentNode.parent;
  34. }
  35. return null; // 未找到
  36. }
  37. /**
  38. * 自动回收子弹
  39. */
  40. public autoRecycle(){
  41. if(!this.isDead){
  42. this.isDead = true;
  43. PoolManager.putNode(this.node);
  44. }
  45. }
  46. }