Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

private void ActivateCardEffect() { // 遍历效果列表以检查卡牌是否具有这些效果 foreach (CardKeyWordType…

  1. User

    private void ActivateCardEffect() { // 遍历效果列表以检查卡牌是否具有这些效果 foreach (CardKeyWordType effect in m_EffectList) { if (cardObject.HasEffect(effect)) { cardEffect.gameObject.SetActive(true); // 卡牌具有效果,激活游戏对象 UpdateCardEffectRender(effect); // 更新卡牌效果的渲染 break; // 找到一个匹配的效果后就退出循环,因为我们只需要更新一次 } } // 遍历效果列表以检查卡牌是否具有这些效果 foreach (CardKeyWordType effect in m_PermanentEffectList) { if (cardObject.HasEffect(effect)) { cardEffect.gameObject.SetActive(true); UpdateCardEffectRender("Permanent"); break; } }

            if (cardObject.HasEffect(CardKeyWordType.Degrade))
            {
                Debug.Log("ActivateCardEffect Degrade");
                // 本地玩家才能看到
                if (IsLocalPlayer)
                {
                    cardEffect.gameObject.SetActive(true);
                    UpdateCardEffectRender(CardKeyWordType.Fragile);
                }
                else
                {
                    // 遍历效果列表以检查卡牌是否具有这些效果
                    foreach (CardKeyWordType effect in m_EffectList)
                    {
                        if (cardObject.HasEffect(effect))
                        {
                            cardEffect.gameObject.SetActive(true); // 卡牌具有效果,激活游戏对象
                            UpdateCardEffectRender(effect); // 更新卡牌效果的渲染
                            break; // 找到一个匹配的效果后就退出循环,因为我们只需要更新一次
                        }
                    }
                    // 遍历效果列表以检查卡牌是否具有这些效果
                    foreach (CardKeyWordType effect in m_PermanentEffectList)
                    {
                        if (cardObject.HasEffect(effect))
                        {
                            cardEffect.gameObject.SetActive(true);
                            UpdateCardEffectRender("Permanent");
                            break;
                        }
                    }
                }
            }
    
            if (cardObject.HasEffect(CardKeyWordType.HolyShield))
            {
                m_shieldIcon.SetActive(true);
            }
        } 请帮忙优化一下这段代码
    
  2. Assistant

    当然,我可以帮助优化这段代码。当前你的代码中有不少重复的逻辑,我们可以通过提取公共部分来减少重复代码,使其更加清晰和紧凑。

    下面是优化后的代码:

    private void ActivateCardEffect()
    {
        bool effectFound = false;
    
        // 检查普通效果列表和永久效果列表,激活相应的效果并更新渲染
        effectFound = CheckAndActivateEffect(m_EffectList) || CheckAndActivateEffect(m_PermanentEffectList, "Permanent");
    
        // 检查是否具有关联降解效果,如果是本地玩家,显示Fragile效果,否则继续检查普通效果和永久效果
        if (cardObject.HasEffect(CardKeyWordType.Degrade))
        {
            Debug.Log("ActivateCardEffect Degrade");
    
            if (IsLocalPlayer)
            {
                cardEffect.gameObject.SetActive(true);
                UpdateCardEffectRender(CardKeyWordType.Fragile);
            }
            else if (!effectFound)
            {
                effectFound = CheckAndActivateEffect(m_EffectList) || CheckAndActivateEffect(m_PermanentEffectList, "Permanent");
            }
        }
        
        // 检查是否具有关联神圣护盾效果
        if (cardObject.HasEffect(CardKeyWordType.HolyShield))
        {
            m_shieldIcon.SetActive(true);
        }
    }
    
    private bool CheckAndActivateEffect(List<CardKeyWordType> effectList, string renderType = null)
    {
        foreach (CardKeyWordType effect in effectList)
        {
            if (cardObject.HasEffect(effect))
            {
                cardEffect.gameObject.SetActive(true);
                UpdateCardEffectRender(renderType ?? effect.ToString());
                return true;
            }
        }
        return false;
    }
    

    主要优化点:

    1. 提取公共函数:将遍历效果列表的逻辑提取到一个名为 CheckAndActivateEffect 的函数中,减少代码重复。
    2. 简化逻辑:使用 || 操作符进行短路求值避免重复的效果检查。
    3. 减少重复:减少对 cardEffect.gameObject.SetActive(true); 和 UpdateCardEffectRender 的重复调用。

    这段代码现在更加清晰、易读,也更便于以后维护和扩展。