First Games for Fun here. Let`s go.

My wife and a friend started developing a mobile game using Unity3D to build up their portifolio and I am playing with them just for fun. Really, just for fun, I am no game developer or expert in any ways.

We are having fun with sort of a runner/beat'n'up/rpg/rogue-like. Yeah, that's a lot of labels. So I wanted to share some of the stuff Iam doing.

On this game, our guy goes running and fighting monsters invading a town and there is an important feature with equipaments, weapons and etc. So let us go down to business and get dirty with some code.


Approaching the combat in the game, basically the player engages monsters and they hit each other. Immediatly this elements pops some interfaces on my head: one for attackers and one for defenders/targets.

ICombatTarget

public interface ICombatTarget
{
    //something is attacking me so I must defend
    void Defend(int attack);
}

IAttacker

public interface IAttacker
{
    //The attacker returns its damage
    int Attack();
    //and must have a target
    ICombatTarget GetTarget();
}

If we wish the player to attack a monster we simply implement IAttacker on the player and ICombatTarget on the monster. Something like this:

Player Actions Manager

public class PlayerActions : MonoBehaviour, IAttacker {

    //a lot of attributes goes here
    ...
    //a lot of other methos goes here
    ...
    
    public int Attack()
    {
        int attacks = 0;
        //the player base attack, calculated using STR attribute or something. 
        attacks += this.GetPlayerBaseAttack();
        //the current equiped weapon on the player
        attacks += this.GetPlayerWeapon().GetAttack();
        return attacks;
    }

    public ICombatTarget GetTarget()
    {
        //get the closest enemy for example
        return this.GetClosestFocusedEnemy();
    }

    //a fight method used by a combat manager or something like this...
    public void Fight(){
        // here I get the engaged target and delegate the player attack for it to defend itself.
        this.GetTarget().Defend(Attack());
    }
    
}

Cool. Now the player can attack whatever object implementing ICombatTarget. In other words, any object that can be targeted during combat, will know how to defend itself, and of course, it is not a responsability of the attacker to know how the target will defend itself. Feels like a SOLID game implementation already =)

Let's check the monster: Monster Actions Manager

public class EnemyActions : MonoBehaviour, ICombatTarget{

    //a lot of attributes goes here
    ...
    //a lot of other methos goes here
    ...
    
    public void Defend(int attack)
    {
        //this one is very simple.
        //first calculate damage based on armor or something:
        int damage = attack - this.GetArmor();
        // then take damage
        if(damage > 0){
            this.TakeDamage(damage);
        }
    }

    //quick example of TakeDamage
    public void TakeDamage(int damage)
    {
        this.health -= damage;
        //so check if this enemy is dead or something. 
    }

}

Any game object implementing ICombatTarget should be able to be target of an attack.

You may be thinking: so, doesn't the enemy hit back at the player?. Yeah, you are right and probably you have already guessed how to achieve that: implement IAttacker on the enemy and ICombatTarget on the player.

The player is leveling up, geting gold and buying new weapons with special effects. Can you imagine what would happen if the player weilded weapon on both hands, of if the player used some weapons with elemental effects or with double hit? Well, attackers and targets/defenders should be opened to extension but closed for modification.

Does it means this strategy for this problem is wrong? Not at all. If it is a simple game with simple attack and defense attributes, thats ok. But on our runner/beat'n'up/rpg/rogue-like, the hero goes crazy about new weapons, so on part 2, I will explain my strategy to handle this.

Thanks, see you next time.