From 571e80d5421c2c043488ddd2e7fc991c9fad1bcc Mon Sep 17 00:00:00 2001 From: MrKBear Date: Tue, 10 May 2022 14:55:45 +0800 Subject: [PATCH] Fix boundary logic & Add wastage behavior --- source/Behavior/Behavior.ts | 2 + source/Behavior/BoundaryConstraint.ts | 14 ++-- source/Behavior/ContactAssimilate.ts | 22 ++++-- source/Behavior/Wastage.ts | 100 ++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 source/Behavior/Wastage.ts diff --git a/source/Behavior/Behavior.ts b/source/Behavior/Behavior.ts index bc06b78..147427e 100644 --- a/source/Behavior/Behavior.ts +++ b/source/Behavior/Behavior.ts @@ -11,6 +11,7 @@ import { Avoidance } from "@Behavior/Avoidance"; import { DirectionCluster } from "@Behavior/DirectionCluster"; import { CentralCluster } from "@Behavior/CentralCluster"; import { Manufacture } from "@Behavior/Manufacture"; +import { Wastage } from "@Behavior/Wastage"; const AllBehaviors: IAnyBehaviorRecorder[] = [ new BehaviorRecorder(Template), @@ -25,6 +26,7 @@ const AllBehaviors: IAnyBehaviorRecorder[] = [ new BehaviorRecorder(DirectionCluster), new BehaviorRecorder(CentralCluster), new BehaviorRecorder(Manufacture), + new BehaviorRecorder(Wastage), ] /** diff --git a/source/Behavior/BoundaryConstraint.ts b/source/Behavior/BoundaryConstraint.ts index d77b3d5..2d2aee3 100644 --- a/source/Behavior/BoundaryConstraint.ts +++ b/source/Behavior/BoundaryConstraint.ts @@ -48,11 +48,17 @@ class BoundaryConstraint extends Behavior { - let assimilateGroup = this.parameter.assimilate.objects; - if (!assimilateGroup) return; - for (let i = 0; i < this.parameter.target.objects.length; i++) { const targetGroup = this.parameter.target.objects[i]; @@ -50,7 +49,18 @@ class ContactAssimilate extends Behavior { + + public override behaviorId: string = "Wastage"; + + public override behaviorName: string = "$Title"; + + public override iconName: string = "BackgroundColor"; + + public override describe: string = "$Intro"; + + public override category: string = "$Initiative"; + + public override parameterOption = { + key: { type: "string", name: "$Key" }, + value: { type: "number", name: "$Value", defaultValue: 100, numberStep: 1 }, + speed: { type: "number", name: "$Speed", defaultValue: 1, numberStep: .1 }, + threshold: { type: "number", name: "$Threshold", defaultValue: 0, numberStep: 1 }, + kill: { type: "boolean", name: "$Kill", defaultValue: true }, + assimilate: { type: "CG", name: "$Assimilate", condition: { key: "kill", value: false } } + }; + + public effect = (individual: Individual, group: Group, model: Model, t: number): void => { + const key = this.parameter.key; + if (!key) return; + + let currentValue = individual.getData(`Wastage.${key}`) ?? this.parameter.value; + currentValue -= this.parameter.speed * t; + + // 超过阈值 + if (currentValue < this.parameter.threshold) { + + currentValue = undefined; + + // 杀死个体 + if (this.parameter.kill) { + individual.die(); + } + + // 开启同化 + else if (this.parameter.assimilate.objects) { + individual.transfer(this.parameter.assimilate.objects); + } + } + + individual.setData(`Wastage.${key}`, currentValue); + } + + public override terms: Record> = { + "$Title": { + "ZH_CN": "流逝", + "EN_US": "Wastage" + }, + "$Intro": { + "ZH_CN": "随着时间流逝", + "EN_US": "As time goes by" + }, + "$Key": { + "ZH_CN": "元数据", + "EN_US": "Metadata" + }, + "$Value": { + "ZH_CN": "默认数值", + "EN_US": "Default value" + }, + "$Speed": { + "ZH_CN": "流逝速度 (c/s)", + "EN_US": "Passing speed (c/s)" + }, + "$Threshold": { + "ZH_CN": "阈值", + "EN_US": "Threshold" + }, + "$Kill": { + "ZH_CN": "死亡", + "EN_US": "Death" + }, + "$Assimilate": { + "ZH_CN": "同化", + "EN_US": "Assimilate" + } + }; +} + +export { Wastage }; \ No newline at end of file