简体   繁体   English

在 C# unity 中使用多态或接口

[英]Using polymorphism or Interfaces in C# unity

In my game in unity C#, I have two classes one is a PlayerHealth and the other is EnemyHealth they have the same functions but these two fields are inverted currentHapinessPower and currentSadnesPower .在我的统一 C# 游戏中,我有两个类,一个是PlayerHealth ,另一个是EnemyHealth它们具有相同的功能,但这两个字段是倒置的currentHapinessPowercurrentSadnesPower I don't know if i should use the polymorphism principle or inheritance or interfaces.我不知道我是否应该使用多态原则或继承或接口。

These are the classes:这些是类:

public class PlayerHealth : MonoBehaviour
{
    #region Public Instances
    public GameObject Player;
    public GameObject Pl_DeathParticle;

    public TextMeshProUGUI NumOfHappyPower;
    public TextMeshProUGUI NumOfSadPower;
    #endregion

    #region HapAndSad System Variables
    public int playerHappinessPower;
    [HideInInspector] public int currentHappinessPower; <--
    [HideInInspector] public int currentSadnesPower;    <--These two
    #endregion

    #region HapAndSad States
    private bool isSad = false;
    #endregion

    void Start()
    {
        currentHappinessPower = playerHappinessPower; // in the enemy class is currentSadnessPower
        NumOfSadPower.enabled = false; // here in the enemy class is true
    }

    void Update()
    {
        HapAndSadPowerSystem();
        StartCoroutine(CheckThePlayerSadState());
    }

    void HapAndSadPowerSystem()
    {
        if (currentHappinessPower > 0) //enemy class: currentSadnessPower
        {
            NumOfHappyPower.text = currentHappinessPower.ToString(); //also here is changed
        }

        else if (currentHappinessPower == 0) // here also is currentSadnessPower
        {
            NumOfHappyPower.enabled = false;
            NumOfSadPower.enabled = true;

        }

        else if (currentHappinessPower < 0 && currentSadnesPower == 0) //and here
        {
            isSad = true;  // enemy class is false                                                                  
            currentSadnesPower = currentHappinessPower * -1; //Make the rest of the soubstraction positive / also here its inverted
            NumOfHappyPower.enabled = false; //true in the enemy class
            NumOfSadPower.enabled = true; // false
            NumOfSadPower.text = currentSadnesPower.ToString();
        }

        else if (currentSadnesPower > 0)
        {
            NumOfSadPower.text = currentSadnesPower.ToString();
        }
    }

    IEnumerator CheckThePlayerSadState()
    {
        if (isSad == true) //here is false
        {
            yield return new WaitForSeconds(2f);
            GameObject PlayerDeath_particle_prefab = Instantiate(Pl_DeathParticle, Player.transform.position, Quaternion.identity);

            Destroy(PlayerDeath_particle_prefab.gameObject, 1f);
            Destroy(Player.gameObject);
        }
    }
}

And here is the enemy class:这是敌人的类:

public class EnemyHealth : MonoBehaviour

    #region Public Instances
    public GameObject Enemy;
    public GameObject En_DeathParticle;

    public TextMeshProUGUI NumOfHappyPower;
    public TextMeshProUGUI NumOfSadPower;
    #endregion

    #region HapAndSad System Variables
    public int enemySadnesPower;
    [HideInInspector] public int currentHappinessPower;
    [HideInInspector] public int currentSadnesPower;
    #endregion

    #region HapAndSad States
    private bool isSad = true;
    #endregion

    void Start()
    {
        currentSadnesPower = enemySadnesPower;
        NumOfHappyPower.enabled = false;
    }

    void Update()
    {
        HapAndSadPowerSystem();
        StartCoroutine(CheckThePlayerSadState());
    }

    void HapAndSadPowerSystem()
    {
        if (currentSadnesPower > 0)
        {
            NumOfSadPower.text = currentSadnesPower.ToString();
        }

        else if (currentSadnesPower == 0)
        {
            NumOfHappyPower.enabled = true;
            NumOfSadPower.enabled = false;

        }

        else if (currentSadnesPower < 0 && currentHappinessPower == 0)
        {
            isSad = false;
            currentHappinessPower = currentSadnesPower * -1;
            NumOfHappyPower.enabled = true;
            NumOfSadPower.enabled = false;
            NumOfHappyPower.text = currentHappinessPower.ToString();
        }

        else if (currentHappinessPower > 0)
        {
            NumOfHappyPower.text = currentHappinessPower.ToString();
        }
    }

    IEnumerator CheckThePlayerSadState()
    {
        if (isSad == false)
        {
            yield return new WaitForSeconds(2f);
            GameObject PlayerDeath_particle_prefab = Instantiate(En_DeathParticle, Enemy.transform.position, Quaternion.identity);

            Destroy(PlayerDeath_particle_prefab.gameObject, 1f);
            Destroy(Enemy.gameObject);
        }
    }
}

These scripts are almost identical.这些脚本几乎相同。 I would use the same script as the player and the enemy.我会使用与玩家和敌人相同的脚本。 And add a boolean to distinguish between the player and the enemy.并添加一个布尔值来区分玩家和敌人。

Edit: If you want to use inheritance or polymorphism desperately, I would recommend you to make an Entity class and make Player and Enemy derive from it.编辑:如果您想拼命使用继承或多态性,我建议您创建一个实体类并从中派生 Player 和 Enemy 。

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

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