import { Database, Engine, RenderingManager, Scene } from "@babylonjs/core"; import { SceneManager } from "./controller/scene-manager"; import { LoginSatus } from "./controller/status/login-status"; import { StatusManager } from "./controller/status/status-manager"; import { UIManager } from "./controller/ui-manager"; export class Game { public canvas: HTMLCanvasElement; public engine: Engine; public scene: Scene; static instance: Game; //初始化引擎和画布 public init(canvas: HTMLCanvasElement) { // FileTools.DefaultRetryStrategy = MyRetryStrategy.ExponentialBackoff(2, 1000); // FileTools.DefaultRetryStrategy = RetryStrategy.ExponentialBackoff(2, 1000); Game.instance = this; this.canvas = canvas; this.engine = new Engine(canvas, null, { stencil: true }); Database.IDBStorageEnabled = true;//开启本地缓存 this.scene = new Scene(this.engine); this.scene.useRightHandedSystem = true;//使用右手坐标系 RenderingManager.MIN_RENDERINGGROUPS = -1;//最小渲染序列 this.createScene(); let scene = this.scene; // canvas.translate = true; //用于设置背景透明 // scene.autoClear = true; UIManager.Instance.init(); StatusManager.enterStatus(LoginSatus); //最后,将场景渲染出来 (重要,不可缺少) this.engine.runRenderLoop(function () { scene.render(); }) // 监听浏览器改变大小的事件,通过调用engine.resize()来自适应窗口大小 window.addEventListener("resize", function () { if (Game.instance != null) { Game.instance.engine.resize(); } }); } //创建初始场景 private createScene() { let sceneManager = SceneManager.init(this.scene, this.canvas, this.engine); sceneManager.initArcRotateCamera(); sceneManager.initLight(); sceneManager.updateSceneBG(); sceneManager.initSceneEvent(); } dispose() { SceneManager.Instance.dispose(); StatusManager.dispose(); this.scene.dispose() this.scene = null; this.engine.dispose(); this.engine = null; } } // /** // * 重试策略 // */ // var MyRetryStrategy = /** @class */ (function () { // function RetryStrategy() { // } // /** // * Function used to defines an exponential back off strategy // * @param maxRetries defines the maximum number of retries (3 by default) // * @param baseInterval defines the interval between retries // * @returns the strategy function to use // */ // MyRetryStrategy.ExponentialBackoff = function (maxRetries, baseInterval) { // if (maxRetries === void 0) { maxRetries = 3; } // if (baseInterval === void 0) { baseInterval = 500; } // return function (url, request, retryIndex) { // if (request.status !== 0 || retryIndex >= maxRetries || url.indexOf("file:") !== -1) { // return -1; // } // return Math.pow(2, retryIndex) * baseInterval; // }; // }; // return MyRetryStrategy; // }());