/**
 * Scene applySettings 补丁：当使用 ammoReplacement (Cannon/p2) 时，
 * 确保 settings.physics 存在，避免 PlayCanvas 引擎在 applySettings 中
 * 访问 undefined.gravity 导致 "Cannot read properties of undefined (reading 'gravity')" 错误。
 *
 * 使用场景：项目使用 Ammo.js 但构建时被替换为 Cannon/p2，部分场景 JSON 可能缺少 physics 配置。
 */
(function () {
    'use strict';
    function patchSceneApplySettings() {
        if (typeof pc === 'undefined' || !pc.Scene || !pc.Scene.prototype.applySettings) {
            return false;
        }
        var original = pc.Scene.prototype.applySettings;
        pc.Scene.prototype.applySettings = function (settings) {
            if (!settings) return;
            if (!settings.physics) {
                settings.physics = { gravity: [0, -9.8, 0] };
            } else if (!settings.physics.gravity) {
                settings.physics.gravity = [0, -9.8, 0];
            }
            if (!settings.render) {
                settings.render = {
                    global_ambient: [0.2, 0.2, 0.2],
                    ambientLuminance: 0,
                    fog: 'none',
                    fog_color: [0.5, 0.5, 0.5],
                    fog_start: 1,
                    fog_end: 1000,
                    fog_density: 0.01,
                    lightmapSizeMultiplier: 16,
                    lightmapMaxResolution: 2048,
                    lightmapMode: 1,
                    exposure: 1
                };
            }
            return original.call(this, settings);
        };
        return true;
    }
    if (patchSceneApplySettings()) {
        console.log('[PlayCraft] Scene applySettings patched for physics defaults');
    } else {
        var attempts = 0;
        var id = setInterval(function () {
            if (patchSceneApplySettings() || ++attempts > 50) clearInterval(id);
        }, 20);
    }
})();
