完善渐隐场景转换
This commit is contained in:
@@ -82,4 +82,9 @@ class SpriteRenderer extends RenderableComponent {
|
||||
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
|
||||
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
|
||||
}
|
||||
|
||||
public onRemovedFromEntity(){
|
||||
if (this._bitmap)
|
||||
this.stage.removeChild(this._bitmap);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ class Entity {
|
||||
public readonly components: ComponentList;
|
||||
private _updateOrder: number = 0;
|
||||
private _enabled: boolean = true;
|
||||
private _isDestoryed: boolean;
|
||||
public _isDestoryed: boolean;
|
||||
private _tag: number = 0;
|
||||
|
||||
public componentBits: BitSet;
|
||||
@@ -253,7 +253,7 @@ class Entity {
|
||||
|
||||
public onRemovedFromScene(){
|
||||
if (this._isDestoryed)
|
||||
this.components.remove
|
||||
this.components.removeAllComponents();
|
||||
}
|
||||
|
||||
public onTransformChanged(comp: ComponentTransform){
|
||||
|
||||
@@ -117,6 +117,9 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
|
||||
for (let i = 0; i < this._renderers.length; i ++){
|
||||
this._renderers[i].unload();
|
||||
}
|
||||
this.entities.removeAllEntities();
|
||||
|
||||
Physics.clear();
|
||||
@@ -127,6 +130,8 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.end();
|
||||
|
||||
this.unload();
|
||||
}
|
||||
|
||||
protected onStart(){
|
||||
@@ -143,6 +148,8 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
|
||||
}
|
||||
|
||||
protected unload(){ }
|
||||
|
||||
public update() {
|
||||
this.entities.updateLists();
|
||||
|
||||
|
||||
@@ -3,10 +3,12 @@ class SceneManager {
|
||||
private static _scene: Scene;
|
||||
private static _nextScene: Scene;
|
||||
public static sceneTransition: SceneTransition;
|
||||
public static stage: egret.Stage;
|
||||
|
||||
constructor(stage: egret.Stage) {
|
||||
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
|
||||
|
||||
SceneManager.stage = stage;
|
||||
SceneManager.initialize(stage);
|
||||
}
|
||||
|
||||
@@ -62,18 +64,19 @@ class SceneManager {
|
||||
}
|
||||
|
||||
public static render() {
|
||||
if (this.sceneTransition)
|
||||
if (this.sceneTransition){
|
||||
this.sceneTransition.preRender();
|
||||
|
||||
if (this.sceneTransition) {
|
||||
if (this._scene && this.sceneTransition.wantsPreviousSceneRender && !this.sceneTransition.hasPreviousSceneRender) {
|
||||
this._scene.render();
|
||||
if (this._scene && !this.sceneTransition.hasPreviousSceneRender){
|
||||
this.scene.render();
|
||||
this.sceneTransition.onBeginTransition();
|
||||
} else if (this._scene && this.sceneTransition.isNewSceneLoaded) {
|
||||
this._scene.render();
|
||||
} else if (this.sceneTransition) {
|
||||
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
|
||||
this._scene.render();
|
||||
}
|
||||
|
||||
this.sceneTransition.render();
|
||||
}
|
||||
|
||||
this.sceneTransition.render();
|
||||
} else if (this.scene) {
|
||||
this.scene.render();
|
||||
}
|
||||
@@ -84,7 +87,7 @@ class SceneManager {
|
||||
* @param sceneTransition
|
||||
*/
|
||||
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T {
|
||||
if (!this.sceneTransition) {
|
||||
if (this.sceneTransition) {
|
||||
throw new Error("在前一个场景完成之前,不能开始一个新的场景转换。");
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,8 @@ class EntityList{
|
||||
this.updateLists();
|
||||
|
||||
for (let i = 0; i < this._entities.length; i ++){
|
||||
this._entities[i]._isDestoryed = true;
|
||||
this._entities[i].onRemovedFromScene();
|
||||
this._entities[i].scene = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ abstract class Renderer {
|
||||
* @param scene
|
||||
*/
|
||||
public abstract render(scene: Scene);
|
||||
|
||||
public unload(){ }
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -2,32 +2,37 @@
|
||||
class FadeTransition extends SceneTransition {
|
||||
public fadeToColor: number = 0x000000;
|
||||
public fadeOutDuration = 0.4;
|
||||
private _color: number = 0xFFFFFF;
|
||||
private _toColor: number = 0xFFFFFF;
|
||||
private _destinationRect: Rectangle;
|
||||
private _overlayTexture: egret.RenderTexture;
|
||||
|
||||
constructor(sceneLoadAction: Function){
|
||||
super(sceneLoadAction, true);
|
||||
this._destinationRect = new Rectangle(0, 0, this.previousSceneRender.textureWidth, this.previousSceneRender.textureHeight);
|
||||
public fadeEaseType: Function = egret.Ease.quadInOut;
|
||||
public delayBeforeFadeInDuration = 0.1;
|
||||
private _mask: egret.Shape;
|
||||
private _alpha: number = 0;
|
||||
|
||||
constructor(sceneLoadAction: Function) {
|
||||
super(sceneLoadAction);
|
||||
this._mask = new egret.Shape();
|
||||
}
|
||||
|
||||
// public onBeginTransition(){
|
||||
// this._overlayTexture = new egret.RenderTexture();
|
||||
// let shape = new egret.Shape();
|
||||
// shape.graphics.beginFill(0xFFFFFF, 1);
|
||||
// shape.graphics.drawRect(0, 0, 1, 1);
|
||||
// shape.graphics.endFill();
|
||||
// this._overlayTexture.drawToTexture(shape);
|
||||
public onBeginTransition() {
|
||||
this._mask.graphics.beginFill(this.fadeToColor, 1);
|
||||
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||
this._mask.graphics.endFill();
|
||||
SceneManager.stage.addChild(this._mask);
|
||||
|
||||
// let elapsed = 0;
|
||||
// let _toColor;
|
||||
// while (elapsed < this.fadeOutDuration){
|
||||
// elapsed += Time.deltaTime;
|
||||
|
||||
// // egret.Tween.get(this).to({_color: this._toColor, })
|
||||
// }
|
||||
egret.Tween.get(this).to({ _alpha: 1}, this.fadeOutDuration * 1000, this.fadeEaseType)
|
||||
.call(() => {
|
||||
this.loadNextScene();
|
||||
}).wait(this.delayBeforeFadeInDuration).call(() => {
|
||||
egret.Tween.get(this).to({ _alpha: 0 }, this.fadeOutDuration * 1000, this.fadeEaseType).call(() => {
|
||||
this.transitionComplete();
|
||||
SceneManager.stage.removeChild(this._mask);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// }
|
||||
public render(){
|
||||
this._mask.graphics.clear();
|
||||
this._mask.graphics.beginFill(this.fadeToColor, this._alpha);
|
||||
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||
this._mask.graphics.endFill();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
* SceneTransition用于从一个场景过渡到另一个场景或在一个有效果的场景中过渡
|
||||
*/
|
||||
abstract class SceneTransition {
|
||||
private _hasPreviousSceneRender: boolean;
|
||||
/** 是否加载新场景的标志 */
|
||||
public loadsNewScene: boolean;
|
||||
/**
|
||||
@@ -9,33 +10,15 @@ abstract class SceneTransition {
|
||||
* 对于场景过渡,isNewSceneLoaded应该在中点设置为true,这就标识一个新的场景被加载了。
|
||||
*/
|
||||
public isNewSceneLoaded: boolean;
|
||||
/**
|
||||
* 如果为true
|
||||
* 会将之前的场景渲染到previousSceneRender中,这样你就可以在转换时使用它
|
||||
*/
|
||||
public wantsPreviousSceneRender: boolean;
|
||||
/** 返回新加载场景的函数 */
|
||||
protected sceneLoadAction: Function;
|
||||
/** 包含上一个场景的最后渲染。可以用来在加载新场景时模糊屏幕。 */
|
||||
public previousSceneRender: egret.RenderTexture;
|
||||
/** 在loadNextScene执行时调用。这在进行场景间过渡时很有用,这样你就知道什么时候可以更多地使用相机或者重置任何实体 */
|
||||
public onScreenObscured: Function;
|
||||
/** 当转换完成执行时调用,以便可以调用其他工作,比如启动另一个转换。 */
|
||||
public onTransitionCompleted: Function;
|
||||
public progress: number = 0;
|
||||
|
||||
constructor(sceneLoadAction: Function, wantsPreviousSceneRender: boolean = true) {
|
||||
this.sceneLoadAction = sceneLoadAction;
|
||||
this.wantsPreviousSceneRender = wantsPreviousSceneRender;
|
||||
this.loadsNewScene = sceneLoadAction != null;
|
||||
|
||||
if (wantsPreviousSceneRender)
|
||||
this.previousSceneRender = new egret.RenderTexture();
|
||||
}
|
||||
|
||||
private _hasPreviousSceneRender;
|
||||
public get hasPreviousSceneRender() {
|
||||
if (!this._hasPreviousSceneRender) {
|
||||
public get hasPreviousSceneRender(){
|
||||
if (!this._hasPreviousSceneRender){
|
||||
this._hasPreviousSceneRender = true;
|
||||
return false;
|
||||
}
|
||||
@@ -43,44 +26,39 @@ abstract class SceneTransition {
|
||||
return true;
|
||||
}
|
||||
|
||||
public preRender() { }
|
||||
|
||||
public render(){
|
||||
|
||||
constructor(sceneLoadAction: Function) {
|
||||
this.sceneLoadAction = sceneLoadAction;
|
||||
this.loadsNewScene = sceneLoadAction != null;
|
||||
}
|
||||
|
||||
public onBeginTransition(): Promise<any> {
|
||||
return new Promise((resolve) => {
|
||||
resolve(this.loadScene());
|
||||
this.transitionComplete();
|
||||
});
|
||||
public preRender() { }
|
||||
|
||||
public render() {
|
||||
|
||||
}
|
||||
|
||||
public onBeginTransition() {
|
||||
this.loadNextScene();
|
||||
this.transitionComplete();
|
||||
}
|
||||
|
||||
protected transitionComplete() {
|
||||
SceneManager.sceneTransition = null;
|
||||
|
||||
if (this.previousSceneRender){
|
||||
this.previousSceneRender.dispose();
|
||||
this.previousSceneRender = null;
|
||||
}
|
||||
|
||||
if (this.onTransitionCompleted){
|
||||
if (this.onTransitionCompleted) {
|
||||
this.onTransitionCompleted();
|
||||
}
|
||||
}
|
||||
|
||||
protected loadScene(): Promise<any> {
|
||||
return new Promise(async (resolve) => {
|
||||
if (this.onScreenObscured)
|
||||
this.onScreenObscured();
|
||||
protected loadNextScene() {
|
||||
if (this.onScreenObscured)
|
||||
this.onScreenObscured();
|
||||
|
||||
if (!this.loadsNewScene) {
|
||||
this.isNewSceneLoaded = true;
|
||||
resolve();
|
||||
}
|
||||
|
||||
SceneManager.scene = await this.sceneLoadAction();
|
||||
if (!this.loadsNewScene) {
|
||||
this.isNewSceneLoaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
SceneManager.scene = this.sceneLoadAction();
|
||||
this.isNewSceneLoaded = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user