对ECS系统进行注释、移除JobSystem

This commit is contained in:
yhh
2023-03-13 17:46:16 +08:00
parent 78079252c9
commit 1adc5f1729
17 changed files with 936 additions and 837 deletions

View File

@@ -1,39 +1,51 @@
module es {
interface IdentityHashMap {
[key: string]: ComponentType;
}
/**
* 组件类型工厂,用于生成和管理组件类型。
* 维护了一个类型映射表,将组件类型与其唯一索引相对应,以便在运行时高效地检查实体是否包含特定的组件类型。
*/
export class ComponentTypeFactory {
private componentTypes_: IdentityHashMap;
/** 组件类型与其唯一索引的映射表 */
private componentTypes: Record<string, ComponentType> = {};
/** 组件类型列表,按索引访问组件类型 */
public readonly types: Bag<ComponentType> = new Bag<ComponentType>();
/** 当前组件类型的计数器 */
private componentTypeCount = 0;
private componentTypeCount_ = 0;
public types: Bag<ComponentType>;
constructor() {
this.componentTypes_ = {};
this.types = new Bag<ComponentType>();
/**
* 获取给定组件类型的唯一索引。
* 如果该组件类型尚未存在于类型映射表中,则创建一个新的组件类型,并将其添加到映射表和类型列表中。
* @param c 要查找或创建的组件类型
* @returns 组件类型的唯一索引
*/
public getIndexFor(c: new (...args: any[]) => any): number {
return this.getTypeFor(c).getIndex();
}
public getTypeFor(c): ComponentType {
if ("number" === typeof c) {
/**
* 获取给定组件类型的ComponentType对象。
* 如果该组件类型尚未存在于类型映射表中则创建一个新的ComponentType对象并将其添加到映射表和类型列表中。
* @param c 要查找或创建的组件类型
* @returns 组件类型的ComponentType对象
*/
public getTypeFor(c: new (...args: any[]) => any): ComponentType {
// 如果给定的组件类型是一个已有的索引则直接返回对应的ComponentType对象
if (typeof c === "number") {
return this.types.get(c);
}
let type: ComponentType = this.componentTypes_[getClassName(c)];
// 获取给定组件类型对应的类名
const className = getClassName(c);
if (type == null) {
const index: number = this.componentTypeCount_++;
type = new ComponentType(c, index);
this.componentTypes_[getClassName(c)] = type;
// 如果类型映射表中不存在该组件类型则创建一个新的ComponentType对象
if (!this.componentTypes[className]) {
const index = this.componentTypeCount++;
const type = new ComponentType(c, index);
this.componentTypes[className] = type;
this.types.set(index, type);
}
return type;
}
public getIndexFor(c): number {
return this.getTypeFor(c).getIndex();
// 返回对应的ComponentType对象
return this.componentTypes[className];
}
}
}

View File

@@ -1,12 +1,28 @@
module es {
/**
* 组件类型管理器,维护了一个组件类型和它们对应的位掩码之间的映射关系。
* 用于实现实体匹配器中组件类型的比较操作,以确定实体是否符合给定的匹配器条件。
*/
export class ComponentTypeManager {
/** 存储组件类型和它们对应的位掩码的Map */
private static _componentTypesMask: Map<any, number> = new Map<any, number>();
/**
* 将给定的组件类型添加到组件类型列表中,并分配一个唯一的位掩码。
* @param type 要添加的组件类型
*/
public static add(type) {
if (!this._componentTypesMask.has(type))
if (!this._componentTypesMask.has(type)) {
this._componentTypesMask.set(type, this._componentTypesMask.size);
}
}
/**
* 获取给定组件类型的位掩码。
* 如果该组件类型还没有分配位掩码,则将其添加到列表中,并分配一个唯一的位掩码。
* @param type 要获取位掩码的组件类型
* @returns 组件类型的位掩码
*/
public static getIndexFor(type) {
let v = -1;
if (!this._componentTypesMask.has(type)) {

View File

@@ -1,4 +1,7 @@
module es {
/**
* 定义一个实体匹配器类。
*/
export class Matcher {
protected allSet: (new (...args: any[]) => Component)[] = [];
protected exclusionSet: (new (...args: any[]) => Component)[] = [];
@@ -25,59 +28,61 @@ module es {
}
public isInterested(components: Bits) {
if (this.allSet.length != 0) {
for (let i = 0, s = this.allSet.length; i < s; ++ i) {
let type = this.allSet[i];
if (!components.get(ComponentTypeManager.getIndexFor(type)))
if (this.allSet.length !== 0) {
for (let i = 0; i < this.allSet.length; i++) {
const type = this.allSet[i];
if (!components.get(ComponentTypeManager.getIndexFor(type))) {
return false;
}
}
}
if (this.exclusionSet.length != 0) {
for (let i = 0, s = this.exclusionSet.length; i < s; ++ i) {
let type = this.exclusionSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type)))
if (this.exclusionSet.length !== 0) {
for (let i = 0; i < this.exclusionSet.length; i++) {
const type = this.exclusionSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type))) {
return false;
}
}
}
if (this.oneSet.length != 0) {
for (let i = 0, s = this.oneSet.length; i < s; ++ i) {
let type = this.oneSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type)))
if (this.oneSet.length !== 0) {
for (let i = 0; i < this.oneSet.length; i++) {
const type = this.oneSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type))) {
return true;
}
}
return false;
}
return true;
}
public all(...types: any[]): Matcher {
let t;
for (let i = 0, s = types.length; i < s; ++ i) {
t = types[i];
this.allSet.push(t);
}
/**
* 添加所有包含的组件类型。
* @param types 所有包含的组件类型列表
*/
public all(...types: (new (...args: any[]) => Component)[]): Matcher {
this.allSet.push(...types);
return this;
}
public exclude(...types: any[]) {
let t;
for (let i = 0, s = types.length; i < s; ++ i) {
t = types[i];
this.exclusionSet.push(t);
}
/**
* 添加排除包含的组件类型。
* @param types 排除包含的组件类型列表
*/
public exclude(...types: (new (...args: any[]) => Component)[]): Matcher {
this.exclusionSet.push(...types);
return this;
}
public one(...types: any[]) {
for (let i = 0, s = types.length; i < s; ++ i) {
const t = types[i];
this.oneSet.push(t);
}
/**
* 添加至少包含其中之一的组件类型。
* @param types 至少包含其中之一的组件类型列表
*/
public one(...types: (new (...args: any[]) => Component)[]): Matcher {
this.oneSet.push(...types);
return this;
}
}

View File

@@ -1,38 +0,0 @@
module es {
/**
* 开辟一个新线程
* 注意:它无法获得主线程中的上下文
*/
export class WorkerUtils {
/** 正在执行的队列 */
private static readonly pendingJobs = {};
private static jobIdGen = 0;
/**
* 创建一个worker
* @param doFunc worker所能做的事情
*/
public static makeWorker(doFunc: Function) {
const worker = new Worker(URL.createObjectURL(new Blob([`(${doFunc.toString()})()`])));
return worker;
}
public static workerMessage(worker: Worker) {
worker.onmessage = ({ data: { result, jobId } }) => {
if (typeof this.pendingJobs[jobId] == 'function')
this.pendingJobs[jobId](result);
delete this.pendingJobs[jobId];
};
return (...message: any[]) => {
return new Promise(resolve => {
const jobId = this.jobIdGen++;
this.pendingJobs[jobId] = resolve;
worker.postMessage({ jobId, message });
});
}
}
}
}