/ Published in: Python
Expand |
Embed | Plain Text
import direct.directbase.DirectStart from pandac.PandaModules import loadPrcFileData from pandac.PandaModules import * from pandac.PandaModules import CollisionTraverser,CollisionNode from pandac.PandaModules import CollisionHandlerQueue,CollisionRay from pandac.PandaModules import Filename,GeomNode from pandac.PandaModules import PandaNode,NodePath,Camera,TextNode from pandac.PandaModules import Vec3,Vec4,BitMask32 from direct.gui.OnscreenText import OnscreenText from direct.actor.Actor import Actor from direct.task.Task import Task from direct.showbase.DirectObject import DirectObject from pandac.PandaModules import GeoMipTerrain import random, sys, os, math loadPrcFileData("", "interpolate-frames 1") # Figure out what directory this program is in. MYDIR=os.path.abspath(sys.path[0]) MYDIR=Filename.fromOsSpecific(MYDIR).getFullpath() # Function to put instructions on the screen. def addInstructions(pos, msg): return OnscreenText(text=msg, style=1, fg=(1,1,1,1), pos=(-1.3, pos), align=TextNode.ALeft, scale = .05,mayChange=True) # Function to put title on the screen. def addTitle(text): return OnscreenText(text=text, style=1, fg=(1,1,1,1), pos=(1.3,-0.95), align=TextNode.ARight, scale = .07) class World(DirectObject): def __init__(self): #-3,5,0 =) self.terrain = GeoMipTerrain("terrain") self.terrain.setHeightfield(Filename("models/heightmap2.bmp")) self.terrain.getRoot().setSz(15) self.terrain.getRoot().reparentTo(render) self.terrain.generate() self.terrain.getRoot().setPos(0,0,10) base.win.setClearColor(Vec4(0,0,0,1)) self.picker = CollisionTraverser() self.queue = CollisionHandlerQueue() # Post the instructions self.title = addTitle("Panda3D Tutorial: Roaming Ralph (Walking on Uneven Terrain)") self.inst1 = addInstructions(0.95, "[ESC]: Quit") self.inst2 = addInstructions(0.90, "[Left Arrow]: Rotate Ralph Left") self.inst3 = addInstructions(0.85, "[Right Arrow]: Rotate Ralph Right") self.inst4 = addInstructions(0.80, "[Up Arrow]: Run Ralph Forward") self.inst5 = addInstructions(0.75, "[Down Arrow]: Home") self.inst10 = addInstructions(0.60, "!!!") self.inst11 = addInstructions(0.50, "...") self.inst30 = addInstructions(0.10, " ") self.loadPicker() # Create the main character, Ralph ralphStartPos = (3,5) self.ralph = Actor("models/ralph", {"run":"models/ralph-run", "walk":"models/ralph-walk"}) self.ralph.reparentTo(render) self.ralph.setScale(.5) self.ralph.setTag('pickable', 'R') # Create a floater object. We use the "floater" as a temporary # variable in a variety of calculations. self.floater = NodePath(PandaNode("floater")) self.floater.reparentTo(render) # Accept the control keys for movement and rotation self.accept("escape", sys.exit) self.accept("shift-mouse1", self.collisionCheck,[1]) self.accept("shift-mouse3", self.collisionCheck,[2]) self.accept("shift-mouse2", self.collisionCheck,[3]) taskMgr.add(self.move,"moveTask") taskMgr.add(self.ZCord,"moveTask") # Set up the camera #~ base.disableMouse() base.camera.setPos(0,0,20) # Game state variables self.prevtime = 0 self.isMoving = False #load s light========================= plight = PointLight('plight') plight.setColor(VBase4(1, 1.3, 1.2, 1)) plnp = render.attachNewNode(plight) plnp.setPos(0, 0, 30) render.setLight(plnp) #~ ============================= #Records the state of the arrow keys def setKey(self, key, value): self.keyMap[key] = value #teleport ralph def teleport(self, pos): self.ralph.setPos(pos) #the same def move2(self, pos): self.ralph.setPos(pos) def loadPicker(self): #attach a CollisionRay node to the camera self.pickerNode = CollisionNode('mouseRay') self.pickerNP = camera.attachNewNode(self.pickerNode) self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask()) self.pickerRay = CollisionRay() self.pickerNode.addSolid(self.pickerRay) self.pickerNP.show() #add collision node (fromObject) to the traverser self.picker.addCollider(self.pickerNP, self.queue) self.picker.showCollisions(render) if base.mouseWatcherNode.hasMouse(): #get the mouse position mpos = base.mouseWatcherNode.getMouse() #aim the collision ray self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY()) self.inst30.destroy() self.inst30 = addInstructions(-0.50, str(mpos.getX())+" "+str(mpos.getY())) #fire the ray and record, in the queue, any collisions self.picker.traverse(render) if self.queue.getNumEntries() > 0: if self.queue.getNumEntries() == 1: self.mytext = "A collision with: _ " \ + str(self.queue.getEntry(0).getIntoNodePath()) +" at point of: _ " \ + str(self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath())) else: #organize the collisions from first hit to second... self.queue.sortEntries() #output the name of the node paths the ray collided with self.mytext = "A collision occured first with " \ + str(self.queue.getEntry(0).getIntoNodePath()) \ + " and then with " + str(self.queue.getEntry(1).getIntoNodePath()) self.onCollisionText.setText(self.mytext) def collisionCheck(self,but): if base.mouseWatcherNode.hasMouse(): #get the mouse position mpos = base.mouseWatcherNode.getMouse() #aim the collision ray self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY()) self.inst30.destroy() self.inst30 = addInstructions(-0.50, str(mpos.getX())+" "+str(mpos.getY())) #fire the ray and record, in the queue, any collisions self.picker.traverse(render) if self.queue.getNumEntries() > 0: if but ==1: #self.teleport(self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath())) self.moveTo = self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath()) self.ralph.loop("run") if but ==2: self.move2(self.queue.getEntry(0).getInteriorPoint(self.queue.getEntry(0).getIntoNodePath())) def move(self, task): #time saving elapsed = task.time - self.prevtime # save ralph's initial position so that we can restore it, # in case he falls off the map or runs into something. startpos = self.ralph.getPos() #mose moving =) thanks InternZero if hasattr(self, "moveTo"): self.ralph.lookAt(self.moveTo) self.ralph.setH(self.ralph, 180) backward = self.ralph.getNetTransform().getMat().getRow3(1) #~ backward.setZ(0) backward.normalize() self.ralph.setPos(self.ralph.getPos() - backward*(elapsed*5)) #stop when reach the destination point if self.ralph.getPos(render).almostEqual(self.moveTo, 0.05): #~ print "Comparison:", self.ralph.getPos(render), self.moveTo self.ralph.setPos(self.moveTo) self.isMoving = False self.ralph.pose("walk",5) backward = self.ralph.getNetTransform().getMat().getRow3(1) #~ backward.setZ(0) backward.normalize() del self.moveTo self.inst10.destroy() self.inst10 = addInstructions(0.55, str(self.ralph.getPos)+"(ralph position):") self.inst11.destroy() self.inst11 = addInstructions(0.50, str(self.ralph.getPos())) # Store the task time and continue. self.prevtime = task.time return Task.cont def ZCord(self, task): # Adjust ralph's Z coordinate. z=self.terrain.getElevation(self.ralph.getX(),self.ralph.getY())*self.terrain.getRoot().getSz() self.ralph.setPos(self.ralph.getX(),self.ralph.getY(),z) #suka rabotay!!! w = World() run()
You need to login to post a comment.
