234 lines
5.5 KiB
JavaScript
234 lines
5.5 KiB
JavaScript
/**
|
||
* main.js - 报恩榴莲微信小游戏入口文件
|
||
*
|
||
* 职责:
|
||
* 1. 初始化微信小游戏环境
|
||
* 2. 加载 Cocos Creator 引擎
|
||
* 3. 启动游戏
|
||
*/
|
||
|
||
// 微信小游戏全局配置
|
||
const GAME_CONFIG = {
|
||
// 屏幕方向
|
||
orientation: 'portrait',
|
||
|
||
// 帧率限制
|
||
fps: 60,
|
||
|
||
// 性能配置(微信小游戏优化)
|
||
performance: {
|
||
maxFruitsOnScreen: 40, // 降低到40以适应微信性能
|
||
enableGC: true, // 启用垃圾回收
|
||
gcInterval: 30000, // 每30秒GC一次
|
||
}
|
||
};
|
||
|
||
class Main {
|
||
constructor() {
|
||
this.initWXEnvironment();
|
||
this.loadGame();
|
||
}
|
||
|
||
/**
|
||
* 初始化微信环境
|
||
*/
|
||
initWXEnvironment() {
|
||
// 设置帧率
|
||
if (wx.setPreferredFramesPerSecond) {
|
||
wx.setPreferredFramesPerSecond(GAME_CONFIG.fps);
|
||
}
|
||
|
||
// 监听音频中断恢复
|
||
if (wx.onAudioInterruptionEnd) {
|
||
wx.onAudioInterruptionEnd(() => {
|
||
console.log('[Main] 音频中断恢复');
|
||
this.resumeAudio();
|
||
});
|
||
}
|
||
|
||
// 监听音频中断开始
|
||
if (wx.onAudioInterruptionBegin) {
|
||
wx.onAudioInterruptionBegin(() => {
|
||
console.log('[Main] 音频中断');
|
||
this.pauseAudio();
|
||
});
|
||
}
|
||
|
||
// 显示加载进度
|
||
this.showLoadingProgress();
|
||
|
||
console.log('[Main] 微信环境初始化完成');
|
||
}
|
||
|
||
/**
|
||
* 暂停音频
|
||
*/
|
||
pauseAudio() {
|
||
if (window.AudioManager) {
|
||
window.AudioManager.getInstance().setMute(true);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 恢复音频
|
||
*/
|
||
resumeAudio() {
|
||
if (window.AudioManager) {
|
||
window.AudioManager.getInstance().setMute(false);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 显示加载进度
|
||
*/
|
||
showLoadingProgress() {
|
||
if (wx.showLoading) {
|
||
wx.showLoading({
|
||
title: '加载中...',
|
||
mask: true
|
||
});
|
||
|
||
let progress = 0;
|
||
const loadingInterval = setInterval(() => {
|
||
progress += 10;
|
||
if (progress >= 90) {
|
||
clearInterval(loadingInterval);
|
||
}
|
||
}, 200);
|
||
|
||
// 保存引用以便后续隐藏
|
||
this.loadingInterval = loadingInterval;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 隐藏加载进度
|
||
*/
|
||
hideLoadingProgress() {
|
||
if (this.loadingInterval) {
|
||
clearInterval(this.loadingInterval);
|
||
}
|
||
if (wx.hideLoading) {
|
||
wx.hideLoading();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 加载游戏
|
||
*/
|
||
loadGame() {
|
||
// 在微信小游戏中,Cocos Creator 会自动加载
|
||
// 这里我们只需要等待 Cocos 引擎就绪
|
||
|
||
if (typeof cocos !== 'undefined') {
|
||
// Cocos 已加载,等待启动
|
||
this.waitForCocosReady();
|
||
} else {
|
||
// 如果 Cocos 未加载,可能需要手动加载
|
||
console.warn('[Main] Cocos 引擎未找到,请确保正确构建项目');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 等待 Cocos 引擎就绪
|
||
*/
|
||
waitForCocosReady() {
|
||
const checkInterval = setInterval(() => {
|
||
if (cc && cc.game) {
|
||
clearInterval(checkInterval);
|
||
this.onCocosReady();
|
||
}
|
||
}, 100);
|
||
}
|
||
|
||
/**
|
||
* Cocos 引擎就绪回调
|
||
*/
|
||
onCocosReady() {
|
||
console.log('[Main] Cocos 引擎就绪');
|
||
|
||
// 隐藏加载进度
|
||
this.hideLoadingProgress();
|
||
|
||
// 注册全局性能监控
|
||
this.registerPerformanceMonitor();
|
||
|
||
// 启动游戏
|
||
this.startGame();
|
||
}
|
||
|
||
/**
|
||
* 启动游戏
|
||
*/
|
||
startGame() {
|
||
// Cocos Creator 会自动启动游戏
|
||
// 这里可以添加额外的初始化逻辑
|
||
|
||
console.log('[Main] 游戏启动');
|
||
|
||
// 预加载广告(可选)
|
||
this.preloadAds();
|
||
|
||
// 上报游戏启动事件
|
||
this.reportLaunchEvent();
|
||
}
|
||
|
||
/**
|
||
* 预加载广告
|
||
*/
|
||
preloadAds() {
|
||
// 延迟预加载,避免影响游戏启动速度
|
||
setTimeout(() => {
|
||
if (window.AdManager) {
|
||
console.log('[Main] 预加载广告');
|
||
window.AdManager.preloadAds();
|
||
}
|
||
}, 3000);
|
||
}
|
||
|
||
/**
|
||
* 上报启动事件
|
||
*/
|
||
reportLaunchEvent() {
|
||
// 使用微信分析接口(如果开通)
|
||
if (wx.reportAnalytics) {
|
||
wx.reportAnalytics('game_launch', {
|
||
timestamp: Date.now(),
|
||
version: '1.0.0'
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 注册性能监控
|
||
*/
|
||
registerPerformanceMonitor() {
|
||
// 定期执行垃圾回收
|
||
if (GAME_CONFIG.performance.enableGC) {
|
||
setInterval(() => {
|
||
if (typeof gc === 'function') {
|
||
gc();
|
||
}
|
||
}, GAME_CONFIG.performance.gcInterval);
|
||
}
|
||
|
||
// 监控内存使用
|
||
if (wx.getSystemInfoSync) {
|
||
try {
|
||
const sysInfo = wx.getSystemInfoSync();
|
||
console.log('[Main] 设备信息:', {
|
||
model: sysInfo.model,
|
||
system: sysInfo.system,
|
||
platform: sysInfo.platform,
|
||
SDKVersion: sysInfo.SDKVersion
|
||
});
|
||
} catch (e) {
|
||
console.warn('[Main] 获取系统信息失败', e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 导出 Main 类
|
||
export default Main;
|