diff --git a/source/Behavior/Attacking.ts b/source/Behavior/Attacking.ts new file mode 100644 index 0000000..829b360 --- /dev/null +++ b/source/Behavior/Attacking.ts @@ -0,0 +1,79 @@ +import { Behavior } from "@Model/Behavior"; +import { Group } from "@Model/Group"; +import { Individual } from "@Model/Individual"; +import { Model } from "@Model/Model"; + +type IAttackingBehaviorParameter = { + target: "CLG", + success: "number", + range: "number" +} + +type IAttackingBehaviorEvent = {} + +class Attacking extends Behavior { + + public override behaviorId: string = "Attacking"; + + public override behaviorName: string = "$Title"; + + public override iconName: string = "DefenderTVM"; + + public override describe: string = "$Intro"; + + public override category: string = "$Interactive"; + + public override parameterOption = { + target: { type: "CLG", name: "$Target" }, + range: { type: "number", name: "$Range", defaultValue: .05, numberMin: 0, numberStep: .01 }, + success: { type: "number", name: "$Success", defaultValue: 90, numberMin: 0, numberMax: 100, numberStep: 5 } + }; + + public effect = (individual: Individual, group: Group, model: Model, t: number): void => { + + for (let i = 0; i < this.parameter.target.objects.length; i++) { + const targetGroup = this.parameter.target.objects[i]; + + targetGroup.individuals.forEach((targetIndividual) => { + + // 排除自己 + if (targetIndividual === individual) return; + let dis = targetIndividual.distanceTo(individual); + + // 进入攻击范围 + if (dis <= this.parameter.range) { + + // 成功判定 + if (Math.random() * 100 < this.parameter.success) { + targetIndividual.die(); + } + } + }); + } + } + + public override terms: Record> = { + "$Title": { + "ZH_CN": "攻击", + "EN_US": "Attacking" + }, + "$Target": { + "ZH_CN": "攻击目标", + "EN_US": "Attacking target" + }, + "$Range": { + "ZH_CN": "攻击范围 (m)", + "EN_US": "Attacking range (m)" + }, + "$Success": { + "ZH_CN": "成功率 (%)", + "EN_US": "Success rate (%)" + }, + "$Intro": { + "ZH_CN": "攻击进入共进范围的目标群个体", + "EN_US": "Attack the target group and individual entering the range" + } + }; +} + +export { Attacking }; \ No newline at end of file diff --git a/source/Behavior/Behavior.ts b/source/Behavior/Behavior.ts index ecd7798..60b6eff 100644 --- a/source/Behavior/Behavior.ts +++ b/source/Behavior/Behavior.ts @@ -4,6 +4,7 @@ import { PhysicsDynamics } from "@Behavior/PhysicsDynamics"; import { Brownian } from "@Behavior/Brownian"; import { BoundaryConstraint } from "@Behavior/BoundaryConstraint"; import { Tracking } from "@Behavior/Tracking"; +import { Attacking } from "@Behavior/Attacking"; const AllBehaviors: IAnyBehaviorRecorder[] = [ new BehaviorRecorder(Template), @@ -11,6 +12,7 @@ const AllBehaviors: IAnyBehaviorRecorder[] = [ new BehaviorRecorder(Brownian), new BehaviorRecorder(BoundaryConstraint), new BehaviorRecorder(Tracking), + new BehaviorRecorder(Attacking), ] /** diff --git a/source/Behavior/PhysicsDynamics.ts b/source/Behavior/PhysicsDynamics.ts index d061450..994b731 100644 --- a/source/Behavior/PhysicsDynamics.ts +++ b/source/Behavior/PhysicsDynamics.ts @@ -27,14 +27,14 @@ class PhysicsDynamics extends Behavior { this.status.model.updateBehaviorParameter();