2020-06-08 11:49:45 +08:00
|
|
|
/** 场景 */
|
|
|
|
|
class Scene extends egret.DisplayObjectContainer {
|
|
|
|
|
public camera: Camera;
|
2020-06-08 20:11:58 +08:00
|
|
|
public readonly entities: EntityList;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
2020-06-08 16:23:48 +08:00
|
|
|
private _projectionMatrix: Matrix2D;
|
|
|
|
|
private _transformMatrix: Matrix2D;
|
|
|
|
|
private _matrixTransformMatrix: Matrix2D;
|
|
|
|
|
|
2020-06-08 21:53:09 +08:00
|
|
|
public readonly entityProcessors: EntityProcessorList;
|
2020-06-08 18:26:05 +08:00
|
|
|
|
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 21:53:09 +08:00
|
|
|
this.entityProcessors = new EntityProcessorList();
|
2020-06-08 20:11:58 +08:00
|
|
|
this.entities = new EntityList(this);
|
2020-06-08 18:26:05 +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-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){
|
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-08 20:11:58 +08:00
|
|
|
for (let i = 0; i < entity.transform.childCount; i ++)
|
|
|
|
|
this.addEntity(entity.transform.getChild(i).entity);
|
|
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 20:11:58 +08:00
|
|
|
public destroyAllEntities(){
|
|
|
|
|
for (let i = 0; i < this.entities.count; i ++){
|
|
|
|
|
this.entities.buffer[i].destory();
|
|
|
|
|
}
|
2020-06-08 18:26:05 +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 处理器
|
|
|
|
|
*/
|
|
|
|
|
public addEntityProcessor(processor: EntitySystem){
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeEntityProcessor(processor: EntitySystem){
|
|
|
|
|
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-08 11:49:45 +08:00
|
|
|
public setActive(): Scene{
|
|
|
|
|
SceneManager.setActiveScene(this);
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 初始化场景 */
|
|
|
|
|
public initialize(){
|
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-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 场景激活 */
|
|
|
|
|
public onActive(){
|
2020-06-08 20:11:58 +08:00
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 场景失去焦点 */
|
|
|
|
|
public onDeactive(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:23:48 +08:00
|
|
|
public update(){
|
2020-06-08 21:53:09 +08:00
|
|
|
Time.update(egret.getTimer());
|
|
|
|
|
|
2020-06-18 12:14:06 +08:00
|
|
|
for (let i = GlobalManager.globalManagers.length - 1; i >= 0; i --){
|
|
|
|
|
if (GlobalManager.globalManagers[i].enabled)
|
|
|
|
|
GlobalManager.globalManagers[i].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-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;
|
|
|
|
|
|
2020-06-08 20:11:58 +08:00
|
|
|
this.entities.removeAllEntities();
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
}
|