简体   繁体   English

如何优化此代码以获得更好的可读性?

[英]How can I optimize this code for better readability?

I'm not a good programmer, for my bodypart-based collision detection in a game in unity I'm making I've ended up with a switch that looks like this despite my best attempts to simplify and shorten it:我不是一个优秀的程序员,因为我在统一的游戏中进行基于身体部位的碰撞检测,我最终得到了一个看起来像这样的开关,尽管我尽了最大的努力来简化和缩短它:

public void GetCollision(Collision2D col) {

    if (attackType == -1) {
        if (col.gameObject.name == "Sword") {
            hitboxDisable();
        } else if (col.gameObject.name == "Player") {
            pim.PlayerDamage(5);
        }
    }

    if (col.gameObject.name == "Player_Body") {
        switch (attackType) {
            case -2: {
                    pim.PlayerDamage(5);
                }
                break;
            case 0:
                if (!pa.playerIsDodging) {
                    pim.PlayerDamage(5);
                } else {
                    pa.dodgeOnCooldown = false;
                    pa.resetDodgeRoutine();
                    hitboxDisable();
                }
                break;
            case 1:
                if (!swordSrc.isDefendingLegRight) {
                    pim.PlayerDamage(5);
                } else {
                    weaponBlocked = true;
                }
                break;
            case 2:
                if (!swordSrc.isDefendingArmRight) {
                    pim.PlayerDamage(5);
                } else {
                    weaponBlocked = true;
                }
                break;
            case 3:
                if (!swordSrc.isDefendingHeadRight) {
                    pim.PlayerDamage(5);
                } else {
                    weaponBlocked = true;
                }
                break;
            case 4:
                if (!swordSrc.isDefendingLegLeft) {
                    pim.PlayerDamage(5);
                } else {
                    weaponBlocked = true;
                }
                break;
            case 5:
                if (!swordSrc.isDefendingArmLeft) {
                    pim.PlayerDamage(5);
                } else {
                    weaponBlocked = true;
                }
                break;
            case 6:
                if (!swordSrc.isDefendingHeadLeft) {
                    pim.PlayerDamage(5);
                } else {
                    weaponBlocked = true;
                }
                break;
        }

        if (weaponBlocked == true) {
            hitboxDisable();
            RandomOpening();
            ApplyForce(testvar1, testvar2);
            weaponBlocked = false;
        }
    }
}

How can this be shortened and optimized for better readability?如何缩短和优化它以获得更好的可读性? I'm new to C# and what little of my programming has been in C, I know there are a lot of ways to improve readability in C# I just don't know when/how to apply them.我是 C# 的新手,我在 C 中的编程很少,我知道有很多方法可以提高 C# 的可读性我只是不知道何时应用它们。 Suggestions would be much appreciated, I'm willing to try and learn anything, I want to try and avoid ending up with big switch statements like this if possible.建议将不胜感激,我愿意尝试学习任何东西,如果可能的话,我想尝试避免以这样的大 switch 语句结束。 Even just a suggestion what to apply here would be really appreciated, an example would be great.即使只是一个建议在这里应用什么也会非常感激,一个例子会很棒。

I made the attackTypes into integers, they could have been strings but I chose not to because to my understanding strings take longer to compare.我将攻击类型变成整数,它们可能是字符串,但我选择不这样做,因为据我了解,比较字符串需要更长的时间。 The attackType value specifies in the switch where the attack is targeting and if/how to block it, then if it was blocked. attackType 值在开关中指定攻击的目标位置以及是否/如何阻止它,然后是否被阻止。

Case 1-6 seem very similar.案例 1-6 看起来非常相似。 You can make use of a local function您可以使用本地 function

public void GetCollision(Collision2D col) {
    // this is a local function which you can only use inside this function
    // which can be useful if you have a short repeating pattern inside the function
    // and don't need it anywhere else
    void _handleHit(bool isDefending) {
        if (isDefending) {
            pim.PlayerDamage(5);
        } else {
            weaponBlocked = true;
        }
    }

    [...] // your code

    switch (attackType) {
        [...] // your code
        case 1: _handleHit(!swordSrc.isDefendingLegRight); break;
        case 2: _handleHit(!swordSrc.isDefendingArmRight); break;
        case 3: _handleHit(!swordSrc.isDefendingHeadRight); break;
        ...
    }
}

You could also take a look at C# enums and replace attackType with a readable version.您还可以查看 C# 枚举并将攻击类型替换为可读版本。

// Declaring the enum
public enum AttackType { Direct, LeftArm, ... }

// Then in a switch case you can do:
switch (attackType) {
    case AttackType.Direct: ...
    case AttackType.LeftArm: ...
}

One thing would be to create a enum to describe the bodyparts, ie一件事是创建一个枚举来描述身体部位,即

public enum Bodypart{
None,
Head,
LeftArm,
RightArm,
LeftLeg,
RightLeg,
}

This could allow you to replace all the isDefendingArmLeft properties with a DefendingBodyPart .这可以让您用DefendingBodyPart替换所有isDefendingArmLeft属性。 Likewise your attacks could be described by a combination of attack type and bodypart, this also removes the need to guess what attackType = -2 means:同样,您的攻击可以通过攻击类型和主体部分的组合来描述,这也消除了猜测攻击类型attackType = -2意味着什么的需要:

public enum Attacktype{
Magic,
Sword,
Bodypart,
}

So that you can check first if the attacker attacks a specific body-part, and if the target is defending that specific body-part.这样您就可以首先检查攻击者是否攻击了特定的身体部位,以及目标是否正在防御该特定的身体部位。 If you want to allow defense or attack of multiple bodyparts you could use a [Flags] enum with one bit per bodypart.如果你想允许对多个身体部位进行防御或攻击,你可以使用一个 [Flags] 枚举,每个身体部位一个位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM