コード例 #1
0
    def __init__(self, playerName = None, threadedNet = True):
        dcFileNames = ['direct.dc', 'tagger.dc']
        
        ClientRepository.__init__(self, dcFileNames = dcFileNames,
                                  connectMethod = self.CM_NET,
                                  threadedNet = threadedNet)

        base.transitions.FadeModelName = 'models/fade'
        
        # Need at least 32 bits to receive big picture packets.
        self.setTcpHeaderSize(4)

        # Allow some time for other processes.  This also allows time
        # each frame for the network thread to run.
        base.setSleep(0.01)

        # If we're using OpenGL, we can enable shaders.  (DirectX
        # shader support is still kind of spotty, even for simple
        # shaders like these.)
        if base.pipe and base.pipe.getInterfaceName() == 'OpenGL':
            Globals.EnableShaders = True

        self.gotMusic = False
        self.__getMusic()
        
        # For the browse button.
        self.posterDefaultDir = Filename.getHomeDirectory().toOsSpecific()

        # Load a fun font to be the default text font.
        labelFont = loader.loadFont('models/amsterdam.ttf', okMissing = True)
        if labelFont:
            # Make a fuzzy halo behind the font so it looks kind of
            # airbrushy
            labelFont.setOutline(VBase4(0, 0, 0, 1), 2.0, 0.9)
            TextNode.setDefaultFont(labelFont)

        base.disableMouse()
        if base.mouseWatcher:
            mb = ModifierButtons()
            mb.addButton(KeyboardButton.control())
            base.mouseWatcher.node().setModifierButtons(mb)
            base.buttonThrowers[0].node().setModifierButtons(mb)
        taskMgr.setupTaskChain('loadPoster', numThreads = 4,
                               threadPriority = TPLow)
        #taskMgr.setupTaskChain('net', numThreads = 1, threadPriority = TPLow, frameSync = True)

        # Set up a text property called "tag" that renders using the
        # tag font, in white, with a shadow.  This is used for
        # rendering the art-painting awards at the end of the round.
        tpMgr = TextPropertiesManager.getGlobalPtr()
        tp = TextProperties()
        tagFont = loader.loadFont('models/one8seven.ttf', okMissing = True)
        if tagFont:
            tp.setFont(tagFont)
        tp.setTextColor(1, 1, 1, 1)
        tp.setShadow(0.05, 0.05)
        tp.setTextScale(1.5)
        tpMgr.setProperties('tag', tp)

        # If we're running from the web, get the gameInfo block from
        # the HTML tokens.
        self.gameInfo = None
        if base.appRunner:
            gameInfoName = base.appRunner.getToken('gameInfo')
            if gameInfoName:
                self.gameInfo = base.appRunner.evalScript(gameInfoName, needsResponse = True)

            # Expose the changePoster() method.
            base.appRunner.main.changePoster = self.changePoster

        print "self.gameInfo = %s" % (self.gameInfo)

        # Also be prepared to update the web form with the table of
        # players and the local player's score.
        self.playerTable = None
        self.scoreTable = None
        if base.appRunner and base.appRunner.dom:
            self.playerTable = base.appRunner.dom.document.getElementById('playerTable')
            self.scoreTable = base.appRunner.dom.document.getElementById('scoreTable')
        print "self.playerTable = %s, scoreTable = %s" % (self.playerTable, self.scoreTable)

        self.playerList = PlayerList(self.playerTable)
        self.onscreenScoreLeft = None

        # When we join a game, we'll prefer to join *this* game.
        self.nextGameId = 0
        self.chooseGameTask = None
        self.allGames = []

        # No game, no avatar (yet).
        self.robot = None
        self.game = None
        self.player = None
        self.av = None
        self.avCell = None
        self.paintThing = None

        self.keyMap = {}
        for key in Globals.ControlKeys:
            self.keyMap[key] = False

        self.dlnp = render.attachNewNode(DirectionalLight('dlnp'))
        self.dlnp.node().setColor((0.8, 0.8, 0.8, 1))
        render.setLight(self.dlnp)
        self.alnp = render.attachNewNode(AmbientLight('alnp'))
        self.alnp.node().setColor((0.2, 0.2, 0.2, 1))
        render.setLight(self.alnp)

        if base.camera:
            self.dlnp.reparentTo(base.camera)

        # Set up the poster FSM to switch the poster modes.
        self.posterFSM = PosterFSM(base.appRunner)

        # A node to hold all avatars.
        self.avRoot = render.attachNewNode('avRoot')

        # The root of the maze.
        self.mazeRoot = render.attachNewNode('maze')
        if Globals.EnableShaders:
            #self.mazeRoot.setShaderAuto()
            s = loader.loadShader('models/nopaint_normal.sha')
            self.mazeRoot.setShader(s)
            self.mazeRoot.setShaderInput('alight0', self.alnp)
            self.mazeRoot.setShaderInput('dlight0', self.dlnp)

        # Initial poster data.
        self.posterData = ('', 0)
        cvar = ConfigVariableFilename('tag-poster', '')
        filename = cvar.getValue()
        if filename:
            self.readTagPoster(filename)

        # Choose a bright paint color.
        h = random.random()
        s = random.random() * 0.3 + 0.7
        v = random.random() * 0.3 + 0.7
        self.playerColor = self.hsv2rgb(h, s, v)

        # Get the player's name.
        name = playerName
        if not name:
            name = getattr(self.gameInfo, 'name', None)
        if not name:
            name = base.config.GetString('player-name', '')
            
        if name:
            # Use the provided name.
            self.playerName = name
            self.startConnect()

        else:
            # Prompt the user.

            # Start with the wall model in the background.
            wall = loader.loadModel('models/wall')
            wall.reparentTo(base.camera)
            wall.setPos(0, 5, -2.5)
            wall.setScale(10)
            if Globals.EnableShaders:
                wall.setShaderAuto()
            self.nameWall = wall

            c = self.playerColor
            self.nameLabel = DirectLabel(
                text = 'Enter your street name:',
                text_align = TextNode.ALeft,
                text_fg = (c[0], c[1], c[2], 1),
                text_shadow = (0, 0, 0, 1),
                pos = (-0.9, 0, 0.45),
                relief = None,
                scale = 0.2)
            tagFont = loader.loadFont('models/one8seven.ttf')

            cardModel = loader.loadModel('models/nametag_card')
            cardTex = cardModel.findTexture('*')
            self.nameEntry = DirectEntry(
                pos = (-0.9, 0, 0.1),
                focus = True,
                relief = DGG.TEXTUREBORDER,
                frameColor = (c[0], c[1], c[2], 0.6),
                frameTexture = cardTex,
                borderWidth = (0.2, 0.2),
                pad = (0.0, -0.2),
                borderUvWidth = (0.1, 0.1),
                text_font = tagFont,
                width = 12, scale = 0.15,
                command = self.enteredName)

            self.nameButton = DirectButton(
                text = 'Enter game',
                frameColor = (c[0], c[1], c[2], 1),
                scale = 0.15,
                pos = (0, 0, -0.5),
                relief = DGG.RAISED,
                borderWidth = (0.05, 0.05),
                pad = (0.8, 0.3),
                command = self.enteredName)