如何重构这个很大的条件

发布于 2025-02-04 14:56:59 字数 980 浏览 2 评论 0原文

如何重构这么大的条件?武器是一个清单,它的游戏对象

如何重新分配这个很大的条件?武器是一个清单,它的gameObject

public class Customanger : MonoBehaviour
{
    public List<GameObject> weapon;
    public static Customanger singleton;
    private void Awake() => singleton = this;
};
if (Customanger.singleton.weapon[1].activeSelf ||
    Customanger.singleton.weapon[2].activeSelf ||
    Customanger.singleton.weapon[3].activeSelf ||
    Customanger.singleton.weapon[4].activeSelf ||
    Customanger.singleton.weapon[5].activeSelf ||
    Customanger.singleton.weapon[8].activeSelf ||
    Customanger.singleton.weapon[10].activeSelf ||
    Customanger.singleton.weapon[12].activeSelf ||
    Customanger.singleton.weapon[13].activeSelf ||
    Customanger.singleton.weapon[14].activeSelf ||
    Customanger.singleton.weapon[15].activeSelf ||
    Customanger.singleton.weapon[16].activeSelf ||
    Customanger.singleton.weapon[17].activeSelf)
{
    dosomething();
}

How to refactoring this big if condition? weapon is a list and it;s gameobject

How to refactoring this big if condition? weapon is a list and it;s gameobject

