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

297 lines
9.5 KiB
TypeScript
Raw Normal View History

2021-01-01 18:29:10 +08:00
///<reference path="../Math/Vector2.ts" />
2020-07-22 20:07:14 +08:00
module es {
2020-07-28 16:25:20 +08:00
/** 场景 */
export class Scene {
2021-05-27 18:32:38 +08:00
public camera: ICamera;
/** 这个场景中的实体列表 */
2020-07-28 16:25:20 +08:00
public readonly entities: EntityList;
2021-05-27 18:32:38 +08:00
public readonly renderableComponents: RenderableComponentList;
/** 管理所有实体处理器 */
2020-07-28 16:25:20 +08:00
public readonly entityProcessors: EntityProcessorList;
public readonly _sceneComponents: SceneComponent[] = [];
2021-05-27 18:32:38 +08:00
public _renderers: Renderer[] = [];
public readonly identifierPool: IdentifierPool;
2021-01-05 09:41:40 +08:00
private _didSceneBegin: boolean;
2020-07-28 16:25:20 +08:00
constructor() {
this.entities = new EntityList(this);
2021-05-27 18:32:38 +08:00
this.renderableComponents = new RenderableComponentList();
this.entityProcessors = new EntityProcessorList();
this.identifierPool = new IdentifierPool();
2020-07-28 16:25:20 +08:00
this.initialize();
}
/**
2020-11-26 11:48:02 +08:00
*
* begin之前contructor中调用这个函数
2020-07-28 16:25:20 +08:00
*/
public initialize() {
}
/**
2020-11-26 11:48:02 +08:00
* Core将这个场景设置为活动场景时
2020-07-28 16:25:20 +08:00
*/
2020-12-01 20:02:45 +08:00
public onStart() {
2020-07-28 16:25:20 +08:00
}
/**
2020-11-26 11:48:02 +08:00
*
* Core把这个场景从活动槽中移除时
2020-07-28 16:25:20 +08:00
*/
public unload() {
}
2020-08-25 17:28:22 +08:00
public begin() {
2021-05-27 18:32:38 +08:00
if (this._renderers.length == 0) {
this.addRenderer(new DefaultRenderer());
}
2020-07-28 16:25:20 +08:00
Physics.reset();
if (this.entityProcessors != null)
2020-07-28 16:25:20 +08:00
this.entityProcessors.begin();
this._didSceneBegin = true;
this.onStart();
2020-07-28 16:25:20 +08:00
}
public end() {
this._didSceneBegin = false;
2021-05-27 18:32:38 +08:00
for (let i = 0; i < this._renderers.length; i ++)
this._renderers[i].unload();
2020-07-28 16:25:20 +08:00
this.entities.removeAllEntities();
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++) {
this._sceneComponents[i].onRemovedFromScene();
2020-08-11 11:07:20 +08:00
}
this._sceneComponents.length = 0;
2020-08-11 11:07:20 +08:00
2021-05-27 18:32:38 +08:00
this.camera = null;
Physics.clear();
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
this.unload();
}
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--) {
if (this._sceneComponents[i].enabled)
this._sceneComponents[i].update();
2020-08-11 11:07:20 +08:00
}
2020-07-28 16:25:20 +08:00
// 更新我们的实体解析器
if (this.entityProcessors != null)
2020-07-28 16:25:20 +08:00
this.entityProcessors.update();
2020-07-28 16:25:20 +08:00
// 更新我们的实体组
this.entities.update();
2021-05-12 13:08:47 +08:00
if (this.entityProcessors != null)
2020-07-28 16:25:20 +08:00
this.entityProcessors.lateUpdate();
2021-05-27 18:32:38 +08:00
this.renderableComponents.updateLists();
this.render();
}
public render() {
for (let i = 0; i < this._renderers.length; i ++) {
this._renderers[i].render(this);
}
}
public addRenderer<T extends Renderer>(renderer: T): T {
this._renderers.push(renderer);
this._renderers.sort((self, other) => self.renderOrder - other.renderOrder);
renderer.onAddedToScene(this);
return renderer;
}
public getRenderer<T extends Renderer>(type: new (...args: any[]) => T): T {
for (let i = 0; i < this._renderers.length; i ++) {
if (this._renderers[i] instanceof type)
return this._renderers[i] as T;
}
return null;
}
public removeRenderer(renderer: Renderer) {
new List(this._renderers).remove(renderer);
renderer.unload();
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.compare);
2020-08-11 11:07:20 +08:00
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++) {
let component = this._sceneComponents[i];
2020-08-11 11:07:20 +08:00
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) {
2021-03-29 15:28:18 +08:00
const sceneComponentList = new es.List(this._sceneComponents);
2021-01-18 19:54:41 +08:00
Insist.isTrue(sceneComponentList.contains(component), `SceneComponent${component}不在SceneComponents列表中!`);
sceneComponentList.remove(component);
2020-08-11 11:07:20 +08:00
component.onRemovedFromScene();
}
2020-07-28 16:25:20 +08:00
/**
*
* @param name
*/
public createEntity(name: string) {
let entity = new Entity(name, this.identifierPool.checkOut());
2020-07-28 16:25:20 +08:00
return this.addEntity(entity);
}
/**
*
* @param entity
*/
public addEntity(entity: Entity) {
2021-03-29 15:28:18 +08:00
Insist.isFalse(new es.List(this.entities.buffer).contains(entity), `您试图将同一实体添加到场景两次: ${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);
}
public findEntityById(id: number): Entity {
return this.entities.findEntityById(id);
}
2020-07-28 16:25:20 +08:00
/**
*
* @param tag
*/
public findEntitiesWithTag(tag: number): Entity[] {
return this.entities.entitiesWithTag(tag);
}
2021-03-30 18:02:27 +08:00
/**
*
* @param tag
* @returns
*/
public findEntityWithTag(tag: number): Entity {
return this.entities.entityWithTag(tag);
}
2020-07-28 16:25:20 +08:00
/**
* T的组件
* @param type
*/
2021-03-29 17:45:36 +08:00
public findComponentOfType<T extends Component>(type: new (...args) => T): T {
2020-07-28 16:25:20 +08:00
return this.entities.findComponentOfType<T>(type);
}
/**
* T的所有已启用已加载组件的列表
* @param type
*/
2021-03-29 17:45:36 +08:00
public findComponentsOfType<T extends Component>(type: new (...args) => T): T[] {
2020-07-28 16:25:20 +08:00
return this.entities.findComponentsOfType<T>(type);
}
/**
*
* @param type
* @returns
*/
public findEntitiesOfComponent(...types): Entity[] {
return this.entities.findEntitesOfComponent(...types);
}
2020-07-28 16:25:20 +08:00
/**
* 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>(type: new (...args: any[]) => T): T {
return this.entityProcessors.getProcessor<T>(type);
2020-07-28 16:25:20 +08:00
}
}
2020-06-08 11:49:45 +08:00
}