Compare commits

...

5 Commits

11 changed files with 111 additions and 37 deletions

View File

@ -1,13 +1,13 @@
import { BehaviorRecorder, IAnyBehaviorRecorder } from "@Model/Behavior";
import { Template } from "@Behavior/Template";
import { Dynamics } from "@Behavior/Dynamics";
import { PhysicsDynamics } from "@Behavior/PhysicsDynamics";
import { Brownian } from "@Behavior/Brownian";
import { BoundaryConstraint } from "@Behavior/BoundaryConstraint";
import { Tracking } from "@Behavior/Tracking";
const AllBehaviors: IAnyBehaviorRecorder[] = [
new BehaviorRecorder(Template),
new BehaviorRecorder(Dynamics),
new BehaviorRecorder(PhysicsDynamics),
new BehaviorRecorder(Brownian),
new BehaviorRecorder(BoundaryConstraint),
new BehaviorRecorder(Tracking),

View File

@ -17,7 +17,7 @@ class BoundaryConstraint extends Behavior<IBoundaryConstraintBehaviorParameter,
public override behaviorName: string = "$Title";
public override iconName: string = "Running";
public override iconName: string = "Quantity";
public override describe: string = "$Intro";

View File

@ -18,7 +18,7 @@ class Brownian extends Behavior<IBrownianBehaviorParameter, IBrownianBehaviorEve
public override behaviorName: string = "$Title";
public override iconName: string = "Running";
public override iconName: string = "ScatterChart";
public override describe: string = "$Intro";

View File

@ -3,22 +3,23 @@ import Group from "@Model/Group";
import Individual from "@Model/Individual";
import { Model } from "@Model/Model";
type IDynamicsBehaviorParameter = {
type IPhysicsDynamicsBehaviorParameter = {
mass: "number",
maxAcceleration: "number",
maxVelocity: "number",
resistance: "number"
resistance: "number",
limit: "boolean"
}
type IDynamicsBehaviorEvent = {}
type IPhysicsDynamicsBehaviorEvent = {}
class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEvent> {
class PhysicsDynamics extends Behavior<IPhysicsDynamicsBehaviorParameter, IPhysicsDynamicsBehaviorEvent> {
public override behaviorId: string = "Dynamics";
public override behaviorId: string = "PhysicsDynamics";
public override behaviorName: string = "$Title";
public override iconName: string = "Running";
public override iconName: string = "SliderHandleSize";
public override describe: string = "$Intro";
@ -26,9 +27,16 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
public override parameterOption = {
mass: { name: "$Mass", type: "number", defaultValue: 1, numberStep: .01, numberMin: .001 },
maxAcceleration: { name: "$Max.Acceleration", type: "number", defaultValue: 5, numberStep: .1, numberMin: 0 },
maxVelocity: { name: "$Max.Velocity", type: "number", defaultValue: 10, numberStep: .1, numberMin: 0 },
resistance: { name: "$Resistance", type: "number", defaultValue: 0.5, numberStep: .1, numberMin: 0 }
resistance: { name: "$Resistance", type: "number", defaultValue: 0.5, numberStep: .1, numberMin: 0 },
limit: { name: "$Limit", type: "boolean", defaultValue: true },
maxAcceleration: {
name: "$Max.Acceleration", type: "number", defaultValue: 5,
numberStep: .1, numberMin: 0, condition: { key: "limit", value: true }
},
maxVelocity: {
name: "$Max.Velocity", type: "number", defaultValue: 10,
numberStep: .1, numberMin: 0, condition: { key: "limit", value: true }
},
};
public override finalEffect = (individual: Individual, group: Group, model: Model, t: number): void => {
@ -54,11 +62,13 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
individual.acceleration[2] = individual.force[2] / this.parameter.mass;
// 加速度约束
const lengthA = individual.vectorLength(individual.acceleration);
if (lengthA > this.parameter.maxAcceleration) {
individual.acceleration[0] = individual.acceleration[0] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[1] = individual.acceleration[1] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[2] = individual.acceleration[2] * this.parameter.maxAcceleration / lengthA;
if (this.parameter.limit) {
const lengthA = individual.vectorLength(individual.acceleration);
if (lengthA > this.parameter.maxAcceleration) {
individual.acceleration[0] = individual.acceleration[0] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[1] = individual.acceleration[1] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[2] = individual.acceleration[2] * this.parameter.maxAcceleration / lengthA;
}
}
// 计算速度
@ -67,11 +77,13 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
individual.velocity[2] = individual.velocity[2] + individual.acceleration[2] * t;
// 速度约束
const lengthV = individual.vectorLength(individual.velocity);
if (lengthV > this.parameter.maxVelocity) {
individual.velocity[0] = individual.velocity[0] * this.parameter.maxVelocity / lengthV;
individual.velocity[1] = individual.velocity[1] * this.parameter.maxVelocity / lengthV;
individual.velocity[2] = individual.velocity[2] * this.parameter.maxVelocity / lengthV;
if (this.parameter.limit) {
const lengthV = individual.vectorLength(individual.velocity);
if (lengthV > this.parameter.maxVelocity) {
individual.velocity[0] = individual.velocity[0] * this.parameter.maxVelocity / lengthV;
individual.velocity[1] = individual.velocity[1] * this.parameter.maxVelocity / lengthV;
individual.velocity[2] = individual.velocity[2] * this.parameter.maxVelocity / lengthV;
}
}
// 应用速度
@ -87,13 +99,17 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
public override terms: Record<string, Record<string, string>> = {
"$Title": {
"ZH_CN": "动力学",
"EN_US": "Dynamics"
"ZH_CN": "物理动力学",
"EN_US": "Physics dynamics"
},
"$Intro": {
"ZH_CN": "一切可以运动物体的必要行为,执行物理法则。",
"EN_US": "All necessary behaviors that can move objects and implement the laws of physics."
"ZH_CN": "一切按照物理规则运动物体的行为, 按照牛顿经典物理运动公式执行。",
"EN_US": "The behavior of all moving objects according to physical rules is carried out according to Newton's classical physical motion formula."
},
"$Limit": {
"ZH_CN": "开启限制",
"EN_US": "Enable limit"
},
"$Mass": {
"ZH_CN": "质量 (Kg)",
"EN_US": "Mass (Kg)"
@ -117,4 +133,4 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
};
}
export { Dynamics };
export { PhysicsDynamics };

View File

@ -16,7 +16,7 @@ class Tracking extends Behavior<ITrackingBehaviorParameter, ITrackingBehaviorEve
public override behaviorName: string = "$Title";
public override iconName: string = "BullseyeTarget";
public override iconName: string = "Bullseye";
public override describe: string = "$Intro";

View File

@ -32,9 +32,15 @@ class Parameter<P extends IParameter> extends Component<IParameterProps<P> & IMi
private renderParameter<K extends keyof P>
(key: K, option: IParameterOptionItem<P[K]>, value: IParamValue<P[K]>): ReactNode {
const language = this.props.setting?.language ?? "EN_US";
const indexKey = `${this.props.key}-${key}`;
// 条件检测
if (option.condition && this.props.value[option.condition.key] !== option.condition.value) {
return <Fragment key={indexKey}/>;
}
const type = option.type;
const language = this.props.setting?.language ?? "EN_US";
let keyI18n: string, keyI18nOption: Record<string, string> | undefined;
// Custom I18N

View File

@ -57,8 +57,11 @@ const EN_US = {
"Popup.Action.No": "Cancel",
"Popup.Action.Objects.Confirm.Title": "Confirm Delete",
"Popup.Action.Objects.Confirm.Delete": "Delete",
"Popup.Action.Objects.Confirm.Restore.Title": "Confirm Restore",
"Popup.Action.Objects.Confirm.Restore": "Restore",
"Popup.Delete.Objects.Confirm": "Are you sure you want to delete this object(s)? The object is deleted and cannot be recalled.",
"Popup.Delete.Behavior.Confirm": "Are you sure you want to delete this behavior? The behavior is deleted and cannot be recalled.",
"Popup.Restore.Behavior.Confirm": "Are you sure you want to reset all parameters of this behavior? This operation cannot be recalled.",
"Popup.Setting.Title": "Preferences setting",
"Popup.Add.Behavior.Title": "Add behavior",
"Popup.Add.Behavior.Action.Add": "Add all select behavior",
@ -108,6 +111,7 @@ const EN_US = {
"Common.Attr.Key.Generation.Error.Invalid.Label": "The specified label has expired",
"Common.Attr.Key.Kill.Random": "Random kill",
"Common.Attr.Key.Kill.Count": "Kill count",
"Common.Attr.Key.Behavior.Restore": "Restore default parameters",
"Common.Render.Attr.Key.Display.Shape": "Display Shape",
"Common.Render.Attr.Key.Display.Shape.Square": "Square",
"Common.Render.Attr.Key.Display.Shape.Hollow.Square": "Hollow square",

View File

@ -57,13 +57,16 @@ const ZH_CN = {
"Popup.Action.No": "取消",
"Popup.Action.Objects.Confirm.Title": "删除确认",
"Popup.Action.Objects.Confirm.Delete": "删除",
"Popup.Action.Objects.Confirm.Restore.Title": "重置确认",
"Popup.Action.Objects.Confirm.Restore": "重置",
"Popup.Delete.Objects.Confirm": "你确定要删除这个(些)对象吗?对象被删除将无法撤回。",
"Popup.Delete.Behavior.Confirm": "你确定要删除这个行为吗?行为被删除将无法撤回。",
"Popup.Restore.Behavior.Confirm": "你确定要重置此行为的全部参数吗?此操作无法撤回。",
"Popup.Setting.Title": "首选项设置",
"Popup.Add.Behavior.Title": "添加行为",
"Popup.Add.Behavior.Action.Add": "添加全部选中行为",
"Popup.Add.Behavior.Select.Counter": "找不到名为 \"{name}\" 的行为",
"Popup.Add.Behavior.Select.Nodata": "Could not find behavior named \"{name}\"",
"Popup.Add.Behavior.Select.Counter": "已选择 {count} 个行为",
"Popup.Add.Behavior.Select.Nodata": "找不到名为 \"{name}\" 的行为",
"Popup.Behavior.Info.Title": "行为详情: {behavior}",
"Popup.Behavior.Info.Confirm": "好的, 我知道了",
"Build.In.Label.Name.All.Group": "全部群",
@ -108,6 +111,7 @@ const ZH_CN = {
"Common.Attr.Key.Generation.Error.Invalid.Label": "指定的标签已失效",
"Common.Attr.Key.Kill.Random": "随机消除",
"Common.Attr.Key.Kill.Count": "消除数量",
"Common.Attr.Key.Behavior.Restore": "还原默认参数",
"Common.Render.Attr.Key.Display.Shape": "显示形状",
"Common.Render.Attr.Key.Display.Shape.Square": "方形",
"Common.Render.Attr.Key.Display.Shape.Hollow.Square": "空心方形",

View File

@ -115,6 +115,11 @@ interface IParameterOptionItem<T extends IParamType = IParamType> {
*/
colorNormal?: boolean;
/**
*
*/
condition?: {key: string, value: any};
/**
*
*/

View File

@ -111,10 +111,10 @@ class SimulatorWeb extends Component {
let template = this.status.model.addBehavior(getBehaviorById("Template"));
template.name = "Template"; template.color = [150, 20, 220];
let dynamicFish = this.status.model.addBehavior(getBehaviorById("Dynamics"));
let dynamicFish = this.status.model.addBehavior(getBehaviorById("PhysicsDynamics"));
dynamicFish.name = "Dynamic Fish"; dynamicFish.color = [250, 200, 80];
let dynamicShark = this.status.model.addBehavior(getBehaviorById("Dynamics"));
let dynamicShark = this.status.model.addBehavior(getBehaviorById("PhysicsDynamics"));
dynamicShark.name = "Dynamic Shark"; dynamicShark.color = [250, 200, 80];
let brownian = this.status.model.addBehavior(getBehaviorById("Brownian"));

View File

@ -1,6 +1,7 @@
import { Component, ReactNode} from "react";
import { useSettingWithEvent, IMixinSettingProps } from "@Context/Setting";
import { useStatusWithEvent, IMixinStatusProps } from "@Context/Status";
import { getDefaultValue } from "@Model/Parameter";
import { IAnyBehavior } from "@Model/Behavior";
import { Message } from "@Input/Message/Message";
import { AttrInput } from "@Input/AttrInput/AttrInput";
@ -12,9 +13,17 @@ import "./BehaviorDetails.scss";
interface IBehaviorDetailsProps {}
interface IBehaviorDetailsState {
updateId: number;
}
@useSettingWithEvent("language")
@useStatusWithEvent("focusBehaviorChange", "behaviorAttrChange")
class BehaviorDetails extends Component<IBehaviorDetailsProps & IMixinStatusProps & IMixinSettingProps> {
class BehaviorDetails extends Component<IBehaviorDetailsProps & IMixinStatusProps & IMixinSettingProps, IBehaviorDetailsState> {
public state: Readonly<IBehaviorDetailsState> = {
updateId: 1
};
private handelDeleteBehavior = (behavior: IAnyBehavior) => {
if (this.props.status) {
@ -32,6 +41,28 @@ class BehaviorDetails extends Component<IBehaviorDetailsProps & IMixinStatusProp
}
}
private handelRestoreBehavior = (behavior: IAnyBehavior) => {
if (this.props.status) {
const status = this.props.status;
status.popup.showPopup(ConfirmPopup, {
infoI18n: "Popup.Restore.Behavior.Confirm",
titleI18N: "Popup.Action.Objects.Confirm.Restore.Title",
yesI18n: "Popup.Action.Objects.Confirm.Restore",
red: "yes",
yes: () => {
status.changeBehaviorAttrib(
behavior.id, "parameter",
getDefaultValue(behavior.parameterOption) as any,
true
);
this.setState({
updateId: this.state.updateId + 1
});
}
})
}
}
private renderFrom(behavior: IAnyBehavior): ReactNode {
return <>
@ -61,8 +92,16 @@ class BehaviorDetails extends Component<IBehaviorDetailsProps & IMixinStatusProp
}}
/>
<TogglesInput
keyI18n="Common.Attr.Key.Behavior.Restore" red
onIconName="ReplyAll" offIconName="ReplyAll"
valueChange={() => {
this.handelRestoreBehavior(behavior)
}}
/>
<Parameter
key={behavior.id}
key={`${behavior.id}-${this.state.updateId}`}
option={behavior.parameterOption}
value={behavior.parameter}
i18n={(name, language) => behavior.getTerms(name, language)}