Add behavior modular

This commit is contained in:
MrKBear 2022-02-17 22:02:16 +08:00
parent 64e22eb3f6
commit a3791eecd1
2 changed files with 38 additions and 1 deletions

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> {
/**

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;