summaryrefslogtreecommitdiff
path: root/asciifarm/client/display/healthpad.py
blob: 559906d9df40dddf8d64a14d7db42d667f98377d (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
35
36

import curses

class HealthPad:
    
    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
        self.maxHealth = maxHealth
        self.widget.change()
    
    def update(self):
        win = self.widget.getWin()
        height, width = win.getmaxyx()
        width -= 1
        barEnd = round(self.health/self.maxHealth * width) if self.maxHealth > 0 else 0
        win.erase()
        win.addstr(0,0,"Health: {}/{}".format(self.health, self.maxHealth)[:width])
        if self.colours:
            win.addstr(1,0, self.char[0]*barEnd, self.colours.get(*self.char[1:]))
            win.addstr(1,barEnd, self.emptyChar[0]*(width-barEnd), self.colours.get(*self.emptyChar[1:]))
        else:
            win.addstr(1,0, self.char[0]*barEnd)
            win.addstr(1,barEnd, self.emptyChar[0]*(width-barEnd))
        win.noutrefresh()