Health Bars (using DirectGUI ?)

small update, this is the bar class I’m using atm. I changed the one of drwr so the origin of the bar is in the center of it, because when I attached drwr’s version to the nodePath of a player and applied a billboard effect on it, the bar was not centered above the player but positioned somewhat to the right of it.

Since the offset parameter of the setBillboard functions didn’t seem to be helping I just adapted the bar class. Probably there are better (cleaner) solutions but oh well …

from pandac.PandaModules import CardMaker
from pandac.PandaModules import NodePath

class Bar(NodePath): 
        def __init__(self, scale=1, value=1, r=1, g=0, b=0): 
                NodePath.__init__(self, 'healthbar') 

                self.scale = scale
                cmfg = CardMaker('fg') 
                cmfg.setFrame(- scale,  scale, -0.1 * scale, 0.1 * scale) 
                self.fg = self.attachNewNode(cmfg.generate()) 

                cmbg = CardMaker('bg') 
                cmbg.setFrame(- scale, scale, -0.1 * scale, 0.1 * scale) 
                self.bg = self.attachNewNode(cmbg.generate()) 

                self.fg.setColor(r, g, b, 1) 
                self.bg.setColor(0.2, 0.2, 0.2, 1) 

                self.setValue(value) 

        def setValue(self, value):
                value = min(max(0, value), 1)
                self.fg.setScale(value * self.scale, 0, self.scale) 
                self.bg.setScale(self.scale * (1.0 - value), 0, self.scale)
                self.fg.setX((value - 1) * self.scale * self.scale)
                self.bg.setX(value * self.scale * self.scale)