import FMap from "../map/FMap";
import CUtilTime from "../../../util/CUtilTime";
import FSprite from "../object/FSprite";
import CMath from "../../../util/CMath";
import { GroupType } from "../object/FObject";

const { ccclass, property } = cc._decorator;
/**
 * 怪物触发器
 */
@ccclass
export default class EnemyTrigger extends cc.Component {
    @property({
        displayName: '关联表格数据'
    })
    mId: number = 0;

    @property({
        type: cc.Prefab,
        displayName: '怪物原型',
    })
    mMonster: cc.Prefab = null;

    @property({
        displayName: '初始怪物数量'
    })
    mInitCount: number = 20;

    @property({
        displayName: '最大存在值',
    })
    mMaxCount: number = 20;

    @property({
        displayName: '刷怪周期/秒(-1:不刷新)',
    })
    mFlushTime: number = 5;

    @property({
        displayName: '周期刷怪个数',
    })
    mFlushCount: number = 3;

    public mMap: FMap;
    /**
     * 上次刷新时间
     */
    private prevTime = 0;


    onLoad() {
        let mapNode = this.node.parent.parent;
        this.mMap = mapNode.getComponent(FMap);

        this.flush(this.mInitCount);
        this.prevTime = CUtilTime.getNowTime();
        if (this.mFlushTime > 0) {
            this.schedule(this.addMonster, this.mFlushTime);
        }
    }
    /**
     * 刷新怪物
     * @param count 
     */
    private flush(count: number) {
        for (let i = 0; i < count; i++) {
            // let node: cc.Node = cc.instantiate(this.mMonster);
            // let sp: FSprite = node.getComponent(FSprite);
            // sp.node.group = GroupType.B;
            // sp.trigger = this;
            // sp.mId = this.mId;
            // sp.fData = this.mMap.ff.main.sManage.getMonsterData(this.mId);
            // node.setPosition(this.getRandInit());

            // this.mMap.addSprite(sp);
        }
    
    }
    public getRandInit(): cc.Vec2 {
        let pos = cc.v2();
        pos.x = CMath.getRandom(this.node.x - this.node.width / 2, this.node.x + this.node.width / 2);
        pos.y = CMath.getRandom(this.node.y - this.node.height / 2, this.node.y + this.node.height / 2);
        return pos;
    }

    public addMonster() {
        //判断数量
        // let count = 0;
        // let sprites = this.mMap.getSprites();
        // for (let i = 0; i < sprites.length; i++) {
        //     const node = sprites[i];
        //     let sp = node.getComponent(FSprite);
        //     if(sp && sp.trigger == this){
        //         count ++;
        //     }
        // }
        // if(count >= this.mMaxCount){
        //     return;
        // }

        // this.flush(this.mFlushCount);
    }

}
