Ejemplo n.º 1
0
class GuiUnitInfo:
    def __init__(self, offset, parent, unit_type, default_hp, hp, default_ap, ap):
            
        self.offset = offset
        self.frame = DirectFrame(   relief = DGG.FLAT
                                  , scale = 1
                                  , frameSize = (-0.5, 0.5, 0, -0.5)
                                  , parent = parent )
        self.frame.setBillboardPointEye()
        self.frame.setLightOff()
        self.frame.setBin("fixed", 40)
        self.frame.setDepthTest(False)
        self.frame.setDepthWrite(False)
        
        fixedWidthFont = loader.loadFont(GUI_FONT)#@UndefinedVariable        
        #fixedWidthFont.setPixelsPerUnit(60)
        #fixedWidthFont.setRenderMode(fontt.RMSolid)
        if not fixedWidthFont.isValid():
            print "pandaInteractiveConsole.py :: could not load the defined font %s" % str(self.font)
            fixedWidthFont = DGG.getDefaultFont()
        
        self.label = OnscreenText( parent = self.frame
                              , text = ""
                              , pos = (offset.getX(),offset.getZ()+0.1)
                              , align=TextNode.ACenter
                              , mayChange=True
                              , scale=0.1
                              , fg = (1,0,0,1)
                              #, shadow = (0, 0, 0, 1)
                              #, frame = (200,0,0,1) 
                              )
        self.label.setFont( fixedWidthFont )
        #self.label.setLightOff()

        self.all_icons = {}
        self.visible_icons = {}
        self.addIcon("overwatch")
        self.addIcon("set_up")
        
        self.ap_bar = DirectWaitBar(parent = self.frame
                                  , text = ""
                                  , range = default_ap
                                  , value = ap
                                  , pos = (offset.getX()+0.08,0,offset.getZ()-0.27)
                                  , barColor = (0,0,1,1)
                                  , frameColor = (0,0,0.5,0.2)
                                  , scale = (0.3,0.5,0.3))
        
        self.hp_bar = DirectWaitBar(parent = self.frame
                                  , text = ""
                                  , range = default_hp
                                  , value = hp
                                  , pos = (offset.getX()+0.08,0,offset.getZ()-0.2)
                                  , barColor = (0,1,0,1)
                                  , frameColor = (1,0,0,0.9)
                                  , scale = (0.3,0.5,0.3))
        
        self.insignia = OnscreenImage(parent = self.frame
                                            ,image = "unit_" + unit_type + "_big_transparent_32.png"
                                            #,pos = (offset.getX(),0,offset.getZ()+0.14)
                                            , pos = (offset.getX() - 0.31,0,offset.getZ()-0.23)
                                            ,scale = 0.09)
        self.insignia.setTransparency(TransparencyAttrib.MAlpha)

    def addIcon(self, name):
        self.all_icons[name] = OnscreenImage(parent = self.frame
                                            ,image = name + "_icon.png"
                                           #,pos = offset + (0,0,-0.1)
                                            ,scale = 0.08)
        
        self.all_icons[name].setTransparency(TransparencyAttrib.MAlpha)
        self.all_icons[name].hide()
        
    def write(self, text):
        text = ""
        self.label.setText(text)
        
    def redraw(self):
        return

    def remove(self):
        self.frame.remove()
        
    def reparentTo(self, parent):
        self.frame.reparentTo(parent)
        
    def hide(self):
        self.label.hide()
        
    def show(self):
        self.label.show()
    
    def refreshBars(self, hp, ap):
        self.ap_bar['value'] = ap
        self.hp_bar['value'] = hp
        self.ap_bar.setValue()
        self.hp_bar.setValue()
        
    def refreshIcons(self):
        count = len(self.visible_icons)
        start_pos =  (1 - count) * 0.25 / 2
        for icon in self.all_icons:
            if icon in self.visible_icons:
                self.visible_icons[icon].setPos(self.offset + (start_pos, 0, -0.08))
                self.visible_icons[icon].show()
                start_pos += 0.21
            else:
                self.all_icons[icon].hide()
            
    def hideOverwatch(self):
        if "overwatch" in self.visible_icons:
            self.visible_icons.pop("overwatch")
        self.refreshIcons()

    def showOverwatch(self):
        self.visible_icons["overwatch"] = self.all_icons["overwatch"]
        self.refreshIcons()
    
    def hideSetUp(self):
        if "set_up" in self.visible_icons:
            self.visible_icons.pop("set_up")
        self.refreshIcons()

    def showSetUp(self):
        self.visible_icons["set_up"] = self.all_icons["set_up"]
        self.refreshIcons()
