SceneManager更改为Core继承egret.DisplayContainer
This commit is contained in:
@@ -105,15 +105,15 @@ module es {
|
||||
if (this._areBoundsDirty){
|
||||
// 旋转或非旋转的边界都需要左上角和右下角
|
||||
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
|
||||
let bottomRight = this.screenToWorldPoint(new Vector2(SceneManager.stage.stageWidth - this._inset.right,
|
||||
SceneManager.stage.stageHeight - this._inset.bottom));
|
||||
let bottomRight = this.screenToWorldPoint(new Vector2(Core.graphicsDevice.viewport.width - this._inset.right,
|
||||
Core.graphicsDevice.viewport.height - this._inset.bottom));
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
// 特别注意旋转的边界。我们需要找到绝对的最小/最大值并从中创建边界
|
||||
let topRight = this.screenToWorldPoint(new Vector2(SceneManager.stage.stageWidth - this._inset.right,
|
||||
let topRight = this.screenToWorldPoint(new Vector2(Core.graphicsDevice.viewport.width - this._inset.right,
|
||||
this._inset.top));
|
||||
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left,
|
||||
SceneManager.stage.stageHeight - this._inset.bottom));
|
||||
Core.graphicsDevice.viewport.height - this._inset.bottom));
|
||||
|
||||
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
@@ -386,8 +386,8 @@ module es {
|
||||
|
||||
public update() {
|
||||
let halfScreen = Vector2.multiply(new Vector2(this.bounds.width, this.bounds.height), new Vector2(0.5));
|
||||
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x * SceneManager.scene.scaleX + this.deadzone.x + this.focusOffset.x;
|
||||
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y * SceneManager.scene.scaleY + this.deadzone.y + this.focusOffset.y;
|
||||
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x * Core.scene.scaleX + this.deadzone.x + this.focusOffset.x;
|
||||
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y * Core.scene.scaleY + this.deadzone.y + this.focusOffset.y;
|
||||
this._worldSpaceDeadZone.width = this.deadzone.width;
|
||||
this._worldSpaceDeadZone.height = this.deadzone.height;
|
||||
|
||||
|
||||
228
source/src/ECS/Core.ts
Normal file
228
source/src/ECS/Core.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
module es {
|
||||
/**
|
||||
* 全局核心类
|
||||
*/
|
||||
export class Core extends egret.DisplayObjectContainer {
|
||||
/**
|
||||
* 订阅此事件以在活动场景发生更改时得到通知。
|
||||
*/
|
||||
public static activeSceneChanged: Function;
|
||||
/**
|
||||
* 核心发射器。只发出核心级别的事件
|
||||
*/
|
||||
public static emitter: Emitter<CoreEvents>;
|
||||
/**
|
||||
* 全局访问图形设备
|
||||
*/
|
||||
public static graphicsDevice: GraphicsDevice;
|
||||
/**
|
||||
* 全局内容管理器加载任何应该停留在场景之间的资产
|
||||
*/
|
||||
public static content: ContentManager;
|
||||
|
||||
/**
|
||||
* 提供对单例/游戏实例的访问
|
||||
* @constructor
|
||||
*/
|
||||
public static get Instance(){
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化对内部类的全局内容实例的访问
|
||||
*/
|
||||
public static _instance: Core;
|
||||
public _scene: Scene;
|
||||
public _nextScene: Scene;
|
||||
public _sceneTransition: SceneTransition;
|
||||
/**
|
||||
* 全局访问系统
|
||||
*/
|
||||
public _globalManagers: GlobalManager[] = [];
|
||||
|
||||
/**
|
||||
* 当前活动的场景。注意,如果设置了该设置,在更新结束之前场景实际上不会改变
|
||||
*/
|
||||
public static get scene() {
|
||||
return this._instance._scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前活动的场景。注意,如果设置了该设置,在更新结束之前场景实际上不会改变
|
||||
* @param value
|
||||
*/
|
||||
public static set scene(value: Scene) {
|
||||
if (!value){
|
||||
console.error("场景不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._instance._scene == null) {
|
||||
this._instance._scene = value;
|
||||
this._instance._scene.begin();
|
||||
Core.Instance.onSceneChanged();
|
||||
} else {
|
||||
this._instance._nextScene = value;
|
||||
}
|
||||
|
||||
this.registerActiveSceneChanged(this._instance._scene, this._instance._nextScene);
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
Core._instance = this;
|
||||
Core.emitter = new Emitter<CoreEvents>();
|
||||
Core.graphicsDevice = new GraphicsDevice();
|
||||
Core.content = new ContentManager();
|
||||
|
||||
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.initialize, this);
|
||||
this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this);
|
||||
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
|
||||
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
|
||||
this.addEventListener(egret.Event.RENDER, this.draw, this);
|
||||
}
|
||||
|
||||
public onOrientationChanged(){
|
||||
Core.emitter.emit(CoreEvents.OrientationChanged);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当屏幕大小发生改变时调用
|
||||
*/
|
||||
protected onGraphicsDeviceReset(){
|
||||
Core.emitter.emit(CoreEvents.GraphicsDeviceReset);
|
||||
}
|
||||
|
||||
protected initialize(){
|
||||
}
|
||||
|
||||
protected update() {
|
||||
this.startDebugUpdate();
|
||||
|
||||
// 更新我们所有的系统管理器
|
||||
Time.update(egret.getTimer());
|
||||
|
||||
if (this._scene) {
|
||||
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
|
||||
if (this._globalManagers[i].enabled)
|
||||
this._globalManagers[i].update();
|
||||
}
|
||||
|
||||
// 仔细阅读:
|
||||
// 当场景转换发生时,我们不会更新场景
|
||||
// -除非是不改变场景的场景转换(没有理由不更新)
|
||||
// -或者它是一个已经切换到新场景的场景转换(新场景需要做它自己的事情)
|
||||
if (!this._sceneTransition ||
|
||||
(this._sceneTransition && (!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
|
||||
this._scene.update();
|
||||
}
|
||||
|
||||
if (this._nextScene) {
|
||||
this._scene.end();
|
||||
|
||||
this._scene = this._nextScene;
|
||||
this._nextScene = null;
|
||||
this.onSceneChanged();
|
||||
|
||||
this._scene.begin();
|
||||
}
|
||||
}
|
||||
|
||||
this.endDebugUpdate();
|
||||
}
|
||||
|
||||
public async draw() {
|
||||
if (this._sceneTransition){
|
||||
this._sceneTransition.preRender();
|
||||
|
||||
// 如果我们有场景转换的特殊处理。我们要么渲染场景过渡,要么渲染场景
|
||||
if (this._scene && !this._sceneTransition.hasPreviousSceneRender){
|
||||
this._scene.render();
|
||||
this._scene.postRender();
|
||||
await this._sceneTransition.onBeginTransition();
|
||||
} else if (this._sceneTransition) {
|
||||
if (this._scene && this._sceneTransition.isNewSceneLoaded) {
|
||||
this._scene.render();
|
||||
this._scene.postRender();
|
||||
}
|
||||
|
||||
this._sceneTransition.render();
|
||||
}
|
||||
} else if (this._scene) {
|
||||
this._scene.render();
|
||||
|
||||
Debug.render();
|
||||
|
||||
// 如果我们没有一个活跃的场景转换,就像平常一样渲染
|
||||
this._scene.postRender();
|
||||
}
|
||||
}
|
||||
|
||||
public startDebugUpdate(){
|
||||
TimeRuler.Instance.startFrame();
|
||||
TimeRuler.Instance.beginMark("update", 0x00FF00);
|
||||
}
|
||||
|
||||
public endDebugUpdate(){
|
||||
TimeRuler.Instance.endMark("update");
|
||||
}
|
||||
|
||||
/**
|
||||
* 在一个场景结束后,下一个场景开始之前调用
|
||||
*/
|
||||
public onSceneChanged(){
|
||||
Core.emitter.emit(CoreEvents.SceneChanged);
|
||||
Time.sceneChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时运行SceneTransition,允许一个场景过渡到另一个平滑的自定义效果。
|
||||
* @param sceneTransition
|
||||
*/
|
||||
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T {
|
||||
if (this._instance._sceneTransition) {
|
||||
console.warn("在前一个场景完成之前,不能开始一个新的场景转换。");
|
||||
return;
|
||||
}
|
||||
|
||||
this._instance._sceneTransition = sceneTransition;
|
||||
return sceneTransition;
|
||||
}
|
||||
|
||||
public static registerActiveSceneChanged(current: Scene, next: Scene){
|
||||
if (this.activeSceneChanged)
|
||||
this.activeSceneChanged(current, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个全局管理器对象,它的更新方法将调用场景前的每一帧。
|
||||
* @param manager
|
||||
*/
|
||||
public static registerGlobalManager(manager: es.GlobalManager){
|
||||
this._instance._globalManagers.push(manager);
|
||||
manager.enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除全局管理器对象
|
||||
* @param manager
|
||||
*/
|
||||
public static unregisterGlobalManager(manager: es.GlobalManager){
|
||||
this._instance._globalManagers.remove(manager);
|
||||
manager.enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型为T的全局管理器
|
||||
* @param type
|
||||
*/
|
||||
public static getGlobalManager<T extends es.GlobalManager>(type): T {
|
||||
for (let i = 0; i < this._instance._globalManagers.length; i ++){
|
||||
if (this._instance._globalManagers[i] instanceof type)
|
||||
return this._instance._globalManagers[i] as T;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,16 @@
|
||||
module es {
|
||||
export enum CoreEvents{
|
||||
/** 当场景发生变化时触发 */
|
||||
/**
|
||||
* 在图形设备重置时触发。当这种情况发生时,任何渲染目标或其他内容的VRAM将被擦除,需要重新生成
|
||||
*/
|
||||
GraphicsDeviceReset,
|
||||
/**
|
||||
* 当场景发生变化时触发
|
||||
*/
|
||||
SceneChanged,
|
||||
/**
|
||||
* 当设备方向改变时触发
|
||||
*/
|
||||
OrientationChanged,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,9 +48,6 @@ module es {
|
||||
|
||||
this.entityProcessors = new EntityProcessorList();
|
||||
|
||||
this.width = SceneManager.stage.stageWidth;
|
||||
this.height = SceneManager.stage.stageHeight;
|
||||
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
@@ -81,13 +78,6 @@ module es {
|
||||
public onDeactive() {}
|
||||
|
||||
public async begin() {
|
||||
// 如果是场景转换需要在最顶层
|
||||
if (SceneManager.sceneTransition){
|
||||
SceneManager.stage.addChildAt(this, SceneManager.stage.numChildren - 1);
|
||||
}else{
|
||||
SceneManager.stage.addChild(this);
|
||||
}
|
||||
|
||||
if (this._renderers.length == 0) {
|
||||
this.addRenderer(new DefaultRenderer());
|
||||
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
|
||||
|
||||
@@ -1,146 +0,0 @@
|
||||
module es {
|
||||
/** 运行时的场景管理。 */
|
||||
export class SceneManager {
|
||||
private static _scene: Scene;
|
||||
private static _nextScene: Scene;
|
||||
public static sceneTransition: SceneTransition;
|
||||
public static stage: egret.Stage;
|
||||
/** 订阅此事件以在活动场景发生更改时得到通知。 */
|
||||
public static activeSceneChanged: Function;
|
||||
/** 核心发射器。只发出核心级别的事件 */
|
||||
public static emitter: Emitter<CoreEvents>;
|
||||
/** 全局内容管理器加载任何应该停留在场景之间的资产 */
|
||||
public static content: ContentManager;
|
||||
/** 简化对内部类的全局内容实例的访问 */
|
||||
private static _instnace: SceneManager;
|
||||
private static timerRuler: TimeRuler;
|
||||
public static get Instance(){
|
||||
return this._instnace;
|
||||
}
|
||||
|
||||
constructor(stage: egret.Stage) {
|
||||
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
|
||||
|
||||
SceneManager._instnace = this;
|
||||
SceneManager.emitter = new Emitter<CoreEvents>();
|
||||
SceneManager.content = new ContentManager();
|
||||
|
||||
SceneManager.stage = stage;
|
||||
SceneManager.initialize(stage);
|
||||
SceneManager.timerRuler = new TimeRuler();
|
||||
}
|
||||
|
||||
public static get scene() {
|
||||
return this._scene;
|
||||
}
|
||||
public static set scene(value: Scene) {
|
||||
if (!value)
|
||||
throw new Error("场景不能为空");
|
||||
|
||||
if (this._scene == null) {
|
||||
this._scene = value;
|
||||
this._scene.begin();
|
||||
SceneManager.Instance.onSceneChanged();
|
||||
} else {
|
||||
this._nextScene = value;
|
||||
}
|
||||
|
||||
this.registerActiveSceneChanged(this._scene, this._nextScene);
|
||||
}
|
||||
|
||||
public static initialize(stage: egret.Stage) {
|
||||
Input.initialize(stage);
|
||||
}
|
||||
|
||||
public static update() {
|
||||
SceneManager.startDebugUpdate();
|
||||
Time.update(egret.getTimer());
|
||||
|
||||
if (SceneManager._scene) {
|
||||
for (let i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
|
||||
if (GlobalManager.globalManagers[i].enabled)
|
||||
GlobalManager.globalManagers[i].update();
|
||||
}
|
||||
|
||||
if (!SceneManager.sceneTransition ||
|
||||
(SceneManager.sceneTransition && (!SceneManager.sceneTransition.loadsNewScene || SceneManager.sceneTransition.isNewSceneLoaded))) {
|
||||
SceneManager._scene.update();
|
||||
}
|
||||
|
||||
if (SceneManager._nextScene) {
|
||||
SceneManager._scene.end();
|
||||
|
||||
SceneManager._scene = SceneManager._nextScene;
|
||||
SceneManager._nextScene = null;
|
||||
SceneManager._instnace.onSceneChanged();
|
||||
|
||||
SceneManager._scene.begin();
|
||||
}
|
||||
}
|
||||
|
||||
SceneManager.endDebugUpdate();
|
||||
SceneManager.render();
|
||||
}
|
||||
|
||||
public static render() {
|
||||
if (this.sceneTransition){
|
||||
this.sceneTransition.preRender();
|
||||
|
||||
if (this._scene && !this.sceneTransition.hasPreviousSceneRender){
|
||||
this._scene.render();
|
||||
this._scene.postRender();
|
||||
this.sceneTransition.onBeginTransition();
|
||||
} else if (this.sceneTransition) {
|
||||
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
|
||||
this._scene.render();
|
||||
this._scene.postRender();
|
||||
}
|
||||
|
||||
this.sceneTransition.render();
|
||||
}
|
||||
} else if (this._scene) {
|
||||
this._scene.render();
|
||||
|
||||
Debug.render();
|
||||
|
||||
this._scene.postRender();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时运行SceneTransition,允许一个场景过渡到另一个平滑的自定义效果。
|
||||
* @param sceneTransition
|
||||
*/
|
||||
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T {
|
||||
if (this.sceneTransition) {
|
||||
console.warn("在前一个场景完成之前,不能开始一个新的场景转换。");
|
||||
return;
|
||||
}
|
||||
|
||||
this.sceneTransition = sceneTransition;
|
||||
return sceneTransition;
|
||||
}
|
||||
|
||||
public static registerActiveSceneChanged(current: Scene, next: Scene){
|
||||
if (this.activeSceneChanged)
|
||||
this.activeSceneChanged(current, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在一个场景结束后,下一个场景开始之前调用
|
||||
*/
|
||||
public onSceneChanged(){
|
||||
SceneManager.emitter.emit(CoreEvents.SceneChanged);
|
||||
Time.sceneChanged();
|
||||
}
|
||||
|
||||
private static startDebugUpdate(){
|
||||
TimeRuler.Instance.startFrame();
|
||||
TimeRuler.Instance.beginMark("update", 0x00FF00);
|
||||
}
|
||||
|
||||
private static endDebugUpdate(){
|
||||
TimeRuler.Instance.endMark("update");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ module es {
|
||||
let offsetY: number = Math.round(bitmapData.$offsetY);
|
||||
let bitmapWidth: number = bitmapData.$bitmapWidth;
|
||||
let bitmapHeight: number = bitmapData.$bitmapHeight;
|
||||
let $TextureScaleFactor = SceneManager.stage.textureScaleFactor;
|
||||
let $TextureScaleFactor = Core._instance.stage.textureScaleFactor;
|
||||
this.sharedContext.drawImage(bitmapData.$bitmapData.source, bitmapData.$bitmapX + rect.x / $TextureScaleFactor, bitmapData.$bitmapY + rect.y / $TextureScaleFactor,
|
||||
bitmapWidth * rect.width / w, bitmapHeight * rect.height / h, offsetX, offsetY, rect.width, rect.height);
|
||||
return surface;
|
||||
|
||||
Reference in New Issue
Block a user