Add brownian behavior

This commit is contained in:
MrKBear 2022-03-30 17:19:45 +08:00
parent e5ae05e737
commit 17ee906f90
5 changed files with 130 additions and 3 deletions

View File

@ -1,9 +1,11 @@
import { BehaviorRecorder, IAnyBehaviorRecorder } from "@Model/Behavior";
import { Template } from "./Template";
import { Dynamics } from "./Dynamics";
import { Brownian } from "./Brownian";
const AllBehaviors: IAnyBehaviorRecorder[] = [
new BehaviorRecorder(Dynamics)
new BehaviorRecorder(Dynamics),
new BehaviorRecorder(Brownian)
]
/**

109
source/Behavior/Brownian.ts Normal file
View File

@ -0,0 +1,109 @@
import { Behavior } from "@Model/Behavior";
import { Group } from "@Model/Group";
import { Individual } from "@Model/Individual";
import { Model } from "@Model/Model";
type IBrownianBehaviorParameter = {
maxFrequency: "number",
minFrequency: "number",
maxStrength: "number",
minStrength: "number"
}
type IBrownianBehaviorEvent = {}
class Brownian extends Behavior<IBrownianBehaviorParameter, IBrownianBehaviorEvent> {
public override behaviorId: string = "Brownian";
public override behaviorName: string = "$Title";
public override iconName: string = "Running";
public override describe: string = "$Intro";
public override category: string = "$Physics";
public override parameterOption = {
maxFrequency: {
type: "number",
name: "$Max.Frequency",
defaultValue: 5,
numberStep: .1,
numberMin: 0
},
minFrequency: {
type: "number",
name: "$Min.Frequency",
defaultValue: 0,
numberStep: .1,
numberMin: 0
},
maxStrength: {
type: "number",
name: "$Max.Strength",
defaultValue: .1,
numberStep: .01,
numberMin: 0
},
minStrength: {
type: "number",
name: "$Min.Strength",
defaultValue: 0,
numberStep: .01,
numberMin: 0
}
};
public override terms: Record<string, Record<string, string>> = {
"$Title": {
"ZH_CN": "布朗运动",
"EN_US": "Brownian motion"
},
"$Intro": {
"ZH_CN": "一种无规则的随机运动",
"EN_US": "An irregular random motion"
},
"$Max.Frequency": {
"ZH_CN": "最大频率",
"EN_US": "Maximum frequency"
},
"$Min.Frequency": {
"ZH_CN": "最小频率",
"EN_US": "Minimum frequency"
},
"$Max.Strength": {
"ZH_CN": "最大强度",
"EN_US": "Maximum strength"
},
"$Min.Strength": {
"ZH_CN": "最小强度",
"EN_US": "Minimum strength"
}
};
public effect(individual: Individual, group: Group, model: Model, t: number): void {
const {maxFrequency, minFrequency, maxStrength, minStrength} = this.parameter;
let nextTime = individual.getData("Brownian.nextTime") ??
minFrequency + Math.random() * (maxFrequency - minFrequency);
let currentTime = individual.getData("Brownian.currentTime") ?? 0;
currentTime += t;
if (currentTime > nextTime) {
individual.applyForce(
minStrength + (Math.random() * 2 - 1) * (maxStrength - minStrength),
minStrength + (Math.random() * 2 - 1) * (maxStrength - minStrength),
minStrength + (Math.random() * 2 - 1) * (maxStrength - minStrength)
);
nextTime = minFrequency + Math.random() * (maxFrequency - minFrequency);
currentTime = 0;
}
individual.setData("Brownian.nextTime", nextTime);
individual.setData("Brownian.currentTime", currentTime);
}
}
export { Brownian };

View File

@ -1,4 +1,7 @@
import { Behavior } from "@Model/Behavior";
import { Group } from "@Model/Group";
import { Individual } from "@Model/Individual";
import { Model } from "@Model/Model";
type ITemplateBehaviorParameter = {
@ -16,7 +19,9 @@ class Template extends Behavior<ITemplateBehaviorParameter, ITemplateBehaviorEve
public override describe: string = "$Intro";
terms: Record<string, Record<string, string>> = {
public override category: string = "$Category";
public override terms: Record<string, Record<string, string>> = {
"$Title": {
"ZH_CN": "行为",
"EN_US": "Behavior"
@ -26,6 +31,10 @@ class Template extends Behavior<ITemplateBehaviorParameter, ITemplateBehaviorEve
"EN_US": "This is a template behavior"
}
};
public effect(individual: Individual, group: Group, model: Model, t: number): void {
}
}
export { Template };

View File

@ -202,7 +202,7 @@ class BehaviorRecorder<
* ID
*/
public getNextId() {
return `B-${this.behaviorName}-${this.nameIndex ++}`;
return `B-${this.behaviorId}-${this.nameIndex ++}`;
}
/**

View File

@ -10,6 +10,7 @@ import { RootContainer } from "@Component/Container/RootContainer";
import { LayoutDirection } from "@Context/Layout";
import { CommandBar } from "@Component/CommandBar/CommandBar";
import { Popup } from "@Component/Popup/Popup";
import { AllBehaviors } from "@Behavior/Behavior";
import "./SimulatorWeb.scss";
initializeIcons("https://img.mrkbear.com/fabric-cdn-prod_20210407.001/");
@ -54,6 +55,12 @@ class SimulatorWeb extends Component {
this.status.model.update(0);
this.status.newLabel().name = "New Label";
this.status.newLabel().name = "Test Label 01";
let dynamic = this.status.model.addBehavior(AllBehaviors[0]);
dynamic.name = "dynamic";
let brownian = this.status.model.addBehavior(AllBehaviors[1]);
brownian.name = "brownian";
group.addBehavior(dynamic);
group.addBehavior(brownian);
}
(window as any).s = this;