// 负责把「当前预设的 spineConfig」应用到 Spine 播放器上。 // // 核心约定:写入与应用分开。WE 可能在 window.load 之前就下发用户属性,那时播放器还不存在; // 旧实现把配置赋值也塞在 `if (this.player)` 里,于是那次配置被静默丢弃, // 结果是"背景与音乐已切到新壁纸、立绘还是旧壁纸"的混合状态(已实测复现)。 // 现在 setConfig() 无论如何都记下配置,create() 用记下的配置创建。 export default class SpineController { constructor(containerId, baseConfig = {}) { this.containerId = containerId; this.baseConfig = { alpha: true, premultipliedAlpha: false, showControls: false, showLoading: false, ...baseConfig, }; this.presetConfig = {}; this.container = document.getElementById(containerId); this.player = undefined; } // 只写状态:与播放器是否存在无关,随时可调用 setConfig(presetConfig = {}) { this.presetConfig = presetConfig; if (this.player) this.rebuild(); } create() { if (this.player) return; // 每次都用 baseConfig + 当前 presetConfig 现算,避免上一份预设的键残留 this.player = new spine.SpinePlayer(this.containerId, { ...this.baseConfig, ...this.presetConfig, }); } // 换骨架没有捷径,只能销毁重建 rebuild() { this.dispose(); this.create(); } dispose() { if (!this.player) return; this.player.dispose(); this.container.innerHTML = ""; this.player = undefined; } }