BulletBase.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. * @param objOrCtor 要查找的对象或构造函数
  24. * @returns 找到的 Enemy 节点(未找到返回 null)
  25. */
  26. public findEnemyNode(startNode: Node,objOrCtor: any): Node | null {
  27. let currentNode: Node | null = startNode;
  28. const clsName: string = js.getClassName(objOrCtor);
  29. let nodeName: string = clsName.charAt(0).toLowerCase() + clsName.slice(1);
  30. while (currentNode) {//匹配节点名字为enemy
  31. if (currentNode.name == nodeName) {
  32. return currentNode;
  33. }
  34. currentNode = currentNode.parent;
  35. }
  36. return null; // 未找到
  37. }
  38. /**
  39. * 自动回收子弹
  40. */
  41. public autoRecycle(){
  42. if(!this.isDead){
  43. this.isDead = true;
  44. PoolManager.putNode(this.node);
  45. }
  46. }
  47. }