Commit 0eb76352 authored by rockyl's avatar rockyl

Merge branch 'dev' into designtime

# Conflicts:
#	src/zeroing/game-warpper/GameStage.ts
#	src/zeroing/launcher.ts
parents 185d9d4f f5734fe7
...@@ -27,6 +27,7 @@ import { alignMgr } from "../decorators/AlignManager"; ...@@ -27,6 +27,7 @@ import { alignMgr } from "../decorators/AlignManager";
export class GameStage extends Node { export class GameStage extends Node {
private _sceneContainer: StackContainer; //场景容器 private _sceneContainer: StackContainer; //场景容器
private _popupContainer: StackContainer; //弹层容器 private _popupContainer: StackContainer; //弹层容器
private _loadingView: Container;
private _toast: Toast; private _toast: Toast;
private _blackLayer: Rect; private _blackLayer: Rect;
private _stage; private _stage;
...@@ -49,25 +50,29 @@ export class GameStage extends Node { ...@@ -49,25 +50,29 @@ export class GameStage extends Node {
this.percentHeight = 100; this.percentHeight = 100;
this.mouseEnabled = false; this.mouseEnabled = false;
let blackLayer = this._blackLayer = new Rect(); let loadingView = this._loadingView = new Container();
this.addChild(this._sceneContainer = new StackContainer()); this.addChild(this._sceneContainer = new StackContainer());
this.addChild(blackLayer); this.addChild(this._popupContainer = new StackContainer(false, 1));
this.addChild(this._popupContainer = new StackContainer());
this.addChild(this._toast = new Toast(this)); this.addChild(this._toast = new Toast(this));
this.addChild(loadingView);
let blackLayer = this._blackLayer = new Rect();
blackLayer.name = 'blackLayer'; blackLayer.name = 'blackLayer';
blackLayer.percentWidth = 100; blackLayer.width = stage.width;
blackLayer.percentHeight = 100; blackLayer.height = stage.height;
blackLayer.visible = false;
blackLayer.fillColor = 0; blackLayer.fillColor = 0;
blackLayer.alpha = 0.7; blackLayer.alpha = 0.7;
loadingView.percentWidth = 100;
loadingView.percentHeight = 100;
loadingView.visible = false;
this._sceneContainer.name = 'scene-container'; this._sceneContainer.name = 'scene-container';
this._popupContainer.name = 'popup-container'; this._popupContainer.name = 'popup-container';
this._popupContainer.addEventListener('change', this.onPopupContainerChange, this); this._popupContainer.addEventListener(Event.START, this.onPopupContainerStart, this);
this._popupContainer.addEventListener(Event.COMPLETE, this.onPopupContainerComplete, this);
} }
/** /**
...@@ -108,6 +113,27 @@ export class GameStage extends Node { ...@@ -108,6 +113,27 @@ export class GameStage extends Node {
async launch(config, onPreloadAssetsProgress?, onPreloadAssetsComplete?, onStart?) { async launch(config, onPreloadAssetsProgress?, onPreloadAssetsComplete?, onStart?) {
this._config = config; this._config = config;
const {options: {env}, dataMapping, processes, builtinProcesses, customs} = this._config;
/// TODO 我也不知道为什么,在 Tween 前加入 flush 可以解决因使用 Tween 移动而产生的相对定位节点大小或位置抖动剧烈的问题
Stage.addUpdateObj(alignMgr);
Stage.addUpdateObj(Tween);
injectEnv(env);
//registerScripts(scripts);
//registerCustomModuleFromConfig(customs);
registerCustomModules(customs);
if(dataMapping) {
this.dataCenter.registerDataMapping(dataMapping);
}
setProcessMetaLibs(processes, builtinProcesses);
await this.loadPreloadAssets(onPreloadAssetsProgress, onPreloadAssetsComplete);
this.start(); this.start();
dealPxEnv(); dealPxEnv();
...@@ -115,61 +141,98 @@ export class GameStage extends Node { ...@@ -115,61 +141,98 @@ export class GameStage extends Node {
onStart && onStart(); onStart && onStart();
function p() { setTimeout(this.lazyLoadAllAssets,
loaded++; this._config.options.lazyLoadWait || 1000);
onAssetsProgress && onAssetsProgress(loaded, total);
if (loaded >= total) {
onAssetsComplete && onAssetsComplete();
}
}
} }
/** /**
* 开始游戏 * 开始游戏
*/ */
start() { start() {
const {options: {entrySceneView, env}, dataMapping, processes, builtinProcesses, scripts, customs} = this._config; const {options: {entrySceneView,},} = this._config;
Stage.addUpdateObj(Tween);
injectEnv(env); setTimeout(async () => {
let sceneEntry = await this.instantiateView(entrySceneView);
//registerScripts(scripts); if(sceneEntry) {
//registerCustomModuleFromConfig(customs); this._sceneContainer.push(sceneEntry);
}
registerCustomModules(customs); })
}
if (dataMapping) { /**
this.dataCenter.registerDataMapping(dataMapping); * 显示加载视图
*/
async showInnerLoadingView() {
let loadingViewName = this._config.options.loadingView;
let loadingView = this._loadingView.getChildAt(0);
if(this._loadingView.children.length == 0 && loadingViewName) {
loadingView = await this.instantiateView(loadingViewName, true, false);
this._loadingView.addChild(loadingView);
}
this._loadingView.visible = true;
} }
setProcessMetaLibs(processes, builtinProcesses);
setTimeout(() => { /**
let sceneEntry = this.instantiateView(entrySceneView); * 隐藏加载视图
if (sceneEntry) { */
this._sceneContainer.push(sceneEntry); hideInnerLoadingView() {
if(this._loadingView.visible) {
this._loadingView.visible = false;
} }
})
} }
/** /**
* 实例化视图 * 实例化视图
* @param name * @param name
* @param cache 如果开启缓存,就会以单例形式存在 * @param cache 如果开启缓存,就会以单例形式存在
* @param showLoadingView
*/ */
instantiateView(name, cache = true) { async instantiateView(name, cache = true, showLoadingView = true) {
let view; let view;
if (cache) { if(cache) {
view = this._viewCache[name]; view = this._viewCache[name];
} }
if (!view) { if(!view) {
let viewConfig = this.getViewConfigByName(name); let viewConfig = this.getViewConfigByName(name);
if (viewConfig) { if(viewConfig) {
const {assets, customs} = this._config;
if(!Array.isArray(assets)) {
let groups = {};
let {depCustoms} = viewConfig;
if(depCustoms && depCustoms.length > 0) {
for(let custom of customs) {
if(depCustoms.indexOf(custom.id) >= 0 && custom.assets) {
if(!checkAssetGroupLoaded(custom.id)) {
groups[custom.id] = custom.assets;
}
}
}
}
if(!checkAssetGroupLoaded(viewConfig.uuid)) {
let depAssets = assets[viewConfig.uuid];
if(depAssets && depAssets.length > 0) {
groups[viewConfig.uuid] = depAssets;
}
}
if(Object.keys(groups).length > 0) {
await loadAssetsGroups(groups, async () => {
if(showLoadingView) {
await this.showInnerLoadingView();
}
}, async () => {
if(showLoadingView) {
this.hideInnerLoadingView();
}
});
}
}
view = instantiate(viewConfig); view = instantiate(viewConfig);
let store = {}; let store = {};
if (viewConfig.store) { if(viewConfig.store) {
const {exp, computed} = viewConfig.store; const {exp, computed} = viewConfig.store;
store = createStore(exp, computed); store = createStore(exp, computed);
} }
...@@ -177,14 +240,11 @@ export class GameStage extends Node { ...@@ -177,14 +240,11 @@ export class GameStage extends Node {
view['$isViewRoot'] = true; view['$isViewRoot'] = true;
view['$_store'] = store; view['$_store'] = store;
/*let label = view.children[0];
label['z-for'] = 'item in list';*/
console.time('bind'); console.time('bind');
bind(store, view); bind(store, view);
console.timeEnd('bind'); console.timeEnd('bind');
if (cache) { if(cache) {
this._viewCache[name] = view; this._viewCache[name] = view;
} }
} else { } else {
...@@ -202,28 +262,98 @@ export class GameStage extends Node { ...@@ -202,28 +262,98 @@ export class GameStage extends Node {
return arrayFind(this._config.views, view => view.name === name); return arrayFind(this._config.views, view => view.name === name);
} }
/** onPopupContainerStart(e) {
* 设置半透明层是否可见 const {action, view, lastView, options, hasView} = e.data;
* @param visible const target = <StackContainer>e.target;
*/
setBlackLayerVisible(visible) {
this._blackLayer.visible = visible;
}
onPopupContainerChange(e) {
const {action, view, options} = e.data;
switch (action) { switch(action) {
case 'push': case 'push':
case 'replace': case 'replace':
case 'popAll': case 'popAll':
if (options && options.center) { if(options && options.center) {
view.horizonCenter = 0; view.x = (target.width - view.width) / 2;
view.verticalCenter = 0; view.y = (target.height - view.height) / 2;
} }
break; break;
} }
this.setBlackLayerVisible(this._popupContainer.children.length > 0); let blackLayer = this._blackLayer;
let pParent = blackLayer.parent;
if(hasView) {
target.addChildAt(blackLayer, target.childNum - 1 - (view ? 0 : 1));
let modalAlpha = 0.7;
if(options && options.hasOwnProperty('modalAlpha')) {
modalAlpha = options.modalAlpha;
}
if(!pParent || blackLayer['tween_count'] > 0) {
blackLayer.alpha = 0;
Tween.get(blackLayer, null, null, true)
.to({alpha: modalAlpha}, 300);
}
} else if(pParent) {
Tween.get(blackLayer, null, null, true)
.to({alpha: 0}, 300)
.call(() => {
pParent.removeChild(blackLayer);
});
}
}
onPopupContainerComplete(e) {
}
lazyLoadAllAssets = async () => {
let {assets, customs} = this._config;
if(!Array.isArray(assets)) {
for(let uuid in assets) {
let group = assets[uuid];
if(group && group.length > 0) {
await loadAssetsGroup(group, uuid);
}
}
}
for(let custom of customs) {
let group = custom.assets;
if(group && group.length > 0) {
await loadAssetsGroup(group, custom.id);
}
}
};
async loadPreloadAssets(onPreloadAssetsProgress, onPreloadAssetsComplete) {
const {assets,} = this._config;
let loaded = 0;
let assetsToLoad = assets;
let total;
if(!Array.isArray(assets)) { //如果是分组后的配置就加载共用分组,否则加载全部
assetsToLoad = assets['common'];
if(assetsToLoad.length > 0) {
await this.showInnerLoadingView();
await loadAssetsGroups({
preload: assetsToLoad,
}, async () => {
}, async () => {
this.hideInnerLoadingView();
});
}
} else {
total = assetsToLoad.length;
await loadAssets(assetsToLoad, p).catch(e => {
console.log(e);
});
}
function p() {
loaded++;
onPreloadAssetsProgress && onPreloadAssetsProgress(loaded, total);
if(loaded >= total) {
onPreloadAssetsComplete && onPreloadAssetsComplete();
}
}
} }
} }
...@@ -13,6 +13,7 @@ import {initAutoLayout} from "./game-warpper/auto-layout"; ...@@ -13,6 +13,7 @@ import {initAutoLayout} from "./game-warpper/auto-layout";
import {registerScripts} from "./decorators/scripts"; import {registerScripts} from "./decorators/scripts";
import {RENDERER_TYPE, StageScaleMode} from "../2d/const"; import {RENDERER_TYPE, StageScaleMode} from "../2d/const";
import { alignMgr } from "./decorators/AlignManager"; import { alignMgr } from "./decorators/AlignManager";
import {EditorStage} from "./game-warpper/EditorStage";
export let gameStage: GameStage; export let gameStage: GameStage;
export let editorStage: EditorStage; export let editorStage: EditorStage;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment