diff --git a/source/Model/Behavior.ts b/source/Model/Behavior.ts index 4f03c62..d0186f6 100644 --- a/source/Model/Behavior.ts +++ b/source/Model/Behavior.ts @@ -7,7 +7,8 @@ import type { Group } from "./Group"; * 群体的某种行为 */ abstract class Behavior< - P extends IAnyObject, E extends Record + P extends IAnyObject = {}, + E extends Record = {} > extends Emitter { /** diff --git a/source/Model/Group.ts b/source/Model/Group.ts index bc68c48..fdcb58e 100644 --- a/source/Model/Group.ts +++ b/source/Model/Group.ts @@ -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;