Add behavior modular

This commit is contained in:
2022-02-17 22:02:16 +08:00
parent 64e22eb3f6
commit a3791eecd1
2 changed files with 38 additions and 1 deletions
+2 -1
View File
@@ -7,7 +7,8 @@ import type { Group } from "./Group";
* 群体的某种行为
*/
abstract class Behavior<
P extends IAnyObject, E extends Record<EventType, any>
P extends IAnyObject = {},
E extends Record<EventType, any> = {}
> extends Emitter<E> {
/**
+36
View File
@@ -1,4 +1,5 @@
import { Individual } from "./Individual";
import type { Behavior } from "./Behavior";
/**
* 群体类型
@@ -75,6 +76,41 @@ class Group {
}));
return result;
}
/**
* 行为列表
*/
public behaviors: Behavior[] = [];
/**
* 添加行为
* @param behavior 添加行为
*/
public addBehavior(behavior: Behavior | Behavior[]): this {
if (Array.isArray(behavior)) {
this.behaviors = this.behaviors.concat(behavior);
} else {
this.behaviors.push(behavior);
}
// 按照优先级
this.behaviors = this.behaviors.sort((b1, b2) => {
return (b1.priority ?? 0) - (b2.priority ?? 0)
});
return this;
}
/**
* 运行效果器
* @param
*/
public runner(t: number): void {
this.individuals.forEach((individual) => {
for(let j = 0; j < this.behaviors.length; j++) {
this.behaviors[j].effect(individual, this, t);
}
});
}
}
export default Group;