Exemplo n.º 1
0
class World(DirectObject):

    def __init__(self):
        self.taskMgr = taskMgr
        self.CharacterManager = CharacterManager(self, render, base,loader)
        base.disableMouse()
        
        base.setBackgroundColor(0,0,0)
        self.backgroundImage = OnscreenImage(parent=render2dp,image="assets/screens/renderpipeline03-full.jpg",scale=(4,1,1),pos=(0,-20,0))
        base.cam2dp.node().getDisplayRegion(0).setSort(-20)
        self.backgroundImage.posInterval(70,Point3(2, 0, 0),startPos=Point3(-2,0,0)).start()
        
        base.cTrav = CollisionTraverser()
        self.pusher = CollisionHandlerPusher()
        self.floor = CollisionHandlerFloor()
        floorNode = render.attachNewNode("Floor NodePath")
        # Create a collision plane solid.
        collPlane = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0)))
        # Call our function that creates a nodepath with a collision node

        floorCollisionNP = self.makeCollisionNodePath(floorNode, collPlane)
        with open('config.json') as data_file:    
            self.config = json.load(data_file)
        
        self.bypassServer = self.config['bypassServer']
        self.jumpTo = self.config['jumpTo']
        self.ServerConnection = ServerConnection()
        self.NotificationScreen = NotificationScreen(self)
        self.CharacterListScreen = CharacterListScreen(self)
        self.CharacterListScreen.hideScreen()
        
        
        if not self.bypassServer:
            self.ServerConnection.connect(self.config['host'],self.config['port'])
       
        
        if self.jumpTo == 1:
            self.doAuthScreen()
        elif self.jumpTo == 2:
            self.doSelectionScreen()
        elif self.jumpTo == 3:
            self.setPlayerCharacter(RalphCharacter(self,render,base,loader))
            self.doGameScreen()
        else:
            self.doAuthScreen()
        
    def setPlayerCharacter(self,Character):
        self.Character = Character
        self.Character.placeAt((random.uniform(-10.0,10.0),random.uniform(-10.0,10.0),0))
        self.MoveManager = MoveManager(self)
        
    def doAuthScreen(self):
        self.authScreen = AuthScreen(self,render,base)
        
    def doSelectionScreen(self):
        self.characterModels = []
        self.characterModels.append(["Ralph",RalphCharacter(self,render,base,loader)])
        self.characterModels.append(["Panda",PandaCharacter(self,render,base,loader)])
        self.characterModels.append(["Motocycle",VehicleCharacter(self,render,base,loader)])
        
        self.selectScreen = CharacterSelectScreen(self,render,base,camera)
    
    def doGameScreen(self):
        self.heartbeatConnection = HeartbeatConnectionModel()
        self.ServerConnection.setupConnectionModel(self.heartbeatConnection)
        self.accept("tab",self.CharacterListScreen.toggleScreen)
        self.stopHeartbeat = False
        self.stopSendingMovement = False
        
        self.backgroundImage.destroy()
        self.Character.setControls()
        self.Character.actor.setHpr(0,0,0)
        taskMgr.add(self.Character.move,"moveTask")
        
        
        self.inst = []
        self.inst.append(addInstructions(0.95, "[ESC]: Quit/Close Chat Window"))
        self.inst.append(addInstructions(0.90, "[a]: Rotate Player Left"))
        self.inst.append(addInstructions(0.85, "[d]: Rotate Player Right"))
        self.inst.append(addInstructions(0.80, "[w]: Move Player Forward"))
        self.inst.append(addInstructions(0.75, "[s]: Move Player Backward"))
        self.inst.append(addInstructions(0.70, "[shift+w]: Move Player Fast"))
        self.inst.append(addInstructions(0.65, "[q]: Rotate Camera Left"))
        self.inst.append(addInstructions(0.60, "[e]: Rotate Camera Right"))
        self.inst.append(addInstructions(0.55, "[tab]: Show players"))
        if not self.bypassServer:
            self.inst.append(addInstructions(0.50, "[t]: Display Chat Window"))
            self.inst.append(addInstructions(0.45, "[p]: Display PM Window")) 
        
        # Set up the environment
        self.environ = loader.loadModel("models/square")
        self.environ.reparentTo(render)
        self.environ.setPos(0,0,0)
        self.environ.setScale(100,100,1)
        self.moon_tex = loader.loadTexture("models/moon_1k_tex.jpg")
        self.environ.setTexture(self.moon_tex, 1)
        
        self.staticRefSun = StaticModelSun(self,render,base,loader)
        self.staticRefVenus = StaticModelVenus(self,render,base,loader)
        self.staticRefEarth = StaticModelEarth(self,render,base,loader)

        self.sun = self.staticRefSun.getSun()
        self.venus = self.staticRefVenus.getVenus()
        self.earth = self.staticRefEarth.getEarth()
        
        taskMgr.add(self.staticRefSun.rotateSun,"rotateSun")
        taskMgr.add(self.staticRefVenus.rotateVenus,"rotateVenus")
        taskMgr.add(self.staticRefEarth.rotateEarth,"rotateEarth")
        taskMgr.add(self.staticRefEarth.stopRotateEarth,"stopRotateEarth")
        taskMgr.add(self.staticRefSun.stopRotateSun,"stopRotateSun")
        taskMgr.add(self.staticRefVenus.stopRotateVenus,"stopRotateVenus")
        taskMgr.add(self.setToGround, "setToGround")
        
        if not self.bypassServer:
            taskMgr.doMethodLater(self.config['heartbeatRate'],self.doHeartbeat,"heartbeat")
            taskMgr.doMethodLater(self.config['sendMoveRate'],self.MoveManager.sendMoves,"movement")
        
        #Change Camera Position Later
        base.camera.setPos(self.Character.actor.getX(),self.Character.actor.getY()+10,2)

        # Create some lighting
        ambientLight = AmbientLight("ambientLight")
        ambientLight.setColor(Vec4(.3, .3, .3, 1))
        directionalLight = DirectionalLight("directionalLight")
        directionalLight.setDirection(Vec3(-5, -5, -5))
        directionalLight.setColor(Vec4(1, 1, 1, 1))
        directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
        render.setLight(render.attachNewNode(ambientLight))
        render.setLight(render.attachNewNode(directionalLight))
        
        if not self.bypassServer:
            self.chatScreen = ChatScreen(self,render,base)            
            self.pChatScreen = PrivateChatScreen(self,render,base)
            self.chatScreen.hideScreen()
            self.pChatScreen.hideScreen()
        
    def doHeartbeat(self,task):
        if self.stopHeartbeat:
            return None
        self.heartbeatConnection.sendHeartbeat()
        return task.again
    
    def makeCollisionNodePath(self, nodepath, solid):
        '''
        Creates a collision node and attaches the collision solid to the
        supplied NodePath. Returns the nodepath of the collision node.
        '''
        # Creates a collision node named after the name of the NodePath.
        collNode = CollisionNode("%s c_node" % nodepath.getName())
        collNode.addSolid(solid)
        collisionNodepath = nodepath.attachNewNode(collNode)
        return collisionNodepath
    
    def setToGround(self, task):
        self.sun.setZ(0)
        self.venus.setZ(0)
        self.earth.setZ(0)
        return task.cont
    
    def endSession(self):
        self.stopHeartbeat = True
        self.stopSendingMovement = True
        self.endSession = EndSessionConnectionModel(self.exit)
        self.ServerConnection.setupConnectionModel(self.endSession)
        
        if not self.bypassServer:
            self.endSession.sendMessage(self.Character.actor.getPos(),self.Character.actor.getH())
        
        #Forces an exit
        taskMgr.doMethodLater(3,self.exit,"forceExit")
        
    def exit(self,data):
        if data != 1:
            print "Houston, we got a problem"
        sys.exit()
