Add label modular

This commit is contained in:
MrKBear 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

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;
} }

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
source/Model/Label.ts Normal file
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;
}
}

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
} }