-
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathinitializeScript.ts
More file actions
110 lines (103 loc) · 3.68 KB
/
initializeScript.ts
File metadata and controls
110 lines (103 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @file 引擎初始化时会执行的脚本,包括获取游戏信息,初始化运行时变量,初始化用户数据存储
*/
import { logger } from './util/logger';
import { infoFetcher } from './util/coreInitialFunction/infoFetcher';
import { assetSetter, fileType } from './util/gameAssetsAccess/assetSetter';
import { sceneFetcher } from './controller/scene/sceneFetcher';
import { sceneParser } from './parser/sceneParser';
import { bindExtraFunc } from '@/Core/util/coreInitialFunction/bindExtraFunc';
import { webSocketFunc } from '@/Core/util/syncWithEditor/webSocketFunc';
import uniqWith from 'lodash/uniqWith';
import { scenePrefetcher } from './util/prefetcher/scenePrefetcher';
import PixiStage from '@/Core/controller/stage/pixi/PixiController';
import axios from 'axios';
import { __INFO } from '@/config/info';
import { WebGAL } from '@/Core/WebGAL';
import { loadTemplate } from '@/Core/util/coreInitialFunction/templateLoader';
const u = navigator.userAgent;
export const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // 判断是否是 iOS终端
/**
* 引擎初始化函数
*/
export const initializeScript = (): void => {
// 打印初始log信息
logger.info(`WebGAL v${__INFO.version}`);
logger.info('Github: https://github.com/OpenWebGAL/WebGAL ');
logger.info('Made with ❤ by OpenWebGAL');
loadTemplate();
// 激活强制缩放
// 在调整窗口大小时重新计算宽高,设计稿按照 1600*900。
if (isIOS) {
/**
* iOS
*/
alert(
`iOS 用户请横屏使用以获得最佳体验
| Please use landscape mode on iOS for the best experience
| iOS ユーザーは横画面での使用をお勧めします`,
);
}
// 获得 userAnimation
loadStyle('./game/userStyleSheet.css');
// 获得 user Animation
getUserAnimation();
// 获取游戏信息
infoFetcher('./game/config.json');
// 获取start场景
const sceneUrl: string = assetSetter('start.txt', fileType.scene);
// 场景写入到运行时
sceneFetcher(sceneUrl).then((rawScene) => {
WebGAL.sceneManager.sceneData.currentScene = sceneParser(rawScene, 'start.txt', sceneUrl);
// 开始场景的预加载
const subSceneList = WebGAL.sceneManager.sceneData.currentScene.subSceneList;
WebGAL.sceneManager.settledScenes.push(sceneUrl); // 放入已加载场景列表,避免递归加载相同场景
const subSceneListUniq = uniqWith(subSceneList); // 去重
scenePrefetcher(subSceneListUniq);
});
/**
* 启动Pixi
*/
WebGAL.gameplay.pixiStage = new PixiStage();
/**
* iOS 设备 卸载所有 Service Worker
*/
// if ('serviceWorker' in navigator && isIOS) {
// navigator.serviceWorker.getRegistrations().then((registrations) => {
// for (const registration of registrations) {
// registration.unregister().then(() => {
// logger.info('已卸载 Service Worker');
// });
// }
// });
// }
/**
* 绑定工具函数
*/
bindExtraFunc();
webSocketFunc();
};
function loadStyle(url: string) {
const link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
const head = document.getElementsByTagName('head')[0];
head.appendChild(link);
}
function getUserAnimation() {
axios.get('./game/animation/animationTable.json').then((res) => {
const animations: Array<string> = res.data;
for (const animationName of animations) {
axios.get(`./game/animation/${animationName}.json`).then((res) => {
if (res.data) {
const userAnimation = {
name: animationName,
effects: res.data,
};
WebGAL.animationManager.addAnimation(userAnimation);
}
});
}
});
}