class DistributedInspectionSite(DistributedNode):
    
    def __init__(self, cr):
        DistributedNode.__init__(self, cr)
        
        #######################################################
        # The below attributes are required at generation time.
        #######################################################
        # The site id is the unique id associated with the data
        # for the inspection site.
        #######################################################
        self.siteId = -1
        self.zoneId = -1
        #######################################################
        self.siteData = None
        
        self.distanceTask = None
        self.interactCollSphereNP = None
        self.interactCollSphere = None
        self.identifierIcon = None
        self.floorMarker = None
        
        self.iconSequence = None
        self.fadeSequence = None
        self.text = None
        
    def setSiteId(self, _id):
        self.siteId = _id
    
    def getSiteId(self):
        return self.siteId
    
    def setZoneId(self, _id):
        self.zoneId = _id
        
    def getZoneId(self):
        return self.zoneId
        
    def announceGenerate(self):
        DistributedNode.announceGenerate(self)
        self.setName('InspectionSite-%d' % self.siteId)
        self.notify = directNotify.newCategory('DistributedInspectionSite[%d]' % self.siteId)
        self.siteData = InspectionSites.getSiteById(self.zoneId, self.siteId)
        
        if self.siteData:
            interactKey = base.inputStore.Interact.upper()
            self.text = OnscreenText(
                text = 'Press \'%s\' to %s' % (interactKey, self.siteData.type),
                pos = (0, -0.75), font = CIGlobals.getToonFont(), fg = (1, 1, 1, 1),
                shadow = (0, 0, 0, 1)
            )
            self.text.hide()
            self.distanceTask = base.taskMgr.add(self.__calculateDistance, self.uniqueName('calculateAvDistance'))
            self.generateCollision()
            self.generateIconAndMarker()
            self.accept('enter' + self.interactCollSphereNP.getName(), self.onEnter)
            self.accept('exit' + self.interactCollSphereNP.getName(), self.onExit)
        self.reparentTo(render)
        
    def __calculateDistance(self, task):
        dist = float(base.localAvatar.getPos(self).length())
        fullAlphaDist = 15.0
        noAlphaDist = 105.0
        alpha = 1.0
        
        if 10 < dist < noAlphaDist:
            alpha = float(fullAlphaDist / dist)
            if alpha < 0.0:
                alpha = 0.0
        elif dist >= noAlphaDist:
            alpha = 0.0
            
        self.identifierIcon.setColorScale(1.0, 1.0, 1.0, alpha)
        self.floorMarker.setColorScale(1.0, 1.0, 1.0, alpha)
        return task.cont
        
    def generateCollision(self):
        self.interactCollSphere = CollisionSphere(0, 0, 0, 4)
        self.interactCollSphere.setTangible(0)
        ss = CollisionNode('inspectionSensor')
        ss.addSolid(self.interactCollSphere)
        
        self.interactCollSphereNP = self.attachNewNode(ss)
        self.interactCollSphereNP.setCollideMask(CIGlobals.EventBitmask)

    def generateIconAndMarker(self):
        icon = 'phase_5/maps/inspect_location.mat'
        self.identifierIcon = DirectFrame(parent = self, image = icon, frameColor = (0, 0, 0, 0))
        self.identifierIcon.setTransparency(TransparencyAttrib.MAlpha)
        self.identifierIcon.setTwoSided(1)
        self.identifierIcon.setBillboardPointEye()
        self.identifierIcon.setZ(2.0)
        
        self.iconSequence = Sequence(
            LerpPosInterval(self.identifierIcon, 1.0, pos = (0, 0, 6.0), startPos = (0, 0, 3.0),
                blendType = 'easeInOut', name = ('inspSite%d-in' % self.siteId)),
            LerpPosInterval(self.identifierIcon, 1.0, pos = (0, 0, 3.0), startPos = (0, 0, 6.0),
                blendType = 'easeInOut', name = ('inspSite%d-out' % self.siteId))
        )
        
        self.iconSequence.loop()
        
        cm = CardMaker('marker')
        self.floorMarker = self.attachNewNode(cm.generate())
        self.floorMarker.setBSPMaterial(icon, 1)
        self.floorMarker.setTransparency(TransparencyAttrib.MAlpha)
        self.floorMarker.setTwoSided(1)
        self.floorMarker.setPosHpr(-2.50, 2.53, -0.01, 0.0, 90.0, 0.0)
        self.floorMarker.setScale(5.0)
        
    def requestEnter(self):
        pass
    
    def canEnter(self):
        place = base.cr.playGame.getPlace()
        return place and place.fsm.getCurrentState().getName() == 'walk'
        
    def onEnter(self, _):
        self.text.show()
        self.acceptOnce(base.inputStore.Interact, self.requestEnter)
        
    def onExit(self, _):
        self.text.hide()
        self.ignore(base.inputStore.Interact)

    def disable(self):
        DistributedNode.disable(self)
        base.taskMgr.remove(self.distanceTask)
        if self.iconSequence:
            self.iconSequence.finish()
            self.iconSequence = None
        if self.fadeSequence:
            self.fadeSequence.finish()
            self.fadeSequence = None
        if self.text:
            self.text.hide()

        self.ignoreAll()
        
    def delete(self):
        if self.siteData:
            self.siteData = None
        if self.identifierIcon:
            self.identifierIcon.destroy()
            self.identifierIcon = None
        if self.distanceTask:
            self.distanceTask = None
        if self.interactCollSphereNP:
            self.interactCollSphereNP.removeNode()
            self.interactCollSphereNP = None
            self.interactCollSphere = None
        if self.floorMarker:
            self.floorMarker.removeNode()
            self.floorMarker = None
        if self.text:
            self.text.hide()
            self.text.destroy()
            self.text = None
        del self.siteData
        del self.identifierIcon
        del self.distanceTask
        del self.interactCollSphereNP
        del self.interactCollSphere
        del self.floorMarker
        del self.text
        DistributedNode.delete(self)