Beispiel #1
0
 def __init__(self):
     ShowBase.__init__(self)
     
     self.console = panda3dIOClass(self)
     
     self.US = UpdateScreen(self.world_size)
     self.US.start()
     
     self.SL = SocketListener()
     QtGui.QMainWindow.connect(self.SL, QtCore.SIGNAL("Update( PyQt_PyObject )"), self.update)
     self.SL.start()
Beispiel #2
0
class PandaGUI(ShowBase):
    world_size = 80
    world_size = world_size, world_size
    
    boxes_list = []
    cubes = {}
    npc_dict = {}
    
    scale_factor = .5
    
    def __init__(self):
        ShowBase.__init__(self)
        
        self.console = panda3dIOClass(self)
        
        self.US = UpdateScreen(self.world_size)
        self.US.start()
        
        self.SL = SocketListener()
        QtGui.QMainWindow.connect(self.SL, QtCore.SIGNAL("Update( PyQt_PyObject )"), self.update)
        self.SL.start()
        
        #self.disableMouse()
        
        #self.loadEnvironment()
        
        #self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
        
        #self.loadActor()
        #self.moveActor()
        
        #self.camera.setPos(100, 100, 100)
        #print "X", self.camera.getX()
        #self.camera.setPos(100, 100, 100)
        #print "Y", self.camera.getPos()
        #self.enableMouse()
        
        #print self.camera
        
    def update(self, list):
        name = list[0]
        
        if name == "terrain":
            #print list
            row_id = list[1]
            self.insertTerrain(row_id, list[2:])
        elif name == "grassd":
            stage = list[1]
            pos = list[2], list[3]
            self.updateGrass(stage, pos)
        elif name == "rabbit":
            try:
                stage = list[1]
                id = list[2]
                old_pos = list[3], list[4]
                new_pos = list[5], list[6]
                self.updateRabbit(stage, id, old_pos, new_pos)
            except IndexError:
                print "IndexError", list
        elif name == "died":
            id = list[2]
            
            try:
                item = self.npc_dict[id]
            except KeyError:
                pass
            else:
                item.cleanup()
                item.removeNode()
                
                try:
                    self.npc_dict.pop(id)
                except KeyError:
                    pass
            
    def insertTerrain(self, row_id, terrain):
        if row_id == self.world_size[0]-1:
            self.generateMesh()
        
        else:
            
            self.cubes[row_id] = {}
            for i, item in enumerate(terrain):
                
                self.cubes[row_id][i] = {}
                self.cubes[row_id][i][0] = item
                
                for j in range(1, self.world_size[0]-1):
                    self.cubes[row_id][i][-j] = item
    
    def generateMesh(self):
        print "GENERATING MESH"
        
        ftime = time()
        mesh = MeshGenerator('world')
        
        for j in self.cubes:
            for i in self.cubes[j]:
                for k in self.cubes[j][i]:
         
                    id = self.cubes[j][i][k]
                   
                    if id == 0:
                        continue
                       
                    if j-1 not in self.cubes or self.cubes[j-1][i][k] == 0:
                        mesh.makeLeftFace(j, i, k, id)
                    if j+1 not in self.cubes or self.cubes[j+1][i][k] == 0:
                        mesh.makeRightFace(j, i, k, id)
                    if i-1 not in self.cubes[j] or self.cubes[j][i-1][k] == 0:
                        mesh.makeBackFace(j, i, k, id)
                    if i+1 not in self.cubes[j] or self.cubes[j][i+1][k] == 0:
                       
                        mesh.makeFrontFace(j, i, k, id)
                    if k-1 not in self.cubes[j][i] or self.cubes[j][i][k-1] == 0:
                        mesh.makeBottomFace(j, i, k, id)
                    if k+1 not in self.cubes[j][i] or self.cubes[j][i][k+1] == 0:
                        mesh.makeTopFace(j, i, k, id)
                       
        cube = render.attachNewNode(mesh.getGeomNode())
        cube.setTransparency(TransparencyAttrib.MAlpha)
        cube.setTexture(loader.loadTexture("./example/tex2.png"))
        cube.setScale(self.scale_factor, self.scale_factor, self.scale_factor)
        cube.setPos(self.scale_factor/2, self.scale_factor/2, self.scale_factor)
        
        print time() - ftime
    
    def insertTerrainOld(self, row_id, terrain):
        if row_id == self.world_size[0]:
            for i, row in enumerate(self.boxes_list):
                for j, box in enumerate(row):
                    pass
                    
            #cube = render.attachNewNode(self.mesh.getGeomNode())
            
        
        else:
            boxes_row = []
            
            for i, item in enumerate(terrain):
                if item == 1:
                    texture = loader.loadTexture("./images/grass_3.png")
                elif item == 2:
                    texture = loader.loadTexture("./images/water.png")
                elif item == 3:
                    texture = loader.loadTexture("./images/sand.png")
                elif item == 4:
                    texture = loader.loadTexture("./images/rock.png")
                
                box = loader.loadModel("models/box")
                box.reparentTo(render)
                box.setTexture(texture, 1)
                # horizontal_x, horizontal_y, vertical
                box.setScale(self.scale_factor, self.scale_factor, self.scale_factor)
                box.setPos(row_id*self.scale_factor, i*self.scale_factor, 0)
                
                boxes_row.append(box)
                
            self.boxes_list.append(boxes_row)
        
    def updateGrass(self, stage, pos):
        if stage == 0:
            texture = loader.loadTexture("./images/grass_0.png")
        elif stage == 1:
            texture = loader.loadTexture("./images/grass_1.png")
        elif stage == 2:
            texture = loader.loadTexture("./images/grass_2.png")
        elif stage == 3:
            texture = loader.loadTexture("./images/grass_3.png")
        
        box = self.boxes_list[pos[0]][pos[1]]
        box.setTexture(texture, 1)
        
    def updateRabbit(self, stage, id, old_pos, new_pos):
        x, y = new_pos
        rabbit = Actor("models/panda-model",
                                  {"walk" : "models/panda-walk4"})
        
        try:
            oldRabbit = self.npc_dict[id]
        except KeyError:
            pass
        else:
            oldRabbit.cleanup()
            oldRabbit.removeNode()
        
        rabbit.setScale(self.scale_factor * .0015, self.scale_factor * .0015, self.scale_factor * .0015)
        rabbit.setPos(x*self.scale_factor + (self.scale_factor/2), y*self.scale_factor + (self.scale_factor/2), self.scale_factor)
        rabbit.reparentTo(render)
        
        self.npc_dict[id] = rabbit
        
    def loadEnvironment(self):
        self.environ = self.loader.loadModel("models/environment")
        self.environ.reparentTo(self.render)
        self.environ.setScale(0.25, 0.25, 0.25)
        self.environ.setPos(-8, 42, 0)
        
    def loadActor(self):
        # Load and transform the panda actor.
        self.pandaActor = Actor("models/panda-model",
                                {"walk": "models/panda-walk4"})
        self.pandaActor.setScale(0.005, 0.005, 0.005)
        self.pandaActor.reparentTo(self.render)
        # Loop its animation.
        self.pandaActor.loop("walk")
        
    def moveActor(self):
        # Create the four lerp intervals needed for the panda to
        # walk back and forth.
        pandaPosInterval1 = self.pandaActor.posInterval(13,
                                                        Point3(0, -10, 0),
                                                        startPos=Point3(0, 10, 0))
        pandaPosInterval2 = self.pandaActor.posInterval(13,
                                                        Point3(0, 10, 0),
                                                        startPos=Point3(0, -10, 0))
        pandaHprInterval1 = self.pandaActor.hprInterval(3,
                                                        Point3(180, 0, 0),
                                                        startHpr=Point3(0, 0, 0))
        pandaHprInterval2 = self.pandaActor.hprInterval(3,
                                                        Point3(0, 0, 0),
                                                        startHpr=Point3(180, 0, 0))
        
        # Create and play the sequence that coordinates the intervals.
        self.pandaPace = Sequence(pandaPosInterval1,
                                  pandaHprInterval1,
                                  pandaPosInterval2,
                                  pandaHprInterval2,
                                  name="pandaPace")
        self.pandaPace.loop()
 
    # Define a procedure to move the camera.
    def spinCameraTask(self, task):
        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(64 * sin(angleRadians), -20*self.scale_factor * cos(angleRadians), 30)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont