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

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

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

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

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

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

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

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

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


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

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

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

    /**敌人生成的间隔时间 */
    @property
    public createDpTime = 2;

    /**当前敌人生成的时间 */
    public _createDpTime = 0;

    /**组合间隔状态 */
    public _createDpArr = dpImpl.dpArr.dps1;

    @property
    public dp1_speed:number = 0.05;

    @property
    public dp2_speed:number = 0.03;

    private _isTouch:boolean = false;

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

    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;
        }

        if(this._createDpArr === dpImpl.dpArr.dps1){

            this._createDpTime += deltaTime;
            if(this._createDpTime > this.createDpTime){
                this.cerateDp();
                this._createDpTime = 0;
            }

        }else{

        }
        
    }

    public cerateDp() {

       let randm = math.randomRangeInt(1,3);
       let p:Prefab = null;
       let speed = 0;
       let t = 1;
       if(randm === dpImpl.dpType.TYPE1){
            p = this.dp1;
            speed = this.dp1_speed;
            t=1;
       }else{
            p = this.dp2;
            speed = this.dp2_speed;
            t=2;
       }
       const dp = instantiate(p);
       dp.setParent(this.node);
       const dpc = dp.getComponent(dpComponent);
       dpc._init(speed);
       let loc = math.randomRange(-3.8,3.8);
       dp.setPosition(loc, 1, -dpc.dp_max);

    }

    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;
    }

    changeDpMode(){
        this.schedule(()=>{
            this._createDpArr++;
        },10,3);
    }
}

