import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
import { car } from '../car';
import { boom_item } from './boom_item';
const { ccclass, property } = _decorator;

@ccclass('boom_manager')
export class boom_manager extends Component {
    @property(Prefab) boom_pf:Prefab = null
    private COUNT:number = 6
    private mCar:car = null
    public init(car:car){
        this.mCar = car
        this.node.removeAllChildren()
        for (let index = 0; index < this.COUNT; index++) {
            this.addItem()
        }
    }
    addItem(){
        let node = instantiate(this.boom_pf)
        node.parent = this.node
        node.getComponent(boom_item).init()
        return node
    }
    public getCanUseItem(){
        for (let index = 0; index < this.node.children.length; index++) {
            const node = this.node.children[index];
            if(node.getComponent(boom_item).IsCanUse()){
                return node
            }
        }
        return this.addItem()
    }
    public showBoom(box_pos:Vec3){
        let boom = this.getCanUseItem()
        boom.getComponent(boom_item).show(box_pos)
    }
    protected update(dt: number): void {
        this.node.position = new Vec3(0,this.mCar.node.parent.position.y)
    }
}


