Add label modular

This commit is contained in:
2022-02-18 14:14:38 +08:00
parent b9ffe91f8f
commit c1dde87e30
6 changed files with 96 additions and 4 deletions
View File
View File
+2 -1
View File
@@ -2,6 +2,7 @@ import { IAnyObject } from "./Renderer";
import { Emitter, EventType } from "./Emitter"; import { Emitter, EventType } from "./Emitter";
import type { Individual } from "./Individual"; import type { Individual } from "./Individual";
import type { Group } from "./Group"; import type { Group } from "./Group";
import type { Model } from "./Model";
/** /**
* 群体的某种行为 * 群体的某种行为
@@ -43,7 +44,7 @@ abstract class Behavior<
* @param group 影响组 * @param group 影响组
* @param t 经过时间 * @param t 经过时间
*/ */
abstract effect(individual: Individual, group: Group, t: number): void; abstract effect(individual: Individual, group: Group, model: Model, t: number): void;
} }
+3 -2
View File
@@ -1,5 +1,6 @@
import { Individual } from "./Individual"; import { Individual } from "./Individual";
import type { Behavior } from "./Behavior"; import type { Behavior } from "./Behavior";
import type { Model } from "./Model";
/** /**
* 群体类型 * 群体类型
@@ -110,10 +111,10 @@ class Group {
* 执行行为影响 * 执行行为影响
* @param * @param
*/ */
public runner(t: number): void { public runner(model: Model, t: number): void {
this.individuals.forEach((individual) => { this.individuals.forEach((individual) => {
for(let j = 0; j < this.behaviors.length; j++) { for(let j = 0; j < this.behaviors.length; j++) {
this.behaviors[j].effect(individual, this, t); this.behaviors[j].effect(individual, this, model, t);
} }
}); });
} }
+82
View File
@@ -0,0 +1,82 @@
/**
* 数据标签
*/
class Label {
/**
* 唯一标识符
*/
public id: string;
/**
* 用户定义的名称
*/
public name?: string;
/**
* CSS 颜色
*/
public color?: string;
/**
* 构造器
* @param id 标签 ID
* @param name 用户定义的名称
*/
public constructor(id: string, name?: string) {
this.id = id;
this.name = name;
}
/**
* 判断是否为相同标签
*/
public equal(label: Label): boolean {
return this === label || this.id === label.id;
}
}
/**
* 可以被打标签的数据
*/
class LabelObject {
/**
* 标签集合
*/
private labels: Set<Label> = new Set();
/**
* 获取全部 Label
*/
public allLabels(): Label[] {
return Array.from(this.labels);
}
/**
* 添加标签
*/
public addLabel(label: Label): this {
this.labels.add(label);
return this;
}
/**
* 移除标签
*/
public removeLabel(label: Label): this {
this.labels.delete(label);
return this;
}
/**
* 是否存在标签
*/
public hasLabel(label: Label): boolean {
let has = false;
this.labels.forEach((localLabel) => {
if (localLabel.equal(label)) has = true;
});
return has;
}
}
+9 -1
View File
@@ -3,10 +3,18 @@ import { Individual } from "./Individual";
import { Group } from "./Group"; import { Group } from "./Group";
import { Emitter, EventType, EventMixin } from "./Emitter"; import { Emitter, EventType, EventMixin } from "./Emitter";
/**
* 模型 全局控制器
*/
class Model extends Emitter<{}> {
}
export { export {
Individual, Individual,
Group, Group,
Emitter, Emitter,
EventType, EventType,
EventMixin EventMixin,
Model
} }