From 8704060b6045c60f3788f6ed7b222155291e0f81 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 21 Mar 2012 23:27:45 -0400 Subject: [PATCH] battleman.py: implemented initial code for handling damage --- battleman.py | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/battleman.py b/battleman.py index 8209359..5c1f6ae 100755 --- a/battleman.py +++ b/battleman.py @@ -98,9 +98,28 @@ class Combatant(): print("{} has initiative.".format(self)) + def damage(self, amount): + was_bloodied = self.is_bloodied() + + self.hp -= amount + + if self.is_bloodied() and not was_bloodied: + print('{} ({}) is bloodied! Remaining hp: {}'.format(self.name, self.index, self.hp)) + if self.is_down(): + print('{} ({}) is down!'.format(self.name, self.index)) + + + def is_bloodied(self): + return self.hp <= self.max_hp / 2 + + + def is_down(self): + return self.hp <= 0 + + def get_health_summary(self): bloodied = '' - if self.hp <= self.max_hp / 2: + if self.is_bloodied(): bloodied = ', bloodied' if len(self.conditions): bloodied = bloodied + ', ' @@ -128,15 +147,23 @@ def input_int(prompt, default=-1): # data about the battle - includes combatant list, etc class Battle(): def __init__(self): + self.combatant_hash = {} self.groups = [] self.current = None self.round = -1 + def is_started(self): + return self.current != None + + def add_group(self, group): self.groups.append(group) + for c in group.members: + self.combatant_hash[c.index] = c + # fixme: still returns None after battle begins def get_current_group(self): if self.current: return self.groups[self.current] @@ -145,7 +172,7 @@ class Battle(): def begin(self): - if self.current != None: + if self.is_started(): print("Error: battle is already running") return @@ -176,10 +203,12 @@ class Battle(): def next_combatant(self): print('Sorry, this is still a stub function.') g = self.get_current_group() - - + def deal_damage(self, index, amount): + c = self.combatant_hash[index] + c.damage(amount) + battle = Battle() @@ -215,7 +244,7 @@ def do_prompt(): elif comm == 'b': battle.begin() elif comm == 'd': - print('Sorry, this is still a stub function.') + do_damage() elif comm == 'h': print('Sorry, this is still a stub function.') elif comm == 's': @@ -232,6 +261,17 @@ def do_prompt(): sys.exit(0) +def do_damage(): + if not battle.is_started(): + print('Error: you can only run this command after starting the battle') + return + + battle.list_combatants() + index = input_int('choose combatant', battle.get_current_group().members[0]) + amount = input_int('damage') + battle.deal_damage(index, amount) + + def do_help(): print("""Possible commands: ? - print this help menu (yay, you already figured that one out)