Skip to content

Commit

Permalink
优化 filterEntities 方法的实现 | 移除无效的系统 | 优化系统更新方法
Browse files Browse the repository at this point in the history
  • Loading branch information
esengine committed May 10, 2023
1 parent 260f61d commit 896ee68
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 46 deletions.
5 changes: 4 additions & 1 deletion source/bin/gs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ declare module gs {
}
}
declare module gs {
/**
* ECS 框架中的系统接口,定义了系统需要实现的方法。
*/
interface ISystem {
update(entities: Entity[]): void;
filterEntities(entities: Entity[]): Entity[];
Expand Down Expand Up @@ -462,7 +465,7 @@ declare module gs {
}
declare module gs {
/**
* 系统管理器
* ECS 框架中的系统管理器类,负责管理系统的注册、注销以及更新。
*/
class SystemManager {
private systems;
Expand Down
71 changes: 39 additions & 32 deletions source/bin/gs.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,12 @@ var gs;
};
System.prototype.filterEntities = function (entities) {
var _this = this;
return entities.filter(function (entity) { return _this.matcher.isInterestedEntity(entity) && _this.entityFilter(entity); });
return entities.reduce(function (filteredEntities, entity) {
if (_this.matcher.isInterestedEntity(entity) && _this.entityFilter(entity)) {
filteredEntities.push(entity);
}
return filteredEntities;
}, []);
};
System.prototype.handleComponentChange = function (entity, added) {
if (this.matcher.isInterestedEntity(entity)) {
Expand Down Expand Up @@ -1163,7 +1168,7 @@ var gs;
var gs;
(function (gs) {
/**
* 系统管理器
* ECS 框架中的系统管理器类,负责管理系统的注册、注销以及更新。
*/
var SystemManager = /** @class */ (function () {
function SystemManager(entityManager) {
Expand All @@ -1178,6 +1183,7 @@ var gs;
* @param system 系统
*/
SystemManager.prototype.registerSystem = function (system) {
var _this = this;
system.onRegister();
this.systems.push(system);
this.systems.sort(function (a, b) { return a.priority - b.priority; });
Expand All @@ -1187,6 +1193,26 @@ var gs;
}
else {
var worker = new Worker(system.workerScript);
worker.onmessage = function (event) {
var e_17, _a;
var updatedEntities = event.data.entities;
try {
for (var updatedEntities_1 = __values(updatedEntities), updatedEntities_1_1 = updatedEntities_1.next(); !updatedEntities_1_1.done; updatedEntities_1_1 = updatedEntities_1.next()) {
var updatedEntityData = updatedEntities_1_1.value;
var entity = _this.entityManager.getEntity(updatedEntityData.id);
if (entity) {
entity.deserialize(updatedEntityData);
}
}
}
catch (e_17_1) { e_17 = { error: e_17_1 }; }
finally {
try {
if (updatedEntities_1_1 && !updatedEntities_1_1.done && (_a = updatedEntities_1.return)) _a.call(updatedEntities_1);
}
finally { if (e_17) throw e_17.error; }
}
};
this.systemWorkers.set(system, worker);
}
}
Expand All @@ -1201,47 +1227,49 @@ var gs;
if (index > -1) {
this.systems.splice(index, 1);
}
this.systemWorkers.delete(system);
this.entityCache.delete(system);
};
/**
* 通知所有系统组件已添加
* @param entity
*/
SystemManager.prototype.notifyComponentAdded = function (entity) {
var e_17, _a;
var e_18, _a;
try {
for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
var system = _c.value;
system.handleComponentChange(entity, true);
this.entityCache.delete(system);
}
}
catch (e_17_1) { e_17 = { error: e_17_1 }; }
catch (e_18_1) { e_18 = { error: e_18_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_17) throw e_17.error; }
finally { if (e_18) throw e_18.error; }
}
};
/**
* 通知所有系统组件已删除
* @param entity
*/
SystemManager.prototype.notifyComponentRemoved = function (entity) {
var e_18, _a;
var e_19, _a;
try {
for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
var system = _c.value;
system.handleComponentChange(entity, false);
this.entityCache.delete(system);
}
}
catch (e_18_1) { e_18 = { error: e_18_1 }; }
catch (e_19_1) { e_19 = { error: e_19_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_18) throw e_18.error; }
finally { if (e_19) throw e_19.error; }
}
};
/**
Expand All @@ -1255,8 +1283,7 @@ var gs;
* 更新系统
*/
SystemManager.prototype.update = function () {
var _this = this;
var e_19, _a;
var e_20, _a;
var entities = this.entityManager.getEntities();
try {
for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
Expand All @@ -1275,38 +1302,18 @@ var gs;
entities: filteredEntities.map(function (entity) { return entity.serialize(); }),
};
worker.postMessage(message);
worker.onmessage = function (event) {
var e_20, _a;
var updatedEntities = event.data.entities;
try {
for (var updatedEntities_1 = __values(updatedEntities), updatedEntities_1_1 = updatedEntities_1.next(); !updatedEntities_1_1.done; updatedEntities_1_1 = updatedEntities_1.next()) {
var updatedEntityData = updatedEntities_1_1.value;
var entity = _this.entityManager.getEntity(updatedEntityData.id);
if (entity) {
entity.deserialize(updatedEntityData);
}
}
}
catch (e_20_1) { e_20 = { error: e_20_1 }; }
finally {
try {
if (updatedEntities_1_1 && !updatedEntities_1_1.done && (_a = updatedEntities_1.return)) _a.call(updatedEntities_1);
}
finally { if (e_20) throw e_20.error; }
}
};
}
else {
system.update(filteredEntities);
}
}
}
catch (e_19_1) { e_19 = { error: e_19_1 }; }
catch (e_20_1) { e_20 = { error: e_20_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_19) throw e_19.error; }
finally { if (e_20) throw e_20.error; }
}
};
return SystemManager;
Expand Down
2 changes: 1 addition & 1 deletion source/bin/gs.min.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion source/src/Core/System.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module gs {
/**
* ECS 框架中的系统接口,定义了系统需要实现的方法。
*/
export interface ISystem {
update(entities: Entity[]): void;
filterEntities(entities: Entity[]): Entity[];
Expand Down Expand Up @@ -71,7 +74,12 @@ module gs {
}

public filterEntities(entities: Entity[]): Entity[] {
return entities.filter(entity => this.matcher.isInterestedEntity(entity) && this.entityFilter(entity));
return entities.reduce((filteredEntities, entity) => {
if (this.matcher.isInterestedEntity(entity) && this.entityFilter(entity)) {
filteredEntities.push(entity);
}
return filteredEntities;
}, [] as Entity[]);
}

public handleComponentChange(entity: Entity, added: boolean): void {
Expand Down
23 changes: 12 additions & 11 deletions source/src/Manager/SystemManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module gs {
/**
* 系统管理器
* ECS 框架中的系统管理器类,负责管理系统的注册、注销以及更新。
*/
export class SystemManager {
private systems: System[];
Expand Down Expand Up @@ -30,6 +30,15 @@ module gs {
);
} else {
const worker = new Worker(system.workerScript);
worker.onmessage = (event) => {
const updatedEntities = event.data.entities;
for (const updatedEntityData of updatedEntities) {
const entity = this.entityManager.getEntity(updatedEntityData.id);
if (entity) {
entity.deserialize(updatedEntityData);
}
}
};
this.systemWorkers.set(system, worker);
}
}
Expand All @@ -45,6 +54,8 @@ module gs {
if (index > -1) {
this.systems.splice(index, 1);
}
this.systemWorkers.delete(system);
this.entityCache.delete(system);
}

/**
Expand Down Expand Up @@ -99,16 +110,6 @@ module gs {
entities: filteredEntities.map(entity => entity.serialize()),
};
worker.postMessage(message);

worker.onmessage = (event) => {
const updatedEntities = event.data.entities;
for (const updatedEntityData of updatedEntities) {
const entity = this.entityManager.getEntity(updatedEntityData.id);
if (entity) {
entity.deserialize(updatedEntityData);
}
}
};
} else {
system.update(filteredEntities);
}
Expand Down

0 comments on commit 896ee68

Please sign in to comment.