summaryrefslogtreecommitdiff
path: root/asciifarm/client/display/health.py
blob: 3a3595bfab4a4e935f503985371d92d7eec55d06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


class Health:
    
    def __init__(self, char=('@',7,0), emptyChar=('-',7,0), colours=False):
        self.char = char
        self.emptyChar = emptyChar
        self.changed = False
        self.colours = colours
        self.health = 0
        self.maxHealth = 0
        self.widget = None
    
    def setWidget(self, widget):
        self.widget = widget
    
    def setHealth(self, health, maxHealth):
        self.health = health or 0
        self.maxHealth = maxHealth or 0
        self.widget.change()
    
    def update(self, win):
        width, height = win.getSize()
        width -= 1
        barEnd = round(self.health/self.maxHealth * width) if self.maxHealth > 0 else 0
        win.erase()
        win.addLine((0,0),"Health: {}/{}".format(self.health, self.maxHealth)[:width])
        if self.colours:
            win.addLine((0, 1), self.char[0]*barEnd, self.char[1:])
            win.addLine((barEnd, 1), self.emptyChar[0]*(width-barEnd), self.emptyChar[1:])
        else:
            win.addLine((0, 1), self.char[0]*barEnd)
            win.addLine((barEnd, 1), self.emptyChar[0]*(width-barEnd))
        win.noutrefresh()