import { _decorator, Camera, Component, Node, RenderTexture, screen, Sprite, SpriteFrame, WebView } from 'cc'; import { AppKernel } from '../../app/AppKernel'; import AppConfig from '@scripts/app/AppConfig'; import { IWebEventHandler, IWebViewMsg, WebViewHandler } from './WebViewHandler'; import LZString from 'lz-string'; import { waitForSeconds } from '@scripts/utils/Helper'; const { ccclass, property } = _decorator; const GAME_FRAME = "http://192.168.1.13:3000"; @ccclass('GameController') export class GameController extends Component implements IWebEventHandler { @property(WebView) webView: WebView | null = null; @property(Node) loadingNode: Node | null = null; private _gameWeb: WebViewHandler | null = null; protected start(): void { if (!this.webView) { console.error('[GameController] WebView is not assigned.'); return; } const gameStore = AppKernel.stores.gameStore; const gameUrl = `${GAME_FRAME}?game=${LZString.compressToBase64(gameStore.gameLink)}`; this._gameWeb = new WebViewHandler( this.webView, gameUrl, this, AppConfig.GameScheme ) // this.webView.node.setPosition(10000, 10000, 0); AppKernel.services.gameService.onEntered(); } protected onDestroy(): void { this._gameWeb?.clear(); this._gameWeb = null; AppKernel.services.gameService.onExited(); } onHandleMsg(data: IWebViewMsg) { switch (data.cmd) { // case "game_ready": // const game_url = AppKernel.stores.gameStore.gameLink; // if (!this._gameWeb) { // console.warn('[GameController] Ignored game_ready after WebView disposal.'); // return; // } // this._gameWeb.sendMessage({ cmd: "update_gamelink", url: game_url }) // break; case "game_exit": this.exitGame(); break; } } async exitGame(): Promise { await AppKernel.services.gameService.exitGame(); } showWebView() { this.webView.node.setPosition(0, 0, 0); } }