java – What’s a great way to implement buff-skill and active-skill

java – What’s a great way to implement buff-skill and active-skill

[ad_1]

I’m attempting to implement buff-skill(which modifies character’s stats or different talent) and active-skill(which does harm to enemy) through the use of polymorphism. The answer does not obligatory have to make use of polymorphism, I am simply attempting to make use of it as a result of I believed it suits one of the best for the issue.

class Character {
    //different fields..
    int power;
    Record<Merchandise> gadgets = new ArrayList<>();
    Record<Ability> abilities = new ArrayList<>();

    void applyItems() {
        for(Merchandise merchandise : gadgets) {
            merchandise.apply(this);
        }
    }

    void applySkillBuffs() {
        for(Ability talent : abilities) {
            talent.apply(this);
        }
    }
}

summary class Ability {
    String title; //Enum will probably be used as an alternative of String in actual code.
    int stage;

    void apply(Character character) {}; //Default : Do nothing

    void doDamage(Enemy enemy) {}; //Default : Do nothing
}

class CharBuffSkill extends Ability {
    @Override
    void apply(Character character) {
        character.power += 10;
    }
}

class SkillBuffSkill extends Ability {
    @Override
    void apply(Character character) {
        for(Ability talent : character.abilities) {
            talent.stage += 1;
        }
    }
}

class ActiveSkill extends Ability {
    @Override
    void doDamage(Enemy enemy) {
        enemy.hp -= 50;
    }
}

CharBuffSkill class represents the talents that modifies character’s stats(In right here, power).
SkillBuffSkill class represents the talents that modifies different talent’s stats(On this code it additionally modifies itself buf I am going to simply ignore that drawback now).
ActiveSkill class represents the talents that really do harm to enemies.

My principal concern is that this : Is it good to handle all types of talent as a Ability class(just like the code above) or is it higher simply to seperate them and handle them seperately(in numerous Record)

What’s on my thoughts is that it looks as if slightly little bit of waste for Energetic talent to have apply() methodology(or CharBuffSkill/SkillBuffSkill having doDamage() methodology). Furthermore, ActiveSkills are at all times referred to as each time characters’s apply() methodology is excuted despite the fact that it would by no means do something.

Then again, if I individually handle them like :

Record<BuffSkill> buffSKills = new ArrayList<>(); 
//CharBuffSKill and SKillBuffSkill extends BuffSkill on this scenario.
Record<ActiveSkill> activeSkills = new ArrayList<>();

no waste of apply(), doDamage() methodology will occur however,
few extra traces are anticipated to those that take care of character’s abilities.

@Override
void apply(Character character) {
    for(Ability talent : character.buffSkills) {
        talent.stage += 1;
    }

      for(Ability talent : character.activeSkills) {
        talent.stage += 1;
    }
}

Please share me your wisdoms.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply