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

@ccclass('gameManager')
export class gameManager extends Component {

    @property(Node)
    public plant:Node = null;

    @property(Node)
    public zdOut:Node = null;

    @property(Prefab)
    public zd1:Prefab = null;

    /**子弹移动速度*/
    @property
    public zd_speed:number = 0.2;

    /**发射子弹的间隔*/
    @property
    public new_zd_time = 0.3;

    /**当前发射子弹的间隔*/
    @property
    public _new_zd_time = 0;

    private _isTouch:boolean = false;

    start() {
        this._new_zd_time = this.new_zd_time;
    }

    update(deltaTime: number) {

        this._new_zd_time += deltaTime;
        if(this._isTouch && this._new_zd_time > this.new_zd_time){
            this.createZd();
            this._new_zd_time = 0;
        }
        
    }

    public createZd(){
        const zd01 = instantiate(this.zd1);
        zd01.setParent(this.zdOut);
        const ppos = this.plant.position;
        zd01.setPosition(ppos.x, ppos.y, ppos.z - 1);
        const zdcop = zd01.getComponent(zd);
        zdcop.z_speed = this.zd_speed;
    }

    isTouch(val:boolean){
        this._isTouch = val;
    }
}