public class Customanger : MonoBehaviour
{
    public List<GameObject> weapon;
    public static Customanger singleton;
    private void Awake() => singleton = this;
};
if (Customanger.singleton.weapon[1].activeSelf ||
    Customanger.singleton.weapon[2].activeSelf ||
    Customanger.singleton.weapon[3].activeSelf ||
    Customanger.singleton.weapon[4].activeSelf ||
    Customanger.singleton.weapon[5].activeSelf ||
    Customanger.singleton.weapon[8].activeSelf ||
    Customanger.singleton.weapon[10].activeSelf ||
    Customanger.singleton.weapon[12].activeSelf ||
    Customanger.singleton.weapon[13].activeSelf ||
    Customanger.singleton.weapon[14].activeSelf ||
    Customanger.singleton.weapon[15].activeSelf ||
    Customanger.singleton.weapon[16].activeSelf ||
    Customanger.singleton.weapon[17].activeSelf)
{
    dosomething();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

带刺的爱情 2025-02-11 14:56:59

您可以尝试 .any()

确定序列的任何元素是否存在或满足
条件。

//Here I considered length of Weapon array/list is 17
if (Customanger.singleton.weapon.Any(x => x.activeSelf))
{
    dosomething();
}

如果您想检查相同的武器子集,则可以在下面尝试,

var subSetOfWeapons = Customanger.singleton.weapon.GetRange(1, 17);

if (subSetOfWeapons.Any(x => x.activeSelf))
{
    dosomething();
}

有关更多详细信息: list.getrange(int index,int count)

You can try .Any(),

Determines whether any element of a sequence exists or satisfies a
condition.

//Here I considered length of Weapon array/list is 17
if (Customanger.singleton.weapon.Any(x => x.activeSelf))
{
    dosomething();
}

If you would like to check same condition for subset of weapons, then you can try below,

var subSetOfWeapons = Customanger.singleton.weapon.GetRange(1, 17);

if (subSetOfWeapons.Any(x => x.activeSelf))
{
    dosomething();
}

For more details: List.GetRange(int index, int count)

救赎№ 2025-02-11 14:56:59

我不知道您正在使用哪种类型。我所做的是检查哪种模式经常出现。我看到您始终访问武器列表/数组。通常可以迭代索引器。

有些假设是由于信息的不完整而进行的。

您可以为此编写一个循环:

private bool CheckActiveSelf(List<WeaponThing> weapons)
{
    // iterate each weapon
    foreach(var weapon in weapons)
        // when activeSelf, return true
        if(weapon.activeSelf)
            return true;
   
    return false;
}


// instead of passing the Customanger, you should pass the deepest
// level. So when the weapon system is used elsewhere, you can still
// use the method.
if(CheckActiveSelf(Customanger.singleton.weapon))
{
    DoSomething();
}

I don't know which types you are using. What I did is check which pattern is recurrent. I see that you always access the weapon list/array. Normally indexers can be iterated.

Some assumes are made because of the incompleteness of information.

You could write a loop for it:

private bool CheckActiveSelf(List<WeaponThing> weapons)
{
    // iterate each weapon
    foreach(var weapon in weapons)
        // when activeSelf, return true
        if(weapon.activeSelf)
            return true;
   
    return false;
}


// instead of passing the Customanger, you should pass the deepest
// level. So when the weapon system is used elsewhere, you can still
// use the method.
if(CheckActiveSelf(Customanger.singleton.weapon))
{
    DoSomething();
}
忆梦 2025-02-11 14:56:59

无论此检查的含义如何,在OOP世界中,应将其作为实例方法(或属性)在ustomanger class中的实例方法(或属性)实现:

class Customanger
{
    private static int[] activeWeaponIndexes = { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 };
    public bool HasActiveWeapon => activeWeaponIndexes.Any(x => weapon[x].activeSelf);
}

然后,条件将减少到:

if (Customanger.singleton.HasActiveWeapon) { \*do smth*\ }

Whatever this check means, in OOP world it should be implemented as instance method (or property) with meaningful name in Customanger class:

class Customanger
{
    private static int[] activeWeaponIndexes = { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 };
    public bool HasActiveWeapon => activeWeaponIndexes.Any(x => weapon[x].activeSelf);
}

Then condition will be reduced to:

if (Customanger.singleton.HasActiveWeapon) { \*do smth*\ }
煮茶煮酒煮时光 2025-02-11 14:56:59

您可以做这样的事情:

var weaponIds = new List<int>() { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 };

var weapons = weaponIds.Select(x => Customanger.singleton.weapon[x]);

if (weapons.Where(x => x.activeSelf).Any())
{
    DoSomething();
}

也许可以将武器阵列分为类别,您可能会做得更好吗?我无法为您提供更多的代码。

编辑:删除完全不必要的Tolist()

You could do something like that:

var weaponIds = new List<int>() { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 };

var weapons = weaponIds.Select(x => Customanger.singleton.weapon[x]);

if (weapons.Where(x => x.activeSelf).Any())
{
    DoSomething();
}

You could probably do better than that by splitting your weapon arrays into categories maybe? I can't help you more with the code you provided.

EDIT: Removed the completely unecessary ToList()

老子叫无熙 2025-02-11 14:56:59

您是如何确定{1、2、3、4、5、8、10、12、13、13、14、15、16、17}的相关武器指数的?


如果它们都是通过履行某种条件来区分自己与其余武器的武器指数,则可以直接识别相关的武器,而不会打扰这些指数。

例如,您可以使用.where()来滤除相关武器:

Customanger.singleton.weapon
    .Where(weapon => <weapon fulfills condition>)

假设情况是相关武器都有一个共同的特征:它们都属于已加载的场景。 可以将条件定义为

<weapon fulfills condition> = weapon.scene.isLoaded

IEnumerable<GameObject> weaponsInLoadedScenes = Customanger.singleton.weapon
    .Where(weapon => weapon.scene.isLoaded);

然后 相关武器:

if (weaponsInLoadedScenes.Any(weapon => weapon.activeSelf))
{
    DoSomething();
}

Where().yany()都在命名空间 system.linq 中找到。


另一方面,如果相关武器指数不是基于共同武器特征选择的 ,我基本上是指<​​a href =“ https://stackoverflow.com/questions/questions/72486634/72486634/ -to-Refactor-this-if-if条件#72487116“>另一个人的答案。

首先,创建相关武器指数的集合(最好使用以某种方式描述为什么这些是相关武器指数的变量名称):

var relevantWeaponIndices = new List<int>() { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 };

然后,验证条件;首先要明确定义相关武器:

var relevantWeapons = relevantWeaponIndices
    .Select(index => Customanger.singleton.weapon[index]);

if (relevantWeapons.Any(weapon => weapon.activeSelf))
{
    DoSomething();
}

或者通过获取相关武器内联武器:

if (relevantWeaponIndices.Any(index => Customanger.singleton.weapon[index].activeSelf))
{
    DoSomething();
}

.select().any()()在命名空间系统中找到.linq

How did you decide that specifically { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 } are the relevant weapon indices?


If they are all indices of weapons that distinguish themselves from the remaining weapons by fulfilling a certain condition, you could identify the relevant weapons directly and not bother with the indices.

As an example, you could use use .Where() to filter out the relevant weapons:

Customanger.singleton.weapon
    .Where(weapon => <weapon fulfills condition>)

Let's say the condition is that the relevant weapons all have a common trait: they all belong to scenes that have been loaded. The condition can then be defined as:

<weapon fulfills condition> = weapon.scene.isLoaded

, and you can filter your relevant weapons as follows:

IEnumerable<GameObject> weaponsInLoadedScenes = Customanger.singleton.weapon
    .Where(weapon => weapon.scene.isLoaded);

Now, you can use .Any() to perform your original if check on these relevant weapons:

if (weaponsInLoadedScenes.Any(weapon => weapon.activeSelf))
{
    DoSomething();
}

Both .Where() and .Any() are found in the namespace System.Linq.


If, on the other hand, the relevant weapon indices are not selected based on common weapon traits, I would basically refer to Another One's answer.

First, create a collection of the relevant weapon indices (preferably using a variable name that somehow describes why these are relevant weapon indices):

var relevantWeaponIndices = new List<int>() { 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 };

Then, verify conditions; either by first explicitly defining the relevant weapons:

var relevantWeapons = relevantWeaponIndices
    .Select(index => Customanger.singleton.weapon[index]);

if (relevantWeapons.Any(weapon => weapon.activeSelf))
{
    DoSomething();
}

or by getting the relevant weapons inline:

if (relevantWeaponIndices.Any(index => Customanger.singleton.weapon[index].activeSelf))
{
    DoSomething();
}

Both .Select() and .Any() are found in the namespace System.Linq.

哑剧 2025-02-11 14:56:59

有多种方法可以这样做,您可以包括或排除收藏中的项目,而不是检查“ ActiveSelf”标志
前任:

List<GameObject> excludedList = new List<GameObject>() { 
Customanger.singleton.weapon[1], 
Customanger.singleton.weapon[3], 
Customanger.singleton.weapon[5]};
if (Customanger.singleton.weapon.Except(excludedList).Any(x => x.activeSelf))
    dosomething();
    ```

There are multiple ways to do it, you can include or exclude items from your collection and than check for the "activeSelf" flag
ex:

List<GameObject> excludedList = new List<GameObject>() { 
Customanger.singleton.weapon[1], 
Customanger.singleton.weapon[3], 
Customanger.singleton.weapon[5]};
if (Customanger.singleton.weapon.Except(excludedList).Any(x => x.activeSelf))
    dosomething();
    ```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文