Exemplo n.º 2
0
    def doGameScreen(self):
        self.heartbeatConnection = HeartbeatConnectionModel()
        self.ServerConnection.setupConnectionModel(self.heartbeatConnection)
        self.accept("tab",self.CharacterListScreen.toggleScreen)
        self.stopHeartbeat = False
        self.stopSendingMovement = False
        
        self.backgroundImage.destroy()
        self.Character.setControls()
        self.Character.actor.setHpr(0,0,0)
        taskMgr.add(self.Character.move,"moveTask")
        
        
        self.inst = []
        self.inst.append(addInstructions(0.95, "[ESC]: Quit/Close Chat Window"))
        self.inst.append(addInstructions(0.90, "[a]: Rotate Player Left"))
        self.inst.append(addInstructions(0.85, "[d]: Rotate Player Right"))
        self.inst.append(addInstructions(0.80, "[w]: Move Player Forward"))
        self.inst.append(addInstructions(0.75, "[s]: Move Player Backward"))
        self.inst.append(addInstructions(0.70, "[shift+w]: Move Player Fast"))
        self.inst.append(addInstructions(0.65, "[q]: Rotate Camera Left"))
        self.inst.append(addInstructions(0.60, "[e]: Rotate Camera Right"))
        self.inst.append(addInstructions(0.55, "[tab]: Show players"))
        if not self.bypassServer:
            self.inst.append(addInstructions(0.50, "[t]: Display Chat Window"))
            self.inst.append(addInstructions(0.45, "[p]: Display PM Window")) 
        
        # Set up the environment
        self.environ = loader.loadModel("models/square")
        self.environ.reparentTo(render)
        self.environ.setPos(0,0,0)
        self.environ.setScale(100,100,1)
        self.moon_tex = loader.loadTexture("models/moon_1k_tex.jpg")
        self.environ.setTexture(self.moon_tex, 1)
        
        self.staticRefSun = StaticModelSun(self,render,base,loader)
        self.staticRefVenus = StaticModelVenus(self,render,base,loader)
        self.staticRefEarth = StaticModelEarth(self,render,base,loader)

        self.sun = self.staticRefSun.getSun()
        self.venus = self.staticRefVenus.getVenus()
        self.earth = self.staticRefEarth.getEarth()
        
        taskMgr.add(self.staticRefSun.rotateSun,"rotateSun")
        taskMgr.add(self.staticRefVenus.rotateVenus,"rotateVenus")
        taskMgr.add(self.staticRefEarth.rotateEarth,"rotateEarth")
        taskMgr.add(self.staticRefEarth.stopRotateEarth,"stopRotateEarth")
        taskMgr.add(self.staticRefSun.stopRotateSun,"stopRotateSun")
        taskMgr.add(self.staticRefVenus.stopRotateVenus,"stopRotateVenus")
        taskMgr.add(self.setToGround, "setToGround")
        
        if not self.bypassServer:
            taskMgr.doMethodLater(self.config['heartbeatRate'],self.doHeartbeat,"heartbeat")
            taskMgr.doMethodLater(self.config['sendMoveRate'],self.MoveManager.sendMoves,"movement")
        
        #Change Camera Position Later
        base.camera.setPos(self.Character.actor.getX(),self.Character.actor.getY()+10,2)

        # Create some lighting
        ambientLight = AmbientLight("ambientLight")
        ambientLight.setColor(Vec4(.3, .3, .3, 1))
        directionalLight = DirectionalLight("directionalLight")
        directionalLight.setDirection(Vec3(-5, -5, -5))
        directionalLight.setColor(Vec4(1, 1, 1, 1))
        directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
        render.setLight(render.attachNewNode(ambientLight))
        render.setLight(render.attachNewNode(directionalLight))
        
        if not self.bypassServer:
            self.chatScreen = ChatScreen(self,render,base)            
            self.pChatScreen = PrivateChatScreen(self,render,base)
            self.chatScreen.hideScreen()
            self.pChatScreen.hideScreen()