def execute(self, data): try: self.playerId = data.getString() self.type = data.getUint16() self.x = data.getFloat32() self.y = data.getFloat32() self.z = data.getFloat32() #print "q" char = Character(self.world, self.type) char.actor.setPos(self.x,self.y,0) char.setPlayerId(self.playerId) charPresent = False for charTemp in self.world.characters: if charTemp.getPlayerId() == char.playerId: charPresent = True break if not charPresent: self.world.characters.append(char) print "ResponseCreateCharacter - ", self.playerId, " x:", self.x, " y:", self.y," z:", self.z #self.log('Received [' + str(Constants.RAND_STRING) + '] String Response') except: self.log('Bad [' + str(Constants.RAND_STRING) + '] String Response') print_exc()
class World(DirectObject): currentTime = 0 idleTime = 0 mySequence = None pandaPace = None jumpState = False isWalk = False previousPos = None #used to store the mainChar pos from one frame to another host = "" port = 0 characters = [] #Stores the list of all the others players characters def __init__(self): # Network Setup print "Object Created" self.characters = [] def setConnectionManager(self, connectionManager): self.cManager = connectionManager def initWorld(self, playerId, chosenCharId, x, y, h): self.keyMap = {"hello":0, "left":0, "right":0, "forward":0, "backward":0, "cam-left":0, "cam-right":0, "chat":0, "fast":0, "chat0": 0, "chat1": 0, "users": 0} base.win.setClearColor(Vec4(0,0,0,1)) # chat box self.chatbox = Chat(self.cManager, self) self.activeUsers = Users(self.cManager, self) taskMgr.add(self.message, 'message') # Post the instructions self.title = addTitle("Panda3D Tutorial: Roaming Ralph (Walking on the Moon)") self.inst1 = addInstructions(0.95, "[ESC]: Quit") self.inst2 = addInstructions(0.90, "[A]: Rotate Ralph Left") self.inst3 = addInstructions(0.85, "[D]: Rotate Ralph Right") self.inst4 = addInstructions(0.80, "[W]: Run Ralph Forward") self.inst4 = addInstructions(0.75, "[S]: Run Ralph Backward") self.inst6 = addInstructions(0.70, "[Left Arrow]: Rotate Camera Left") self.inst7 = addInstructions(0.65, "[Right Arrow]: Rotate Camera Right") self.inst8 = addInstructions(0.60, "[0]: Toggle Chat Broadcast") self.inst9 = addInstructions(0.55, "[1]: Toggle Private Chat - username/") self.inst10 = addInstructions(0.50, "[L]: List Connected Users") # 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) #Collision Code # Initialize the collision traverser. base.cTrav = CollisionTraverser() # Initialize the Pusher collision handler. self.pusher = CollisionHandlerPusher() self.bTrav = base.cTrav # Create the main character, Ralph self.mainCharRef = Character(self, chosenCharId, True) self.mainCharRef.setPlayerId(playerId) self.characters.append(self.mainCharRef) self.mainChar = self.mainCharRef.getActor() #Create second bike self.mainCharRef2 = Character(self, 3, True) self.mainCharRef2.setPlayerId(2) self.characters.append(self.mainCharRef2) self.mainChar2 = self.mainCharRef2.getActor() resetPos = True if (x == 0 and y == 0): resetPos = False if resetPos: self.mainChar.setPos(x, y, 0) self.mainChar.setH(h) self.cManager.sendRequest(Constants.CMSG_CREATE_CHARACTER, [playerId, chosenCharId, self.mainChar.getX(), self.mainChar.getY(), self.mainChar.getZ()]) self.previousPos = self.mainChar.getPos() taskMgr.doMethodLater(.1, self.updateMove, 'updateMove') # Creating Stationary spheres self.spheres = [] self.sphereCount = 3 for x in range(self.sphereCount): self.spheres.append(StationarySphere(self)) for sphere in self.spheres: taskMgr.add(sphere.rotateForAll, "rotateSphere") self.floater = NodePath(PandaNode("floater")) self.floater.reparentTo(render) 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) # Accept the control keys for movement and rotation self.accept("escape", self.quitGame) self.accept("a", self.setKey, ["left",1]) self.accept("d", self.setKey, ["right",1]) self.accept("w", self.setKey, ["forward",1]) self.accept("s", self.setKey, ["backward",1]) self.accept("lshift", self.setKey, ["fast", 1]) self.accept("arrow_left", self.setKey, ["cam-left",1]) self.accept("arrow_right", self.setKey, ["cam-right",1]) self.accept("a-up", self.setKey, ["left",0]) self.accept("d-up", self.setKey, ["right",0]) self.accept("w-up", self.setKey, ["forward",0]) self.accept("s-up", self.setKey, ["backward",0]) self.accept("lshift-up", self.setKey, ["fast", 0]) self.accept("arrow_left-up", self.setKey, ["cam-left",0]) self.accept("arrow_right-up", self.setKey, ["cam-right",0]) self.accept("h", self.setKey, ["hello",1]) self.accept("h-up", self.setKey, ["hello",0]) self.accept("0", self.setKey, ["chat0", 1]) self.accept("0-up", self.setKey, ["chat0", 0]) self.accept("1", self.setKey, ["chat1", 1]) self.accept("1-up", self.setKey, ["chat1", 0]) self.accept("l", self.setKey, ["users", 1]) self.accept("l-up", self.setKey, ["users", 0]) taskMgr.add(self.move,"moveTask") # Game state variables self.isMoving = False # Set up the camera base.disableMouse() base.camera.setPos(self.mainChar.getX(),self.mainChar.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)) 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 #Records the state of the arrow keys def setKey(self, key, value): self.keyMap[key] = value #print "In setKey", key, value self.cManager.sendRequest(Constants.CMSG_KEY, [key, value]) # Accepts arrow keys to move either the player or the menu cursor, # Also deals with grid checking and collision detection def getDist(self): mainCharX = self.mainChar.getPos().x mainCharY = self.mainChar.getPos().y pandaX = self.pandaActor2.getPos().x pandaY = self.pandaActor2.getPos().y dist = math.sqrt(abs(mainCharX-pandaX)**2 + abs(mainCharY-pandaY)**2) return dist def move(self, task): # If the camera-left key is pressed, move camera left. # If the camera-right key is pressed, move camera right. base.camera.lookAt(self.mainChar) if (self.keyMap["cam-left"]!=0): base.camera.setX(base.camera, -20 * globalClock.getDt()) if (self.keyMap["cam-right"]!=0): base.camera.setX(base.camera, +20 * globalClock.getDt()) # save mainChar's initial position so that we can restore it, # in case he falls off the map or runs into something. startpos = self.mainChar.getPos() starthpr = self.mainChar.getHpr() # If left-shift is pressed, speed-up ralph ralphSpeed = 1 if(self.keyMap["fast"]==1): ralphSpeed = 3 # If a move-key is pressed, move ralph in the specified direction. if (self.keyMap["left"]!=0): self.mainChar.setH(self.mainChar.getH() + 300 * globalClock.getDt() * ralphSpeed) if (self.keyMap["right"]!=0): self.mainChar.setH(self.mainChar.getH() - 300 * globalClock.getDt() * ralphSpeed) if (self.keyMap["forward"] != 0 and self.mainCharRef.type == 2): self.mainChar.setY(self.mainChar, -500 * globalClock.getDt() * ralphSpeed) elif (self.keyMap["forward"] != 0 and self.mainCharRef.type == 3): self.mainChar.setZ(self.mainChar, 25 * globalClock.getDt() * ralphSpeed) elif (self.keyMap["forward"] != 0): self.mainChar.setY(self.mainChar, -25 * globalClock.getDt() * ralphSpeed) if (self.keyMap["backward"] != 0 and self.mainCharRef.type == 2): self.mainChar.setY(self.mainChar, 500 * globalClock.getDt() * ralphSpeed) elif (self.keyMap["backward"] != 0 and self.mainCharRef.type == 3): self.mainChar.setZ(self.mainChar, -25 * globalClock.getDt() * ralphSpeed) elif (self.keyMap["backward"] != 0): self.mainChar.setY(self.mainChar, 25 * globalClock.getDt() * ralphSpeed) # If ralph is moving, loop the run animation. # If he is standing still, stop the animation. if (self.keyMap["forward"] != 0) or (self.keyMap["backward"] != 0) or (self.keyMap["left"] != 0) or ( self.keyMap["right"] != 0): if self.isMoving is False : self.isMoving = True if self.mainCharRef.type != 3: self.mainChar.loop("run") else: if self.isMoving: self.isMoving = False if self.mainCharRef.type != 3: self.mainChar.stop() self.mainChar.pose("walk",5) # Detecting collisions only when ralph is moving if self.isMoving: # Collision With Spheres isCollidingWithSphere = False for sphere in self.spheres: if self.mainCharRef.isColliding(sphere.actor.getPos(), sphere.boundingRadius): isCollidingWithSphere = True if isCollidingWithSphere: self.mainChar.setPos(startpos) self.mainChar.setHpr(starthpr) # Collision With Other Ralphs isCollidingWithOtherRalphs = False for chacracter in self.characters: if not chacracter.isMainChar and self.mainCharRef.isColliding(chacracter.actor.getPos(), chacracter.boundingRadius): isCollidingWithOtherRalphs = True if isCollidingWithOtherRalphs: self.mainChar.setH(self.mainChar.getH() - 180) # If the camera is too far from ralph, move it closer. # If the camera is too close to ralph, move it farther. camvec = self.mainChar.getPos() - base.camera.getPos() camvec.setZ(0) camdist = camvec.length() camvec.normalize() if (camdist > 10.0): base.camera.setPos(base.camera.getPos() + camvec*(camdist-10)) camdist = 10.0 if (camdist < 5.0): base.camera.setPos(base.camera.getPos() - camvec*(5-camdist)) camdist = 5.0 # The camera should look in ralph's direction, # but it should also try to stay horizontal, so look at # a floater which hovers above ralph's head. self.floater.setPos(self.mainChar.getPos()) self.floater.setZ(self.mainChar.getZ() + 2.0) base.camera.lookAt(self.floater) return task.cont def startConnection(self): """Create a connection to the remote host. If a connection cannot be created, it will ask the user to perform additional retries. """ if self.cManager.connection == None: if not self.cManager.startConnection(): return False return True def message(self, task): # hide all chatboxes if self.keyMap["chat0"] != 0: if self.chatbox.getVisible() is False: self.chatbox.setVisible(True) self.chatbox.show(0) else: self.chatbox.setVisible(False) self.chatbox.hide() self.keyMap["chat0"] = 0 if self.keyMap["chat1"] != 0: if self.chatbox.getVisible() is False: self.chatbox.setVisible(True) self.chatbox.show(1) else: self.chatbox.setVisible(False) self.chatbox.hide() self.keyMap["chat1"] = 0 if self.keyMap["users"] != 0: self.activeUsers.toggle() self.keyMap["users"] = 0 return task.cont def updateMove(self, task): if self.isMoving == True : # moving = self.mainChar.getPos() - self.previousPos moving = self.mainChar.getPos(); self.cManager.sendRequest(Constants.CMSG_MOVE, [moving.getX() ,moving.getY(),moving.getZ(), self.mainChar.getH()]) #self.cManager.sendRequest(Constants.RAND_FLOAT, 1.0) self.previousPos = self.mainChar.getPos() return task.again def quitGame(self): self.cManager.closeConnection() sys.exit()