预加载

This commit is contained in:
MushrooMagician 2025-03-23 09:07:30 +08:00
parent 174d284e22
commit 6dfac856d5
7 changed files with 135 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import { ModuleDef } from 'db://assets/scripts/ModuleDef';
import { resLoader } from 'db://assets/core_tgx/base/utils/ResLoader';
import { EMusicDefine } from 'db://assets/module_basic/scripts/MusicDefine';
import { SceneDef } from 'db://assets/scripts/SceneDef';
import { ScenePreloadManager } from 'db://assets/scripts/ScenePreloadManager';
const { ccclass, property } = _decorator;

View File

@ -1,4 +1,6 @@
import { _decorator, Component, Node, Animation, Label, director } from 'cc';
import { ScenePreloadManager } from 'db://assets/scripts/ScenePreloadManager';
const { ccclass, property } = _decorator;
enum GameState {

View File

@ -6,6 +6,7 @@ import { ModuleDef } from 'db://assets/scripts/ModuleDef';
import { tgxUIAlert } from 'db://assets/core_tgx/tgx';
import { AreanNetMgr } from '../../scripts/AreanNetMgr';
import { RewardManager } from './RewardManager';
import { ScenePreloadManager } from 'db://assets/scripts/ScenePreloadManager';
const { ccclass, property } = _decorator;
enum GameState {
@ -83,10 +84,10 @@ export class BallGame extends Component {
private gameState: GameState = GameState.Playing;
private countdownDuration: number = 180; // 3分钟 = 180秒
private countdownRemaining: number = 180;
private countdownDuration: number = 120; // 2分钟 = 120秒
private countdownRemaining: number = 120;
private isCountdownStarted: boolean = false;
private startDelay: number = 5; // 开场动画结束后延迟1.5秒开始
private startDelay: number = 0; // 开场动画结束后延迟1.5秒开始
start() {
this.initPhysics();

View File

@ -16,7 +16,8 @@ export const GameServerListFileURL = ""; //'http://192.168.0.104:7456/web-mobile
export const GameServerURLs = [
// 'wss://chengchennet.cn:15005',//wss远程链接
//"ws://150.158.100.241:8092", //ws远程链接
"ws://52.221.198.126:8023", //亚马逊 ws远程链接
//"ws://52.221.198.126:8023", //亚马逊 ws远程链接
"wss://game.squidcryptoweb.com:8023", //亚马逊 ws远程链接
// "ws://127.0.0.1:8092", //ws本地连接
// 'wss://squidgameserver.gryphon.fun'
];

View File

@ -11,6 +11,7 @@ import { ModuleDef } from "db://assets/scripts/ModuleDef";
import { SceneUtil } from "db://assets/core_tgx/base/SceneUtils";
import { urlParse } from "db://assets/core_tgx/base/URLUtils";
import { NetUtil } from "db://assets/module_basic/scripts/NetUtil";
import { ScenePreloadManager } from "db://assets/scripts/ScenePreloadManager";
const { ccclass, property } = _decorator;
export const LAST_NAMES = ['勇敢的', '怂包的', '无辜的', '残暴的', '天真的', '胆小的', '幽默的', '好色的'];
export const GIVEN_NAMES = ['角斗士', '仲裁者', '看守者', '黑色教长', '格里芬', '好战者', '和平使者', '征服者', '百夫长'];
@ -28,6 +29,9 @@ export class LobbyScene extends Component {
tgxUIMgr.inst.closeAll();
// 初始化场景预加载管理器 - 它会自动开始监听场景加载事件
ScenePreloadManager.instance;
// tgxUIMgr.inst.showUI(UIAnnouncement)
// tgxUIMgr.inst.showUI(UIChat);
}

View File

@ -0,0 +1,113 @@
import { _decorator, assetManager, director, Director } from 'cc';
const { ccclass } = _decorator;
/**
*
*
*/
@ccclass('ScenePreloadManager')
export class ScenePreloadManager {
private static _instance: ScenePreloadManager = null;
// 场景预加载映射关系
private readonly scenePreloadMap: Map<string, string[]> = new Map();
// 记录已经预加载的场景
private preloadedScenes: Set<string> = new Set();
// 是否正在预加载
private isPreloading: boolean = false;
public static get instance(): ScenePreloadManager {
if (!this._instance) {
this._instance = new ScenePreloadManager();
this._instance.initialize();
}
return this._instance;
}
private initialize(): void {
// 设置预加载映射关系 - 使用简单的场景名称
this.scenePreloadMap.set('lobby_arean', ['WoodenManScene']); // 大厅 -> 木头人
this.scenePreloadMap.set('WoodenManScene', ['level2Scene']); // 木头人 -> 拔河
this.scenePreloadMap.set('level2Scene', ['BallScene']); // 拔河 -> 弹珠
// 监听场景加载完成事件
director.on(Director.EVENT_AFTER_SCENE_LAUNCH, this.onSceneLoaded, this);
}
/**
*
*/
private onSceneLoaded(): void {
// 获取当前场景名称
const currentScene = director.getScene()?.name || '';
console.log(`当前场景: ${currentScene}`);
// 尝试预加载下一个场景
this.preloadNextScenes(currentScene);
}
/**
*
* @param currentScene
*/
private preloadNextScenes(currentScene: string): void {
if (this.isPreloading || !currentScene) return;
// 获取下一个需要预加载的场景列表
const nextScenes = this.scenePreloadMap.get(currentScene);
if (!nextScenes || nextScenes.length === 0) return;
this.isPreloading = true;
// 延迟1秒进行预加载避免影响当前场景加载
setTimeout(() => {
for (const nextScene of nextScenes) {
// 如果已经预加载过,则跳过
if (this.preloadedScenes.has(nextScene)) continue;
console.log(`预加载场景: ${nextScene}`);
this.preloadScene(nextScene);
}
this.isPreloading = false;
}, 1000);
}
/**
*
* @param sceneName
*/
private preloadScene(sceneName: string): void {
try {
// 简单直接地使用director预加载场景
director.preloadScene(sceneName, (err) => {
if (err) {
console.error(`预加载场景失败: ${sceneName}`, err);
return;
}
console.log(`预加载场景成功: ${sceneName}`);
this.preloadedScenes.add(sceneName);
});
} catch (error) {
console.error(`预加载场景出错: ${sceneName}`, error);
}
}
/**
*
* @param sceneName
*/
public preloadSceneManually(sceneName: string): void {
if (this.preloadedScenes.has(sceneName)) return;
this.preloadScene(sceneName);
}
/**
*
*/
public clearPreloadCache(): void {
this.preloadedScenes.clear();
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "8444d776-cfa8-44f5-a0f0-b369b6a25e61",
"files": [],
"subMetas": {},
"userData": {}
}