"""按模板 + 参数生成 Cocos ParticleSystem2D 配置(JSON)。 纯本地、无需 API。运行时把 JSON 喂给 ParticleSystem2D 即可(或转成 .plist)。 """ import json import os # 几套基础粒子模板(可继续加)。坐标/数值为 Cocos ParticleSystem2D 字段。 TEMPLATES = { "rain": { "emitterType": 0, "duration": -1, "emissionRate": 80, "life": 2.2, "lifeVar": 0.4, "angle": 270, "angleVar": 10, "speed": 220, "speedVar": 40, "gravityY": -300, "gravityX": 0, "startSize": 36, "startSizeVar": 8, "endSize": 36, "startSpin": 0, "startSpinVar": 180, "endSpin": 0, "endSpinVar": 180, "posVarX": 360, "posVarY": 0, }, "burst": { "emitterType": 0, "duration": 0.4, "emissionRate": 600, "life": 0.8, "lifeVar": 0.2, "angle": 90, "angleVar": 180, "speed": 320, "speedVar": 80, "gravityY": -120, "gravityX": 0, "startSize": 28, "startSizeVar": 10, "endSize": 0, "posVarX": 8, "posVarY": 8, }, "glow": { "emitterType": 0, "duration": -1, "emissionRate": 24, "life": 1.4, "lifeVar": 0.3, "angle": 90, "angleVar": 360, "speed": 30, "speedVar": 10, "gravityY": 0, "gravityX": 0, "startSize": 60, "startSizeVar": 14, "endSize": 0, "posVarX": 30, "posVarY": 30, }, "confetti": { "emitterType": 0, "duration": 0.6, "emissionRate": 300, "life": 2.0, "lifeVar": 0.5, "angle": 90, "angleVar": 60, "speed": 400, "speedVar": 120, "gravityY": -260, "gravityX": 0, "startSize": 24, "startSizeVar": 8, "endSize": 16, "startSpin": 0, "startSpinVar": 360, "endSpin": 0, "endSpinVar": 360, "posVarX": 20, "posVarY": 0, }, } def build_particle(vfx_id, template, color, out_dir, texture="particle.png"): base = TEMPLATES.get(template) if base is None: raise ValueError(f"未知粒子模板: {template} (可选 {list(TEMPLATES)})") r, g, b = (color + [255, 255, 255])[:3] cfg = dict(base) cfg.update({ "id": vfx_id, "texture": texture, "startColor": [r, g, b, 255], "startColorVar": [0, 0, 0, 0], "endColor": [r, g, b, 0], "endColorVar": [0, 0, 0, 0], "totalParticles": max(64, int(base["emissionRate"] * base["life"])), "blendFunc": {"src": 770, "dst": 1}, # 加色,适合金币/光效 }) os.makedirs(out_dir, exist_ok=True) path = os.path.join(out_dir, f"{vfx_id}.particle.json") with open(path, "w", encoding="utf-8") as f: json.dump(cfg, f, ensure_ascii=False, indent=2) return path