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

109 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-06-08 11:49:45 +08:00
/** 场景 */
class Scene extends egret.DisplayObjectContainer {
public camera: Camera;
public entities: Entity[] = [];
2020-06-08 16:23:48 +08:00
private _projectionMatrix: Matrix2D;
private _transformMatrix: Matrix2D;
private _matrixTransformMatrix: Matrix2D;
2020-06-08 18:26:05 +08:00
public readonly entityProcessors: EntitySystem[];
2020-06-08 11:49:45 +08:00
constructor(displayObject: egret.DisplayObject){
super();
2020-06-08 16:23:48 +08:00
displayObject.stage.addChild(this);
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
2020-06-08 18:26:05 +08:00
this.entityProcessors = [];
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-08 16:23:48 +08:00
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
2020-06-08 11:49:45 +08:00
}
public createEntity(name: string){
let entity = new Entity(name);
2020-06-08 16:23:48 +08:00
entity.transform.position = new Vector2(0, 0);
2020-06-08 11:49:45 +08:00
return this.addEntity(entity);
}
public addEntity(entity: Entity){
this.entities.push(entity);
entity.scene = this;
return entity;
}
2020-06-08 18:26:05 +08:00
public destoryAllEntities(){
this.entities.forEach(entity => entity.destory());
}
public findEntity(name: string): Entity{
return this.entities.firstOrDefault(entity => entity.name == name);
}
/**
* EntitySystem处理器
* @param processor
*/
public addEntityProcessor(processor: EntitySystem){
processor.scene = this;
this.entityProcessors.push(processor);
return processor;
}
public removeEntityProcessor(processor: EntitySystem){
this.entityProcessors.remove(processor);
}
public getEntityProcessor<T extends EntitySystem>(): T {
return this.entityProcessors.firstOrDefault(processor => processor instanceof EntitySystem) as T;
}
2020-06-08 11:49:45 +08:00
public setActive(): Scene{
SceneManager.setActiveScene(this);
return this;
}
/** 初始化场景 */
public initialize(){
2020-06-08 18:26:05 +08:00
/** 初始化默认相机 */
this.camera = this.createEntity("camera").addComponent(new Camera());
this.entityProcessors.forEach(processor => processor.initialize());
2020-06-08 11:49:45 +08:00
}
/** 场景激活 */
public onActive(){
}
/** 场景失去焦点 */
public onDeactive(){
}
2020-06-08 16:23:48 +08:00
public update(){
2020-06-08 18:26:05 +08:00
this.entityProcessors.forEach(processor => processor.update());
2020-06-08 16:23:48 +08:00
this.entities.forEach(entity => entity.update());
2020-06-08 18:26:05 +08:00
this.entityProcessors.forEach(processor => processor.lateUpdate());
2020-06-08 16:23:48 +08:00
}
public prepRenderState(){
this._projectionMatrix.m11 = 2 / this.stage.width;
this._projectionMatrix.m22 = -2 / this.stage.height;
this._transformMatrix = this.camera.transformMatrix;
this._matrixTransformMatrix = Matrix2D.multiply(this._transformMatrix, this._projectionMatrix);
}
2020-06-08 11:49:45 +08:00
public destory(){
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.camera.destory();
this.camera = null;
this.entities.forEach(entity => entity.destory());
this.entities.length = 0;
}
}