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
|
|
|
|
/** 场景 */
|
2020-11-23 16:05:06 +08:00
|
|
|
|
export class Scene {
|
2021-05-27 18:32:38 +08:00
|
|
|
|
/** 这个场景中的实体列表 */
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public readonly entities: EntityList;
|
2021-05-27 18:32:38 +08:00
|
|
|
|
/** 管理所有实体处理器 */
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public readonly entityProcessors: EntityProcessorList;
|
2020-12-30 16:28:07 +08:00
|
|
|
|
|
2020-12-14 11:34:23 +08:00
|
|
|
|
public readonly _sceneComponents: SceneComponent[] = [];
|
2021-05-11 17:23:04 +08:00
|
|
|
|
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
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
this.entityProcessors = new EntityProcessorList();
|
2021-05-11 17:23:04 +08:00
|
|
|
|
this.identifierPool = new IdentifierPool();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
this.initialize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-14 14:03:41 +08:00
|
|
|
|
* 初始化场景,可以在派生类中覆盖
|
|
|
|
|
|
*
|
|
|
|
|
|
* 这个方法会在场景创建时被调用。您可以在这个方法中添加实体和组件,
|
|
|
|
|
|
* 或者执行一些必要的准备工作,以便场景能够开始运行。
|
2020-07-28 16:25:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public initialize() {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-14 14:03:41 +08:00
|
|
|
|
* 开始运行场景时调用此方法,可以在派生类中覆盖
|
|
|
|
|
|
*
|
|
|
|
|
|
* 这个方法会在场景开始运行时被调用。您可以在这个方法中执行场景开始时需要进行的操作。
|
|
|
|
|
|
* 比如,您可以开始播放一段背景音乐、启动UI等等。
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-14 14:03:41 +08:00
|
|
|
|
* 卸载场景时调用此方法,可以在派生类中覆盖
|
|
|
|
|
|
*
|
|
|
|
|
|
* 这个方法会在场景被销毁时被调用。您可以在这个方法中销毁实体和组件、释放资源等等。
|
|
|
|
|
|
* 您也可以在这个方法中执行一些必要的清理工作,以确保场景被完全卸载。
|
2020-07-28 16:25:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public unload() {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 开始场景,初始化物理系统、启动实体处理器等
|
|
|
|
|
|
*
|
|
|
|
|
|
* 这个方法会启动场景。它将重置物理系统、启动实体处理器等,并调用onStart方法。
|
|
|
|
|
|
*/
|
2020-08-25 17:28:22 +08:00
|
|
|
|
public begin() {
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 重置物理系统
|
2020-07-28 16:25:20 +08:00
|
|
|
|
Physics.reset();
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 启动实体处理器
|
2020-11-26 10:39:32 +08:00
|
|
|
|
if (this.entityProcessors != null)
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this.entityProcessors.begin();
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 标记场景已开始运行并调用onStart方法
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this._didSceneBegin = true;
|
|
|
|
|
|
this.onStart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 结束场景,清除实体、场景组件、物理系统等
|
|
|
|
|
|
*
|
|
|
|
|
|
* 这个方法会结束场景。它将移除所有实体并调用它们的onRemovedFromScene方法,清除物理系统,结束实体处理器等,并调用unload方法。
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public end() {
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 标记场景已结束运行
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this._didSceneBegin = false;
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 移除所有实体并调用它们的onRemovedFromScene方法
|
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++) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._sceneComponents[i].onRemovedFromScene();
|
2020-08-11 11:07:20 +08:00
|
|
|
|
}
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._sceneComponents.length = 0;
|
2020-08-11 11:07:20 +08:00
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 清除物理系统
|
2020-11-26 10:39:32 +08:00
|
|
|
|
Physics.clear();
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 结束实体处理器
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this.entityProcessors)
|
|
|
|
|
|
this.entityProcessors.end();
|
2020-06-18 23:22:54 +08:00
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 调用卸载方法
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this.unload();
|
|
|
|
|
|
}
|
2020-06-21 10:27:15 +08:00
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新场景,更新实体组件、实体处理器等
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public update() {
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 更新实体列表
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this.entities.updateLists();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 更新场景组件
|
2020-08-12 19:57:06 +08:00
|
|
|
|
for (let i = this._sceneComponents.length - 1; i >= 0; i--) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
if (this._sceneComponents[i].enabled)
|
|
|
|
|
|
this._sceneComponents[i].update();
|
2020-08-11 11:07:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 更新实体处理器
|
2020-11-26 10:39:32 +08:00
|
|
|
|
if (this.entityProcessors != null)
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this.entityProcessors.update();
|
2020-06-21 10:27:15 +08:00
|
|
|
|
|
2023-03-14 14:03:41 +08:00
|
|
|
|
// 更新实体组
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this.entities.update();
|
2023-03-14 14:03:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新实体处理器的后处理方法
|
2020-11-26 10:39:32 +08:00
|
|
|
|
if (this.entityProcessors != null)
|
2020-07-28 16:25:20 +08:00
|
|
|
|
this.entityProcessors.lateUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-11 11:07:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 向组件列表添加并返回SceneComponent
|
|
|
|
|
|
* @param component
|
|
|
|
|
|
*/
|
|
|
|
|
|
public addSceneComponent<T extends SceneComponent>(component: T): T {
|
|
|
|
|
|
component.scene = this;
|
|
|
|
|
|
component.onEnabled();
|
2020-12-14 11:34:23 +08:00
|
|
|
|
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++) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
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并返回它。如果没有找到SceneComponent,则将创建SceneComponent。
|
|
|
|
|
|
* @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) {
|
2021-05-11 17:23:04 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-26 15:23:16 +08:00
|
|
|
|
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
|
|
|
|
/**
|
|
|
|
|
|
* 返回提一个具有该标记的实体
|
2023-03-14 14:03:41 +08:00
|
|
|
|
* @param tag
|
|
|
|
|
|
* @returns
|
2021-03-30 18:02:27 +08:00
|
|
|
|
*/
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-26 17:36:50 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 返回场景中包含特定组件的实体列表
|
2023-03-14 14:03:41 +08:00
|
|
|
|
* @param type
|
|
|
|
|
|
* @returns
|
2021-03-26 17:36:50 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public findEntitiesOfComponent(...types): Entity[] {
|
2021-10-04 16:53:00 +08:00
|
|
|
|
return this.entities.findEntitiesOfComponent(...types);
|
2021-03-26 17:36:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在场景中添加一个EntitySystem处理器
|
|
|
|
|
|
* @param processor 处理器
|
|
|
|
|
|
*/
|
|
|
|
|
|
public addEntityProcessor(processor: EntitySystem) {
|
|
|
|
|
|
processor.scene = this;
|
|
|
|
|
|
this.entityProcessors.add(processor);
|
2023-03-14 14:03:41 +08:00
|
|
|
|
|
2021-08-28 21:26:44 +08:00
|
|
|
|
processor.setUpdateOrder(this.entityProcessors.count - 1);
|
|
|
|
|
|
this.entityProcessors.clearDirty();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
return processor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从场景中删除EntitySystem处理器
|
|
|
|
|
|
* @param processor
|
|
|
|
|
|
*/
|
|
|
|
|
|
public removeEntityProcessor(processor: EntitySystem) {
|
|
|
|
|
|
this.entityProcessors.remove(processor);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取EntitySystem处理器
|
|
|
|
|
|
*/
|
2021-05-13 16:58:24 +08:00
|
|
|
|
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
|
|
|
|
}
|