I'm working on a turn based rpg and have an action/queue system setup that I'm happy with. However, I am having trouble implementing a feature and I am here for some advice. I want to have effects/abilities that alter other outcomes of actions/effects and am not sure how to structure things to allow for this.
I'll provide an example to illustrate what I am trying to achieve.
Say we have two characters in combat. The attacker needs to roll 1d20 and add the result to their current strength to see if they make a hit. If that value is equal to or greater than the defender's toughness the attack is successful and the defender takes damage.
def Attack(attacker, defender):
roll = Dice.roll()
roll += attacker.strength
if roll >= defender.toughness:
defender.damage(attacker.weapon.power)
Now lets say the attacker has a piece of gear on that says if they get a critical hit (a roll of 20) they hit the monster no matter what and deal one point of damage. How do I inject this code into the attack function and have it ignore the portion of the function after the dice is rolled (the armor's effect is replacing the rest of the attack)?
I've thought of breaking the code into chunks
def Critical_Hit_Buff(roll, defender):
if roll == 20:
defender.damage(1)
return False
return True
def Attack(attacker, defender):
do_continue = True
roll = Dice.roll()
#check if any buffs from armor/etc. activate
for buff in attacker.buffs('attack'):
do_continue = buff(roll, defender)
if do_continue:
roll += attacker.strength
if roll >= defender.toughness:
defender.damage(attacker.weapon.power)
But I really don't like having to carry around the continue variable everywhere and further I don't know if an action is not continuing because the action failed (some buff/debuff prevents the attack from being successful) or if it was because some effect replaced the attack event (this might be relevant if I give the player notifications that about if the action was successful or not).
Any advice?