控制实体update默认不更新
This commit is contained in:
@@ -151,4 +151,9 @@ export interface ISceneConfig {
|
||||
* 调试配置
|
||||
*/
|
||||
debug?: boolean;
|
||||
/**
|
||||
* 是否启用实体直接更新
|
||||
* @default false
|
||||
*/
|
||||
enableEntityDirectUpdate?: boolean;
|
||||
}
|
||||
@@ -95,6 +95,11 @@ export class Scene implements IScene {
|
||||
this.name = config.name;
|
||||
}
|
||||
|
||||
// 配置实体直接更新选项
|
||||
if (config?.enableEntityDirectUpdate !== undefined) {
|
||||
this.entities.setEnableEntityDirectUpdate(config.enableEntityDirectUpdate);
|
||||
}
|
||||
|
||||
if (!Entity.eventBus) {
|
||||
Entity.eventBus = new EventBus(false);
|
||||
}
|
||||
@@ -172,7 +177,7 @@ export class Scene implements IScene {
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新场景,更新实体组件、实体处理器等
|
||||
* 更新场景
|
||||
*/
|
||||
public update() {
|
||||
// 更新实体列表
|
||||
@@ -182,10 +187,10 @@ export class Scene implements IScene {
|
||||
if (this.entityProcessors != null)
|
||||
this.entityProcessors.update();
|
||||
|
||||
// 更新实体组
|
||||
// 更新实体
|
||||
this.entities.update();
|
||||
|
||||
// 更新实体处理器的后处理方法
|
||||
// 更新实体处理器后处理
|
||||
if (this.entityProcessors != null)
|
||||
this.entityProcessors.lateUpdate();
|
||||
}
|
||||
|
||||
@@ -12,12 +12,15 @@ export class EntityList {
|
||||
// 索引映射,提升查找性能
|
||||
private _idToEntity = new Map<number, Entity>();
|
||||
private _nameToEntities = new Map<string, Entity[]>();
|
||||
|
||||
|
||||
// 延迟操作队列
|
||||
private _entitiesToAdd: Entity[] = [];
|
||||
private _entitiesToRemove: Entity[] = [];
|
||||
private _isUpdating = false;
|
||||
|
||||
// 是否启用实体直接更新
|
||||
private _enableEntityDirectUpdate = false;
|
||||
|
||||
public get count(): number {
|
||||
return this.buffer.length;
|
||||
}
|
||||
@@ -26,6 +29,13 @@ export class EntityList {
|
||||
this._scene = scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用实体直接更新
|
||||
*/
|
||||
public setEnableEntityDirectUpdate(enabled: boolean): void {
|
||||
this._enableEntityDirectUpdate = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加实体(立即添加或延迟添加)
|
||||
* @param entity 要添加的实体
|
||||
@@ -137,22 +147,25 @@ export class EntityList {
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新所有实体
|
||||
* 更新实体列表和实体
|
||||
*/
|
||||
public update(): void {
|
||||
this._isUpdating = true;
|
||||
|
||||
|
||||
try {
|
||||
for (let i = 0; i < this.buffer.length; i++) {
|
||||
const entity = this.buffer[i];
|
||||
if (entity.enabled && !entity.isDestroyed) {
|
||||
entity.update();
|
||||
// 只有启用实体直接更新时才遍历更新实体
|
||||
if (this._enableEntityDirectUpdate) {
|
||||
for (let i = 0; i < this.buffer.length; i++) {
|
||||
const entity = this.buffer[i];
|
||||
if (entity.enabled && !entity.isDestroyed) {
|
||||
entity.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this._isUpdating = false;
|
||||
}
|
||||
|
||||
|
||||
// 处理延迟操作
|
||||
this.updateLists();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user