battleman.py: implemented initial code for handling damage
This commit is contained in:
parent
ddbc14871a
commit
8704060b60
48
battleman.py
48
battleman.py
|
@ -98,9 +98,28 @@ class Combatant():
|
||||||
print("{} has initiative.".format(self))
|
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):
|
def get_health_summary(self):
|
||||||
bloodied = ''
|
bloodied = ''
|
||||||
if self.hp <= self.max_hp / 2:
|
if self.is_bloodied():
|
||||||
bloodied = ', bloodied'
|
bloodied = ', bloodied'
|
||||||
if len(self.conditions):
|
if len(self.conditions):
|
||||||
bloodied = bloodied + ', '
|
bloodied = bloodied + ', '
|
||||||
|
@ -128,15 +147,23 @@ def input_int(prompt, default=-1):
|
||||||
# data about the battle - includes combatant list, etc
|
# data about the battle - includes combatant list, etc
|
||||||
class Battle():
|
class Battle():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.combatant_hash = {}
|
||||||
self.groups = []
|
self.groups = []
|
||||||
self.current = None
|
self.current = None
|
||||||
self.round = -1
|
self.round = -1
|
||||||
|
|
||||||
|
|
||||||
|
def is_started(self):
|
||||||
|
return self.current != None
|
||||||
|
|
||||||
|
|
||||||
def add_group(self, group):
|
def add_group(self, group):
|
||||||
self.groups.append(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):
|
def get_current_group(self):
|
||||||
if self.current:
|
if self.current:
|
||||||
return self.groups[self.current]
|
return self.groups[self.current]
|
||||||
|
@ -145,7 +172,7 @@ class Battle():
|
||||||
|
|
||||||
|
|
||||||
def begin(self):
|
def begin(self):
|
||||||
if self.current != None:
|
if self.is_started():
|
||||||
print("Error: battle is already running")
|
print("Error: battle is already running")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -178,7 +205,9 @@ class Battle():
|
||||||
g = self.get_current_group()
|
g = self.get_current_group()
|
||||||
|
|
||||||
|
|
||||||
|
def deal_damage(self, index, amount):
|
||||||
|
c = self.combatant_hash[index]
|
||||||
|
c.damage(amount)
|
||||||
|
|
||||||
|
|
||||||
battle = Battle()
|
battle = Battle()
|
||||||
|
@ -215,7 +244,7 @@ def do_prompt():
|
||||||
elif comm == 'b':
|
elif comm == 'b':
|
||||||
battle.begin()
|
battle.begin()
|
||||||
elif comm == 'd':
|
elif comm == 'd':
|
||||||
print('Sorry, this is still a stub function.')
|
do_damage()
|
||||||
elif comm == 'h':
|
elif comm == 'h':
|
||||||
print('Sorry, this is still a stub function.')
|
print('Sorry, this is still a stub function.')
|
||||||
elif comm == 's':
|
elif comm == 's':
|
||||||
|
@ -232,6 +261,17 @@ def do_prompt():
|
||||||
sys.exit(0)
|
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():
|
def do_help():
|
||||||
print("""Possible commands:
|
print("""Possible commands:
|
||||||
? - print this help menu (yay, you already figured that one out)
|
? - print this help menu (yay, you already figured that one out)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user