From 30a785eabbd060aa228f26e68486123351daca52 Mon Sep 17 00:00:00 2001 From: MrKBear Date: Fri, 18 Mar 2022 17:31:13 +0800 Subject: [PATCH] Add kill individual api --- source/Localization/EN-US.ts | 3 ++ source/Localization/ZH-CN.ts | 5 ++- source/Model/Group.ts | 41 ++++++++++++++++++++++ source/Panel/GroupDetails/GroupDetails.tsx | 18 ++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/source/Localization/EN-US.ts b/source/Localization/EN-US.ts index b18fb44..a7d5b06 100644 --- a/source/Localization/EN-US.ts +++ b/source/Localization/EN-US.ts @@ -51,6 +51,7 @@ const EN_US = { "Common.Attr.Title.Basic": "Basic properties", "Common.Attr.Title.Spatial": "Spatial property", "Common.Attr.Title.Individual.Generation": "Individual generation", + "Common.Attr.Title.Individual.kill": "Individual kill", "Common.Attr.Key.Display.Name": "Display name", "Common.Attr.Key.Position.X": "Position X", "Common.Attr.Key.Position.Y": "Position Y", @@ -79,6 +80,8 @@ const EN_US = { "Common.Attr.Key.Generation.Error.Empty.Range.List": "The specified label does not contain any scope objects", "Common.Attr.Key.Generation.Error.Invalid.Range": "The specified scope object is invalid", "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", "Panel.Info.Range.Details.Attr.Error.Not.Range": "Object is not a Range", "Panel.Info.Range.Details.Attr.Error.Unspecified": "Unspecified range object", "Panel.Info.Group.Details.Attr.Error.Not.Group": "Object is not a Group", diff --git a/source/Localization/ZH-CN.ts b/source/Localization/ZH-CN.ts index e880dd5..8f2b46a 100644 --- a/source/Localization/ZH-CN.ts +++ b/source/Localization/ZH-CN.ts @@ -50,7 +50,8 @@ const ZH_CN = { "Common.No.Unknown.Error": "未知错误", "Common.Attr.Title.Basic": "基础属性", "Common.Attr.Title.Spatial": "空间属性", - "Common.Attr.Title.Individual.Generation": "个体生成", + "Common.Attr.Title.Individual.Generation": "生成个体", + "Common.Attr.Title.Individual.kill": "消除个体", "Common.Attr.Key.Display.Name": "显示名称", "Common.Attr.Key.Position.X": "X 坐标", "Common.Attr.Key.Position.Y": "Y 坐标", @@ -79,6 +80,8 @@ const ZH_CN = { "Common.Attr.Key.Generation.Error.Empty.Range.List": "指定的标签中没有包含任何范围对象", "Common.Attr.Key.Generation.Error.Invalid.Range": "指定的范围对象已失效", "Common.Attr.Key.Generation.Error.Invalid.Label": "指定的标签已失效", + "Common.Attr.Key.Kill.Random": "随机消除", + "Common.Attr.Key.Kill.Count": "消除数量", "Panel.Info.Range.Details.Attr.Error.Not.Range": "对象不是一个范围", "Panel.Info.Range.Details.Attr.Error.Unspecified": "未指定范围对象", "Panel.Info.Group.Details.Attr.Error.Not.Group": "对象不是一个群", diff --git a/source/Model/Group.ts b/source/Model/Group.ts index 831083f..b536ae5 100644 --- a/source/Model/Group.ts +++ b/source/Model/Group.ts @@ -49,6 +49,11 @@ class Group extends CtrlObject { */ public genErrorMessageShowCount: number = 0; + /** + * 删除个数 + */ + public killCount: number = 1; + private genInSingleRange(count: number, range: Range) { for (let i = 0; i < count; i++) { let individual = new Individual(this); @@ -188,6 +193,42 @@ class Group extends CtrlObject { return success; } + /** + * 随机杀死个体 + */ + public killIndividuals(): boolean { + let success = false; + let killCount = this.killCount; + if (killCount > this.individuals.size) { + killCount = this.individuals.size; + } + + // 生成索引数组 + const allIndex = new Array(this.individuals.size).fill(0).map((_, i) => i); + const deleteIndex: Set = new Set(); + + for (let i = 0; i < killCount; i++) { + let randomIndex = Math.floor(Math.random() * allIndex.length); + deleteIndex.add(allIndex[randomIndex]); + allIndex.splice(randomIndex, 1); + } + + let j = 0; + this.individuals.forEach((individual) => { + if (deleteIndex.has(j)) { + this.remove(individual); + success = true; + } + j++; + }); + + if (success) { + this.model.emit("individualChange", this); + } + + return success + } + /** * 创建个体 * @param count 创建数量 diff --git a/source/Panel/GroupDetails/GroupDetails.tsx b/source/Panel/GroupDetails/GroupDetails.tsx index f38596e..b37221a 100644 --- a/source/Panel/GroupDetails/GroupDetails.tsx +++ b/source/Panel/GroupDetails/GroupDetails.tsx @@ -136,6 +136,24 @@ class GroupDetails extends Component { }} /> + + + { + this.props.status?.changeGroupAttrib(group.id, "killCount", (val as any) / 1); + }} + /> + + { + group.killIndividuals() + }} + /> + }