123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { _decorator, Component, js,Node } from 'cc';
- import { GunBase } from './GunBase';
- import { PoolManager } from '../../core/manager/PoolManager';
- import { Enemy } from '../../game/Enemy';
- const { ccclass, property } = _decorator;
- @ccclass('BulletBase')
- export class BulletBase extends Component {
- public isDead: boolean = false;
- //枪的数据 在gunBase中
- public gunBase: GunBase = null!;
- //子弹自动回收的时间4s
- public recycleTime: number = 4;
-
- constructor(){
- super();
- this.isDead = false;
- }
-
- start() {
- this.scheduleOnce(this.autoRecycle.bind(this),this.recycleTime);
- }
- /**
- * 递归查找父节点直到找到名字为 "enemy" 的节点
- * @param startNode 起始查找的节点
- * @param objOrCtor 要查找的对象或构造函数
- * @returns 找到的 Enemy 节点(未找到返回 null)
- */
- public findEnemyNode(startNode: Node,objOrCtor: any): Node | null {
- let currentNode: Node | null = startNode;
- const clsName: string = js.getClassName(objOrCtor);
- let nodeName: string = clsName.charAt(0).toLowerCase() + clsName.slice(1);
- while (currentNode) {//匹配节点名字为enemy
- if (currentNode.name == nodeName) {
- return currentNode;
- }
- currentNode = currentNode.parent;
- }
- return null; // 未找到
- }
- /**
- * 自动回收子弹
- */
- public autoRecycle(){
- if(!this.isDead){
- this.isDead = true;
- PoolManager.putNode(this.node);
- }
- }
- }
|