Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Help reduce amount of code required to manage

This topic is 2037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!