import StartScene from "./StartScene";
import createItem from "./items/createItem";
import { award, awardMask } from "./collisionConfig";
import random from "./random";
import LabelType from "./items/LabelType";
import { getCurrentItems } from "./levels";

export const createNewBubble = (_this: StartScene, type: LabelType) => {
    createBubble(_this, type, 750/2, 220,0.001*100);
}

const createBubble = (_this: StartScene, type: LabelType, x: number, y: number,density=0.001) => {
    let scale;
    const size = random(70, 100);
    const maxsize = 100;
    scale = size / maxsize;
    const baseItem = createItem(scale, type);
    let angle;
    angle = 0;
    //气泡纹理种气泡的尺寸默认100（总体还包含气泡周围的阴影效果）
    //刚体和贴图尺寸比为1:1.12
    _this._egretRender.circle(x, y, (size - 17) / 2, baseItem,
        {
            frictionAir: 0,
            collisionFilter: { group: award, category: award, mask: awardMask },
            angle: angle,
            label: baseItem.type,
            friction: 0,
            density:density
        });
}

export default (_this: StartScene) => {
    const items = getCurrentItems();//[[LabelType.boom, 1], [LabelType.coin, 5]]
    const list = [];
    for (const item of items) {
        for (let i = 0; i < item[1]; i++) {
            list.push(item[0]);
        }
    }
    let count = 0;
    const countMax = 1;
    const coinsFall = setInterval(() => {
        if (count <= list.length - 1) {
            const type = list[count];
            createBubble(_this, type, 375 + Math.random() * 200 - 100, 400 + 200 + 100)
            count++;
        } else {
            //结束
        }
    }, 100);
}