Sign in to follow this  
BaneTrapper

Help reduce amount of code required to manage

Recommended Posts

Hello.

Currently i get codes like this:

"Scroll down if your not interested in code and its unneeded to know it"

void Buffs::Burn(HeroUnit& unit, Buff& objBuf, Battle& objBat)
{
    int amount = objBuf.amount - unit.battMA;
    if(amount < 1)
        amount = 1;

    //Check if damage kills unit
    if(unit.battHP - amount < 1)
    {
        //Kill unit
        unit.battHP = 0;
        unit.isAlive = false;
        unit.battRT = 0;
        objBat.ChatPushBackText(unit.name + " burned for " + std::to_string(amount) + " and died in process", sf::Color(255,150,150));
    }
    else
    {
        //Damage
        unit.battHP -= amount;
        objBat.ChatPushBackText(unit.name + " burned for " + std::to_string(amount), sf::Color(255,150,150));
    }
}
void Buffs::Burn(EnemyUnit& unit, Buff& objBuf, Battle& objBat)
{
    int amount = objBuf.amount - unit.battMA;
    if(amount < 1)
        amount = 1;

    //Check if damage kills unit
    if(unit.battHP - amount < 1)
    {
        //Kill unit
        unit.battHP = 0;
        unit.isAlive = false;
        unit.battRT = 0;
        objBat.ChatPushBackText(unit.name + " burned for " + std::to_string(amount) + " and died in process", sf::Color(255,150,150));
    }
    else
    {
        //Damage
        unit.battHP -= amount;
        objBat.ChatPushBackText(unit.name + " burned for " + std::to_string(amount), sf::Color(255,150,150));
    }
}

Exact code, but takes different unit parameter.

The different between units is sever, but they share same attributes used for/in battle.
I would use "inheritance" (structure units to inherit a "UnitBattleData" class).

But how to call a function when its parameters is "void Battle(UnitBattleData& good_unit, UnitBattleData& evil_unit);" and i have object of "HeroUnit good_unit" "EvilUnit evil_unit".

Share this post


Link to post
Share on other sites
If both HeroUnit and EvilUnit inherit from UnitBattleData then you can just use Battle(good_unit, evil_unit).

Share this post


Link to post
Share on other sites

If both HeroUnit and EvilUnit inherit from UnitBattleData then you can just use Battle(good_unit, evil_unit).

I have encountered problems with the way you provided even before i made the post. But after a bit of checking i found that i have/had an error, thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this