2020-06-08 11:49:45 +08:00
|
|
|
/** 场景 */
|
|
|
|
|
class Scene extends egret.DisplayObjectContainer {
|
2020-06-18 16:35:51 +08:00
|
|
|
public camera: Camera;
|
2020-06-08 20:11:58 +08:00
|
|
|
public readonly entities: EntityList;
|
2020-06-18 16:35:51 +08:00
|
|
|
public readonly renderableComponents: RenderableComponentList;
|
2020-06-21 10:27:15 +08:00
|
|
|
public readonly content: ContentManager;
|
2020-06-23 09:10:40 +08:00
|
|
|
public enablePostProcessing = true;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
private _renderers: Renderer[] = [];
|
2020-06-23 09:10:40 +08:00
|
|
|
private _postProcessors: PostProcessor[] = [];
|
2020-06-21 10:27:15 +08:00
|
|
|
private _didSceneBegin;
|
2020-06-08 16:23:48 +08:00
|
|
|
|
2020-06-08 21:53:09 +08:00
|
|
|
public readonly entityProcessors: EntityProcessorList;
|
2020-06-08 18:26:05 +08:00
|
|
|
|
2020-06-23 16:18:14 +08:00
|
|
|
constructor() {
|
2020-06-08 11:49:45 +08:00
|
|
|
super();
|
2020-06-08 21:53:09 +08:00
|
|
|
this.entityProcessors = new EntityProcessorList();
|
2020-06-18 16:35:51 +08:00
|
|
|
this.renderableComponents = new RenderableComponentList();
|
2020-06-08 20:11:58 +08:00
|
|
|
this.entities = new EntityList(this);
|
2020-06-21 10:27:15 +08:00
|
|
|
this.content = new ContentManager();
|
2020-07-01 14:19:40 +08:00
|
|
|
this.width = SceneManager.stage.stageWidth;
|
|
|
|
|
this.height = SceneManager.stage.stageHeight;
|
2020-06-18 16:35:51 +08:00
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
|
|
|
|
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public createEntity(name: string) {
|
2020-06-08 11:49:45 +08:00
|
|
|
let entity = new Entity(name);
|
2020-06-29 15:41:02 +08:00
|
|
|
entity.position = new Vector2(0, 0);
|
2020-06-08 11:49:45 +08:00
|
|
|
return this.addEntity(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public addEntity(entity: Entity) {
|
2020-06-08 20:11:58 +08:00
|
|
|
this.entities.add(entity);
|
2020-06-08 11:49:45 +08:00
|
|
|
entity.scene = this;
|
2020-06-29 15:41:02 +08:00
|
|
|
this.addChild(entity);
|
2020-06-08 11:49:45 +08:00
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
for (let i = 0; i < entity.numChildren; i++)
|
|
|
|
|
this.addEntity((entity.getChildAt(i) as Component).entity);
|
2020-06-08 20:11:58 +08:00
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public destroyAllEntities() {
|
|
|
|
|
for (let i = 0; i < this.entities.count; i++) {
|
2020-06-29 15:41:02 +08:00
|
|
|
this.entities.buffer[i].destroy();
|
2020-06-08 20:11:58 +08:00
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public findEntity(name: string): Entity {
|
2020-06-08 20:11:58 +08:00
|
|
|
return this.entities.findEntity(name);
|
2020-06-08 18:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在场景中添加一个EntitySystem处理器
|
|
|
|
|
* @param processor 处理器
|
|
|
|
|
*/
|
2020-06-18 16:35:51 +08:00
|
|
|
public addEntityProcessor(processor: EntitySystem) {
|
2020-06-08 18:26:05 +08:00
|
|
|
processor.scene = this;
|
2020-06-08 21:53:09 +08:00
|
|
|
this.entityProcessors.add(processor);
|
2020-06-08 18:26:05 +08:00
|
|
|
return processor;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public removeEntityProcessor(processor: EntitySystem) {
|
2020-06-08 18:26:05 +08:00
|
|
|
this.entityProcessors.remove(processor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getEntityProcessor<T extends EntitySystem>(): T {
|
2020-06-08 21:53:09 +08:00
|
|
|
return this.entityProcessors.getProcessor<T>();
|
2020-06-08 18:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public addRenderer<T extends Renderer>(renderer: T) {
|
|
|
|
|
this._renderers.push(renderer);
|
|
|
|
|
this._renderers.sort();
|
|
|
|
|
|
|
|
|
|
renderer.onAddedToScene(this);
|
|
|
|
|
|
|
|
|
|
return renderer;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
public getRenderer<T extends Renderer>(type): T {
|
|
|
|
|
for (let i = 0; i < this._renderers.length; i++) {
|
2020-06-18 16:35:51 +08:00
|
|
|
if (this._renderers[i] instanceof type)
|
|
|
|
|
return this._renderers[i] as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
public removeRenderer(renderer: Renderer) {
|
2020-06-18 16:35:51 +08:00
|
|
|
this._renderers.remove(renderer);
|
2020-06-23 16:18:14 +08:00
|
|
|
renderer.unload();
|
2020-06-18 16:35:51 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
public begin() {
|
2020-06-23 17:36:39 +08:00
|
|
|
if (SceneManager.sceneTransition){
|
|
|
|
|
SceneManager.stage.addChildAt(this, SceneManager.stage.numChildren - 1);
|
|
|
|
|
}else{
|
|
|
|
|
SceneManager.stage.addChild(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
if (this._renderers.length == 0) {
|
|
|
|
|
this.addRenderer(new DefaultRenderer());
|
|
|
|
|
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
|
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
/** 初始化默认相机 */
|
2020-06-16 11:59:40 +08:00
|
|
|
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
|
|
|
|
|
|
|
|
|
|
Physics.reset();
|
2020-06-08 21:53:09 +08:00
|
|
|
|
|
|
|
|
if (this.entityProcessors)
|
|
|
|
|
this.entityProcessors.begin();
|
2020-06-18 23:22:54 +08:00
|
|
|
|
2020-06-19 08:32:35 +08:00
|
|
|
this.camera.onSceneSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
|
2020-06-23 09:10:40 +08:00
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
this._didSceneBegin = true;
|
|
|
|
|
this.onStart();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
public end() {
|
2020-06-21 10:27:15 +08:00
|
|
|
this._didSceneBegin = false;
|
|
|
|
|
|
|
|
|
|
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
|
|
|
|
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
for (let i = 0; i < this._renderers.length; i++) {
|
2020-06-22 15:27:58 +08:00
|
|
|
this._renderers[i].unload();
|
|
|
|
|
}
|
2020-06-23 09:10:40 +08:00
|
|
|
|
|
|
|
|
for (let i = 0; i < this._postProcessors.length; i++) {
|
|
|
|
|
this._postProcessors[i].unload();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
this.entities.removeAllEntities();
|
2020-06-29 15:41:02 +08:00
|
|
|
this.removeChildren();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
|
|
|
|
Physics.clear();
|
|
|
|
|
|
|
|
|
|
this.camera = null;
|
|
|
|
|
this.content.dispose();
|
|
|
|
|
|
|
|
|
|
if (this.entityProcessors)
|
|
|
|
|
this.entityProcessors.end();
|
2020-06-22 15:27:58 +08:00
|
|
|
|
|
|
|
|
this.unload();
|
2020-07-08 15:15:15 +08:00
|
|
|
|
|
|
|
|
if (this.parent)
|
|
|
|
|
this.parent.removeChild(this);
|
2020-06-21 10:27:15 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-30 14:32:29 +08:00
|
|
|
protected async onStart() {
|
2020-06-21 10:27:15 +08:00
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 场景激活 */
|
2020-06-21 10:27:15 +08:00
|
|
|
protected onActive() {
|
2020-06-18 16:35:51 +08:00
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 场景失去焦点 */
|
2020-06-21 10:27:15 +08:00
|
|
|
protected onDeactive() {
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
protected unload() { }
|
2020-06-22 15:27:58 +08:00
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
public update() {
|
2020-06-08 20:11:58 +08:00
|
|
|
this.entities.updateLists();
|
|
|
|
|
|
2020-06-08 21:53:09 +08:00
|
|
|
if (this.entityProcessors)
|
|
|
|
|
this.entityProcessors.update()
|
|
|
|
|
|
2020-06-08 20:11:58 +08:00
|
|
|
this.entities.update();
|
2020-06-08 21:53:09 +08:00
|
|
|
|
|
|
|
|
if (this.entityProcessors)
|
|
|
|
|
this.entityProcessors.lateUpdate();
|
2020-06-18 16:35:51 +08:00
|
|
|
|
|
|
|
|
this.renderableComponents.updateList();
|
2020-06-18 23:22:54 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-23 09:10:40 +08:00
|
|
|
public postRender() {
|
|
|
|
|
let enabledCounter = 0;
|
|
|
|
|
if (this.enablePostProcessing) {
|
|
|
|
|
for (let i = 0; i < this._postProcessors.length; i++) {
|
|
|
|
|
if (this._postProcessors[i].enable) {
|
|
|
|
|
let isEven = MathHelper.isEven(enabledCounter);
|
|
|
|
|
enabledCounter ++;
|
|
|
|
|
|
2020-06-23 17:36:39 +08:00
|
|
|
this._postProcessors[i].process();
|
2020-06-23 09:10:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
|
for (let i = 0; i < this._renderers.length; i++) {
|
2020-06-18 23:22:54 +08:00
|
|
|
this._renderers[i].render(this);
|
|
|
|
|
}
|
2020-06-08 16:23:48 +08:00
|
|
|
}
|
2020-06-23 16:18:14 +08:00
|
|
|
|
|
|
|
|
public addPostProcessor<T extends PostProcessor>(postProcessor: T): T{
|
|
|
|
|
this._postProcessors.push(postProcessor);
|
|
|
|
|
this._postProcessors.sort();
|
|
|
|
|
postProcessor.onAddedToScene(this);
|
|
|
|
|
|
|
|
|
|
if (this._didSceneBegin){
|
|
|
|
|
postProcessor.onSceneBackBufferSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return postProcessor;
|
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|