预加载
This commit is contained in:
parent
174d284e22
commit
6dfac856d5
|
@ -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;
|
||||
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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'
|
||||
];
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8444d776-cfa8-44f5-a0f0-b369b6a25e61",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Loading…
Reference in New Issue