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";
export class GameStage extends Node {
private _sceneContainer: StackContainer; //场景容器
private _popupContainer: StackContainer; //弹层容器
private _loadingView: Container;
private _toast: Toast;
private _blackLayer: Rect;
private _stage;
......@@ -49,25 +50,29 @@ export class GameStage extends Node {
this.percentHeight = 100;
this.mouseEnabled = false;
let blackLayer = this._blackLayer = new Rect();
let loadingView = this._loadingView = new Container();
this.addChild(this._sceneContainer = new StackContainer());
this.addChild(blackLayer);
this.addChild(this._popupContainer = new StackContainer());
this.addChild(this._popupContainer = new StackContainer(false, 1));
this.addChild(this._toast = new Toast(this));
this.addChild(loadingView);
let blackLayer = this._blackLayer = new Rect();
blackLayer.name = 'blackLayer';
blackLayer.percentWidth = 100;
blackLayer.percentHeight = 100;
blackLayer.visible = false;
blackLayer.width = stage.width;
blackLayer.height = stage.height;
blackLayer.fillColor = 0;
blackLayer.alpha = 0.7;
loadingView.percentWidth = 100;
loadingView.percentHeight = 100;
loadingView.visible = false;
this._sceneContainer.name = 'scene-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);
}
/**
......@@ -98,38 +103,20 @@ export class GameStage extends Node {
return this._dataCenter;
}
/**
* 启动游戏
* @param config
* @param onPreloadAssetsProgress 预加载素材进度
* @param onPreloadAssetsComplete 预加载素材完成
* @param onStart
*/
async launch(config, onPreloadAssetsProgress?, onPreloadAssetsComplete?, onStart?) {
this._config = config;
this.start();
dealPxEnv();
dealPageRemainTime();
onStart && onStart();
function p() {
loaded++;
onAssetsProgress && onAssetsProgress(loaded, total);
if (loaded >= total) {
onAssetsComplete && onAssetsComplete();
}
}
}
/**
* 开始游戏
* 启动游戏
* @param config
* @param onPreloadAssetsProgress 预加载素材进度
* @param onPreloadAssetsComplete 预加载素材完成
* @param onStart
*/
start() {
const {options: {entrySceneView, env}, dataMapping, processes, builtinProcesses, scripts, customs} = this._config;
async launch(config, onPreloadAssetsProgress?, onPreloadAssetsComplete?, onStart?) {
this._config = config;
const {options: {env}, dataMapping, processes, builtinProcesses, customs} = this._config;
/// TODO 我也不知道为什么,在 Tween 前加入 flush 可以解决因使用 Tween 移动而产生的相对定位节点大小或位置抖动剧烈的问题
Stage.addUpdateObj(alignMgr);
Stage.addUpdateObj(Tween);
......@@ -140,36 +127,112 @@ export class GameStage extends Node {
registerCustomModules(customs);
if (dataMapping) {
if(dataMapping) {
this.dataCenter.registerDataMapping(dataMapping);
}
setProcessMetaLibs(processes, builtinProcesses);
setTimeout(() => {
let sceneEntry = this.instantiateView(entrySceneView);
if (sceneEntry) {
await this.loadPreloadAssets(onPreloadAssetsProgress, onPreloadAssetsComplete);
this.start();
dealPxEnv();
dealPageRemainTime();
onStart && onStart();
setTimeout(this.lazyLoadAllAssets,
this._config.options.lazyLoadWait || 1000);
}
/**
* 开始游戏
*/
start() {
const {options: {entrySceneView,},} = this._config;
setTimeout(async () => {
let sceneEntry = await this.instantiateView(entrySceneView);
if(sceneEntry) {
this._sceneContainer.push(sceneEntry);
}
})
}
/**
* 显示加载视图
*/
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;
}
/**
* 隐藏加载视图
*/
hideInnerLoadingView() {
if(this._loadingView.visible) {
this._loadingView.visible = false;
}
}
/**
* 实例化视图
* @param name
* @param cache 如果开启缓存,就会以单例形式存在
* @param showLoadingView
*/
instantiateView(name, cache = true) {
async instantiateView(name, cache = true, showLoadingView = true) {
let view;
if (cache) {
if(cache) {
view = this._viewCache[name];
}
if (!view) {
if(!view) {
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);
let store = {};
if (viewConfig.store) {
if(viewConfig.store) {
const {exp, computed} = viewConfig.store;
store = createStore(exp, computed);
}
......@@ -177,14 +240,11 @@ export class GameStage extends Node {
view['$isViewRoot'] = true;
view['$_store'] = store;
/*let label = view.children[0];
label['z-for'] = 'item in list';*/
console.time('bind');
bind(store, view);
console.timeEnd('bind');
if (cache) {
if(cache) {
this._viewCache[name] = view;
}
} else {
......@@ -202,28 +262,98 @@ export class GameStage extends Node {
return arrayFind(this._config.views, view => view.name === name);
}
/**
* 设置半透明层是否可见
* @param visible
*/
setBlackLayerVisible(visible) {
this._blackLayer.visible = visible;
}
onPopupContainerChange(e) {
const {action, view, options} = e.data;
onPopupContainerStart(e) {
const {action, view, lastView, options, hasView} = e.data;
const target = <StackContainer>e.target;
switch (action) {
switch(action) {
case 'push':
case 'replace':
case 'popAll':
if (options && options.center) {
view.horizonCenter = 0;
view.verticalCenter = 0;
if(options && options.center) {
view.x = (target.width - view.width) / 2;
view.y = (target.height - view.height) / 2;
}
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";
import {registerScripts} from "./decorators/scripts";
import {RENDERER_TYPE, StageScaleMode} from "../2d/const";
import { alignMgr } from "./decorators/AlignManager";
import {EditorStage} from "./game-warpper/EditorStage";
export let gameStage: GameStage;
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