Search Unity

Slightly better way to handle input method with different outcome?

Discussion in 'Scripting' started by Rechronicle, Feb 10, 2022.

  1. Rechronicle

    Rechronicle

    Joined:
    Dec 14, 2017
    Posts:
    32
    So uh, I'm trying to create basic 'Attack' and 'Skill' buttons. It's just going to reduce target health and wait for a little until the next round.

    the code in action:
    Code (CSharp):
    1.     private void Attack(Animator anim, UnitManager.BattlerStat targetStat, UnitManager.BattlerStat attackerStat)
    2.     {
    3.         anim.Play("Unit_Attack");
    4.         targetStat.CurrentHealth -= attackerStat.Attack;
    5.  
    6.         UnitManager.OnValueChange.Invoke();
    7.         StartCoroutine(WaitBeforeProceedTurn());
    8.     }
    9.  
    10.     public void AttackTheEnemy()
    11.     {
    12.         Attack(playerAnim, UnitManager.EnemyBattler, UnitManager.PlayerBattler);
    13.     }
    14.  
    15.     public void AttackThePlayer()
    16.     {
    17.         Attack(enemyAnim, UnitManager.PlayerBattler, UnitManager.EnemyBattler);
    18.     }
    19.  
    20.     public void SkillThatDamagePlayer()
    21.     {
    22.         Attack();
    23.     }
    24.  
    25.     public void SkillThatDamageEnemy()
    26.     {
    27.         Attack();
    28.     }
    I try to 'encapsulate' the Attack method so it can be invoked from UnityEvent.
    But it's starting to get out of control if I add even little things from here (heal, etc).

    Any tips to handle this kind of approach?
    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692