From 6dfac856d5f19156cd2efe3c2fb9dcfa390f2c90 Mon Sep 17 00:00:00 2001 From: MushrooMagician <799794507@qq.com> Date: Sun, 23 Mar 2025 09:07:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level1/Script/WoodenManGame.ts | 1 + .../module_arean/level2/Script/RopeGame.ts | 2 + .../module_arean/level3/script/BallGame.ts | 7 +- .../module_basic/scripts/FrontConfig.ts | 3 +- .../assets/module_basic/scripts/LobbyScene.ts | 4 + .../assets/scripts/ScenePreloadManager.ts | 113 ++++++++++++++++++ .../scripts/ScenePreloadManager.ts.meta | 9 ++ 7 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 tgx-games-client/assets/scripts/ScenePreloadManager.ts create mode 100644 tgx-games-client/assets/scripts/ScenePreloadManager.ts.meta diff --git a/tgx-games-client/assets/module_arean/level1/Script/WoodenManGame.ts b/tgx-games-client/assets/module_arean/level1/Script/WoodenManGame.ts index 06ce59f..15bd556 100644 --- a/tgx-games-client/assets/module_arean/level1/Script/WoodenManGame.ts +++ b/tgx-games-client/assets/module_arean/level1/Script/WoodenManGame.ts @@ -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; diff --git a/tgx-games-client/assets/module_arean/level2/Script/RopeGame.ts b/tgx-games-client/assets/module_arean/level2/Script/RopeGame.ts index 31b274f..c31915b 100644 --- a/tgx-games-client/assets/module_arean/level2/Script/RopeGame.ts +++ b/tgx-games-client/assets/module_arean/level2/Script/RopeGame.ts @@ -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 { diff --git a/tgx-games-client/assets/module_arean/level3/script/BallGame.ts b/tgx-games-client/assets/module_arean/level3/script/BallGame.ts index e6d920b..1661077 100644 --- a/tgx-games-client/assets/module_arean/level3/script/BallGame.ts +++ b/tgx-games-client/assets/module_arean/level3/script/BallGame.ts @@ -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(); diff --git a/tgx-games-client/assets/module_basic/scripts/FrontConfig.ts b/tgx-games-client/assets/module_basic/scripts/FrontConfig.ts index 5586433..6e86c70 100644 --- a/tgx-games-client/assets/module_basic/scripts/FrontConfig.ts +++ b/tgx-games-client/assets/module_basic/scripts/FrontConfig.ts @@ -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' ]; diff --git a/tgx-games-client/assets/module_basic/scripts/LobbyScene.ts b/tgx-games-client/assets/module_basic/scripts/LobbyScene.ts index 94a084e..3103046 100644 --- a/tgx-games-client/assets/module_basic/scripts/LobbyScene.ts +++ b/tgx-games-client/assets/module_basic/scripts/LobbyScene.ts @@ -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); } diff --git a/tgx-games-client/assets/scripts/ScenePreloadManager.ts b/tgx-games-client/assets/scripts/ScenePreloadManager.ts new file mode 100644 index 0000000..9e94526 --- /dev/null +++ b/tgx-games-client/assets/scripts/ScenePreloadManager.ts @@ -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 = new Map(); + + // 记录已经预加载的场景 + private preloadedScenes: Set = 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(); + } +} \ No newline at end of file diff --git a/tgx-games-client/assets/scripts/ScenePreloadManager.ts.meta b/tgx-games-client/assets/scripts/ScenePreloadManager.ts.meta new file mode 100644 index 0000000..11460de --- /dev/null +++ b/tgx-games-client/assets/scripts/ScenePreloadManager.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "8444d776-cfa8-44f5-a0f0-b369b6a25e61", + "files": [], + "subMetas": {}, + "userData": {} +}