-
Notifications
You must be signed in to change notification settings - Fork 5
How buffs work
Buffs currently only support static buffs, as in Increase the player's health by 10 for 3 turns. The health stays increased for three turns and after said turns pass, it is removed from the player.
Using loader.py's load_buff function, we load a function from the database table spell_buffs, whose contents are the following:
entry, name, duration, stat, amount, stat2, amount2,stat3,amount3, comment
1, Heart of a Lion, 5,strength, 10, , , , , For the potion: Strength Potion
stat - the stat this buff increases
amount - the amount it increases the stat by
duration - the amount of turns this buff lasts for
This buff increases your strength by 10 for 5 turns.
We pair all three stat/amount columns into tuples and put them in a list. [("strength", 10),(None, None),(None,None)]
Using the class Buff from buffs.py, we create an instance of it to hold a specific buff. It's init constructor accepts the following
def __init__(self, name: str, buff_stats_and_amounts, duration: int):
Where name is the name of the buff, buff_stats_and_amounts is a list of tuples, each holding information about the stat we are about to increase and the amount we are about to increase it by - [("health", 5), ("mana", 5), ("strength", 10)] and the duration is the amount of turns that it will last for.
Class Buff has a dictionary, which holds the amount each stat is increased by:
buff_amounts = {KEY_BUFF_TYPE_ARMOR: 0,
KEY_BUFF_TYPE_STRENGTH: 0,
KEY_BUFF_TYPE_HEALTH: 0,
KEY_BUFF_TYPE_MANA: 0} # type: dict
Said dictionary is filled from the def _manage_buff_types(self, buff_list: list): function which is called in init.
The Class Character stores it's buffs in a dictionary `self.buffs = {}
Whenever a buff is applied, the Character's def add_buff(self, buff: Buff): is called. It adds the buff to our self.buffs dictionary and calls the _apply_buff method.
def _apply_buff(self, buff: Buff): """ Add the buff to the character's stats""" buff_attributes = buff.get_buffed_attributes() # type: dict
It gets the Buff's buff_amounts dictionary, with all the unecessary (empty) key/value pairs removed, iterates through it and increases the appropriate Character stats.
# iterate through the buffed attributes and apply them to the character for buff_type, buff_amount in buff_attributes.items(): if buff_type == "health": self.max_health += buff_amount elif buff_type == "mana": self.max_mana += buff_amount elif buff_type == "strength": self.strength += buff_amount elif buff_type == "armor": self.armor += buff_amount
The buff stays active until it's active turns expire. On the end of each turn, the character's
def end_turn_update(self): method is called, which (currently) goes through every buff we have active and lowers it's duration by 1. If the duration is 0 or less (it has expired) we call the def remove_buff(self, buff:Buff): method.
remove_buff deletes the buff entry in the character's self.buffs dictionary and calls the _deapply_buff method.
_deapply_buff is the same as apply_buff only difference is it removes the appropriate amounts.