Files
esengine/source/src/ECS/Scene.ts

488 lines
16 KiB
TypeScript
Raw Normal View History

2020-07-22 20:07:14 +08:00
module es {
import Bitmap = egret.Bitmap;
2020-07-28 16:25:20 +08:00
/** 场景 */
export class Scene extends egret.DisplayObjectContainer {
/**
*
*/
public camera: Camera;
/**
* 使/使SceneManager.content
* contentManager来加载它们Nez不会卸载它们
*/
public readonly content: ContentManager;
/**
*
*/
public enablePostProcessing = true;
/**
*
*/
public readonly entities: EntityList;
/**
*
*/
public readonly renderableComponents: RenderableComponentList;
/**
*
*/
public readonly entityProcessors: EntityProcessorList;
2020-08-12 19:57:06 +08:00
public _screenshotRequestCallback: Function;
2020-08-11 11:07:20 +08:00
public readonly _sceneComponents: SceneComponent[] = [];
2020-07-28 16:25:20 +08:00
public _renderers: Renderer[] = [];
public readonly _postProcessors: PostProcessor[] = [];
public _didSceneBegin;
/**
*
*/
2020-09-15 09:39:26 +08:00
public dynamicBatch: boolean = false;
2020-07-28 16:25:20 +08:00
constructor() {
super();
this.entities = new EntityList(this);
this.renderableComponents = new RenderableComponentList();
this.content = new ContentManager();
this.entityProcessors = new EntityProcessorList();
this.initialize();
}
/**
* DefaultRenderer附加并准备使用
*/
public static createWithDefaultRenderer() {
let scene = new Scene();
scene.addRenderer(new DefaultRenderer());
return scene;
}
/**
* begin之前
*/
public initialize() {
}
/**
* SceneManager将此场景设置为活动场景时
*/
public async onStart() {
}
/**
* SceneManager从活动槽中删除此场景时调用
*/
public unload() {
}
/**
*
*/
public onActive() {
}
/**
*
*/
public onDeactive() {
}
2020-08-25 17:28:22 +08:00
public begin() {
2020-07-28 16:25:20 +08:00
if (this._renderers.length == 0) {
this.addRenderer(new DefaultRenderer());
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
}
let cameraEntity = this.findEntity("camera");
if (!cameraEntity)
cameraEntity = this.createEntity("camera");
this.camera = cameraEntity.getOrCreateComponent(new Camera());
2020-07-28 16:25:20 +08:00
Physics.reset();
this.updateResolutionScaler();
2020-07-28 16:25:20 +08:00
if (this.entityProcessors)
this.entityProcessors.begin();
Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset,this.updateResolutionScaler, this);
Core.emitter.addObserver(CoreEvents.OrientationChanged, this.updateResolutionScaler, this);
2020-07-28 16:25:20 +08:00
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this._didSceneBegin = true;
this.onStart();
2020-07-28 16:25:20 +08:00
}
public end() {
this._didSceneBegin = false;
Core.emitter.removeObserver(CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler);
Core.emitter.removeObserver(CoreEvents.OrientationChanged, this.updateResolutionScaler);
2020-07-28 16:25:20 +08:00
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();
}
for (let i = 0; i < this._postProcessors.length; i++) {
this._postProcessors[i].unload();
}
2020-07-22 20:07:14 +08:00
2020-07-28 16:25:20 +08:00
this.entities.removeAllEntities();
this.removeChildren();
2020-06-16 11:59:40 +08:00
2020-08-12 19:57:06 +08:00
for (let i = 0; i < this._sceneComponents.length; i++) {
2020-08-11 11:07:20 +08:00
this._sceneComponents[i].onRemovedFromScene();
}
this._sceneComponents.length = 0;
2020-07-28 16:25:20 +08:00
this.camera = null;
this.content.dispose();
2020-07-28 16:25:20 +08:00
if (this.entityProcessors)
this.entityProcessors.end();
2020-06-18 23:22:54 +08:00
2020-07-28 16:25:20 +08:00
if (this.parent)
this.parent.removeChild(this);
2020-07-28 16:25:20 +08:00
this.unload();
}
public updateResolutionScaler(){
this.camera.onSceneRenderTargetSizeChanged(Core.Instance.stage.stageWidth, Core.Instance.stage.stageHeight);
}
2020-07-28 16:25:20 +08:00
public update() {
// 更新我们的列表,以防它们有任何变化
this.entities.updateLists();
2020-08-12 19:57:06 +08:00
for (let i = this._sceneComponents.length - 1; i >= 0; i--) {
2020-08-11 11:07:20 +08:00
if (this._sceneComponents[i].enabled)
this._sceneComponents[i].update();
}
2020-07-28 16:25:20 +08:00
// 更新我们的实体解析器
if (this.entityProcessors)
this.entityProcessors.update();
2020-07-28 16:25:20 +08:00
// 更新我们的实体组
this.entities.update();
2020-07-28 16:25:20 +08:00
if (this.entityProcessors)
this.entityProcessors.lateUpdate();
// 我们在实体之后更新我们的呈现。如果添加了任何新的渲染,请进行更新
this.renderableComponents.updateList();
}
public render() {
if (this._renderers.length == 0) {
2020-08-08 09:43:03 +08:00
console.error("场景中没有渲染器!");
2020-07-28 16:25:20 +08:00
return;
}
for (let i = 0; i < this._renderers.length; i++) {
this.camera.forceMatrixUpdate();
2020-07-28 16:25:20 +08:00
this._renderers[i].render(this);
}
}
2020-09-15 09:39:26 +08:00
/**
*
*/
public dynamicInBatch(){
this.removeChildren();
let batching = false;
let displayContainer: egret.DisplayObjectContainer;
for (let component of this.renderableComponents.buffer){
if (component instanceof SpriteAnimator){
// 动态
this.addChild(component.displayObject);
this.addChild(component.debugDisplayObject);
batching = false;
displayContainer = null;
} else if (component instanceof RenderableComponent) {
// 静态
if (!batching){
batching = true;
displayContainer = new egret.DisplayObjectContainer();
displayContainer.cacheAsBitmap = true;
displayContainer.touchEnabled = false;
displayContainer.touchChildren = false;
2020-09-15 09:39:26 +08:00
this.addChild(displayContainer);
}
displayContainer.addChild(component.displayObject);
displayContainer.addChild(component.debugDisplayObject);
}
}
}
2020-07-28 16:25:20 +08:00
/**
*
* SceneTransition请求渲染时
*/
public postRender() {
if (this.enablePostProcessing) {
for (let i = 0; i < this._postProcessors.length; i++) {
if (this._postProcessors[i].enabled) {
this._postProcessors[i].process();
}
}
}
2020-08-12 19:57:06 +08:00
// 如果我们有一个屏幕截图请求处理它之前最后渲染到backbuffer
if (this._screenshotRequestCallback){
let tex = new egret.RenderTexture();
tex.drawToTexture(this, new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight));
this._screenshotRequestCallback(tex);
this._screenshotRequestCallback = null;
}
}
/**
*
* @param callback
*/
public requestScreenshot(callback: Function){
this._screenshotRequestCallback = callback;
2020-07-28 16:25:20 +08:00
}
2020-08-11 11:07:20 +08:00
/**
* SceneComponent
* @param component
*/
public addSceneComponent<T extends SceneComponent>(component: T): T {
component.scene = this;
component.onEnabled();
this._sceneComponents.push(component);
this._sceneComponents.sort(component.compareTo);
return component;
}
/**
* T的第一个SceneComponent并返回它null
* @param type
*/
2020-08-12 19:57:06 +08:00
public getSceneComponent<T extends SceneComponent>(type) {
for (let i = 0; i < this._sceneComponents.length; i++) {
2020-08-11 11:07:20 +08:00
let component = this._sceneComponents[i];
if (component instanceof type)
return component as T;
}
return null;
}
/**
* T的第一个SceneComponent并返回它SceneComponentSceneComponent
* @param type
*/
2020-08-12 19:57:06 +08:00
public getOrCreateSceneComponent<T extends SceneComponent>(type) {
2020-08-11 11:07:20 +08:00
let comp = this.getSceneComponent<T>(type);
if (comp == null)
comp = this.addSceneComponent<T>(new type());
return comp;
}
/**
* SceneComponents列表中删除一个SceneComponent
* @param component
*/
2020-08-12 19:57:06 +08:00
public removeSceneComponent(component: SceneComponent) {
if (!this._sceneComponents.contains(component)) {
2020-08-11 11:07:20 +08:00
console.warn(`SceneComponent${component}不在SceneComponents列表中!`);
return;
}
this._sceneComponents.remove(component);
component.onRemovedFromScene();
}
2020-07-28 16:25:20 +08:00
/**
*
* @param renderer
*/
public addRenderer<T extends Renderer>(renderer: T) {
this._renderers.push(renderer);
this._renderers.sort();
renderer.onAddedToScene(this);
return renderer;
}
/**
* T的第一个渲染器
* @param type
*/
public getRenderer<T extends Renderer>(type): T {
for (let i = 0; i < this._renderers.length; i++) {
if (this._renderers[i] instanceof type)
return this._renderers[i] as T;
}
return null;
}
/**
*
* @param renderer
*/
public removeRenderer(renderer: Renderer) {
if (!this._renderers.contains(renderer))
return;
this._renderers.remove(renderer);
renderer.unload();
}
/**
* onAddedToScene使后处理器可以使用场景ContentManager加载资源
* @param postProcessor
*/
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;
}
/**
* T的第一个后处理器
* @param type
*/
public getPostProcessor<T extends PostProcessor>(type): T {
2020-07-22 20:07:14 +08:00
for (let i = 0; i < this._postProcessors.length; i++) {
2020-07-28 16:25:20 +08:00
if (this._postProcessors[i] instanceof type)
return this._postProcessors[i] as T;
}
return null;
}
/**
* unloadPostProcessorunload来释放资源
* @param postProcessor
*/
public removePostProcessor(postProcessor: PostProcessor) {
if (!this._postProcessors.contains(postProcessor))
return;
this._postProcessors.remove(postProcessor);
postProcessor.unload();
}
/**
*
* @param name
*/
public createEntity(name: string) {
let entity = new Entity(name);
return this.addEntity(entity);
}
/**
*
* @param entity
*/
public addEntity(entity: Entity) {
if (this.entities.buffer.contains(entity))
console.warn(`您试图将同一实体添加到场景两次: ${entity}`);
2020-07-28 16:25:20 +08:00
this.entities.add(entity);
entity.scene = this;
for (let i = 0; i < entity.transform.childCount; i++)
this.addEntity(entity.transform.getChild(i).entity);
return entity;
}
/**
*
*/
public destroyAllEntities() {
for (let i = 0; i < this.entities.count; i++) {
this.entities.buffer[i].destroy();
2020-07-22 20:07:14 +08:00
}
2020-07-28 16:25:20 +08:00
}
/**
*
* @param name
*/
public findEntity(name: string): Entity {
return this.entities.findEntity(name);
}
/**
*
* @param tag
*/
public findEntitiesWithTag(tag: number): Entity[] {
return this.entities.entitiesWithTag(tag);
}
/**
* T的所有实体
* @param type
*/
public entitiesOfType<T extends Entity>(type): T[] {
return this.entities.entitiesOfType<T>(type);
}
/**
* T的组件
* @param type
*/
public findComponentOfType<T extends Component>(type): T {
return this.entities.findComponentOfType<T>(type);
}
/**
* T的所有已启用已加载组件的列表
* @param type
*/
public findComponentsOfType<T extends Component>(type): T[] {
return this.entities.findComponentsOfType<T>(type);
}
/**
* EntitySystem处理器
* @param processor
*/
public addEntityProcessor(processor: EntitySystem) {
processor.scene = this;
this.entityProcessors.add(processor);
return processor;
}
/**
* EntitySystem处理器
* @param processor
*/
public removeEntityProcessor(processor: EntitySystem) {
this.entityProcessors.remove(processor);
}
/**
* EntitySystem处理器
*/
public getEntityProcessor<T extends EntitySystem>(): T {
return this.entityProcessors.getProcessor<T>();
}
}
2020-06-08 11:49:45 +08:00
}