class Logger: """ Prints messages to the consol and/or file and or gui element """ def __init__(self, gui=None, out_file=None): self.notify = DirectNotify().newCategory('a4p') self.gui = gui self.out_file = None if out_file: self.out_file = open(path + out_file, 'a') self.out_file.write('---<Opening file on: ' + str(datetime.now()) + ' >---\n') self.debug('Logger is now logging') self.debug('Logger gui is ' + str(gui)) self.debug('Logger out_file is ' + str(out_file)) def error(self, msg): if self.gui: self.gui.showMsg('Error: ' + msg) if self.out_file: self.out_file.write('Error:' + msg + '\n') self.notify.warning('Error: ' + msg) def warning(self, msg): self.notify.warning(msg) if self.gui: self.gui.showMsg('Warning: ' + msg) if self.out_file: self.out_file.write('Warning: ' + msg + '\n') def info(self, msg): self.notify.info(msg) if self.gui: self.gui.showMsg(msg) if self.out_file: self.out_file.write(msg) def debug(self, msg): self.notify.debug(msg) if self.gui: self.gui.showMsg('Debug: ' + msg) if self.out_file: self.out_file.write('Debug: ' + msg + '\n') def exception(self, msg): if self.gui: self.gui.showMsg('Exception: ' + msg) if self.out_file: self.out_file.write('Exception: ' + msg + '\n') self.notify.error(msg) def closeLogFile(self): if self.out_file: self.out_file.write('---<Closing file on: ' + str(datetime.now()) + ' >---\n') self.out_file.close() self.out_file = None
class Logging(object): def __init__(self): #create a new notify category self._notify = DirectNotify().newCategory("CamMovement") self._notify.info("Put some informational text here.") self._notify.debug("Put some informational text here.") self._notify.warning("Put some informational text here.") self._notify.error( "Put some informational text here.") #raise an exeption
def setup(name='Server Name'): log = DirectNotify().newCategory("Allegiance-Server-" + name) log.debug("Loading space drive") spacedrive.init(run_server=True, run_client=False, log_level='debug', window_title='Allegiance 2') log.info("Setting up server network") spacedrive.init_server_net(NetworkSystem, port=47624)
class Logger: """ Prints messages to the consol and/or file and or gui element """ def __init__(self, gui=None, out_file=None): self.notify = DirectNotify().newCategory('a4p') self.gui=gui self.out_file=None if out_file: self.out_file=open(path+out_file,'a') self.out_file.write('---<Opening file on: '+str(datetime.now())+' >---\n') self.debug('Logger is now logging') self.debug('Logger gui is '+str(gui)) self.debug('Logger out_file is '+str(out_file)) def error(self, msg): if self.gui: self.gui.showMsg('Error: '+msg) if self.out_file: self.out_file.write('Error:'+msg+'\n') self.notify.warning('Error: '+msg) def warning(self, msg): self.notify.warning(msg) if self.gui: self.gui.showMsg('Warning: '+msg) if self.out_file: self.out_file.write('Warning: '+msg+'\n') def info(self, msg): self.notify.info(msg) if self.gui: self.gui.showMsg(msg) if self.out_file: self.out_file.write(msg) def debug(self, msg): self.notify.debug(msg) if self.gui: self.gui.showMsg('Debug: '+msg) if self.out_file: self.out_file.write('Debug: '+msg+'\n') def exception(self, msg): if self.gui: self.gui.showMsg('Exception: '+msg) if self.out_file: self.out_file.write('Exception: '+msg+'\n') self.notify.error(msg) def closeLogFile(self): if self.out_file: self.out_file.write('---<Closing file on: '+str(datetime.now())+' >---\n') self.out_file.close() self.out_file=None
class Logger: def __init__(self): self.notify = DirectNotify().newCategory("core") def error(self, msg): self.notify.warning(msg) def warning(self, msg): self.notify.warning(msg) def info(self, msg): self.notify.info(msg) def debug(self, msg): self.notify.debug(msg) def exception(self, msg): self.notify.error(msg)
def setup(): log = DirectNotify().newCategory("Allegiance-Client") log.debug("Loading space drive") spacedrive.init(run_server=False, run_client=True, log_level='debug', window_title='Allegiance 2') log.info('Setting up graphics') #spacedrive.init_graphics(debug_mouse=False) log.info("Setting up client gui") spacedrive.init_gui() spacedrive.base.wireframe = False spacedrive.base.accept("f3", toggleSceneWireframe) spacedrive.gui_system.setup_screen(MainMenu()) log.info("Setting up client network") spacedrive.init_server_net(NetworkSystem)
def setup(): log = DirectNotify().newCategory("Allegiance-Client") log.debug("Loading space drive") spacedrive.init(run_server=False, run_client=True, log_level='debug', window_title='Allegiance 2') log.info('Setting up graphics') #spacedrive.init_graphics(debug_mouse=False) log.info("Setting up client gui") spacedrive.init_gui() spacedrive.base.wireframe = False spacedrive.base.accept("f3", toggleSceneWireframe) spacedrive.gui_system.setup_screen(MainMenu()) log.info("Setting up client network") spacedrive.init_server_net(NetworkSystem)
class Logger: def __init__(self): self.notify = DirectNotify().newCategory("core") def error(self, msg): self.notify.warning(msg) def warning(self, msg): self.notify.warning(msg) def info(self, msg): self.notify.info(msg) def debug(self, msg): self.notify.debug(msg) def exception(self, msg): self.notify.error(msg)
class Menu(object): ''' ''' def __init__(self, parent): self._notify = DirectNotify().newCategory("Menu") self._notify.info("New Menu-Object created: %s" %(self)) #Font self.font = DynamicTextFont(FONT) self.font.setRenderMode(TextFont.RMSolid) self.KEY_DELAY = 0.15 self.player_buttonpressed = [] self._parent = parent self._players = parent.players self._devices = parent.devices taskMgr.add(self.fetchAnyKey, "fetchAnyKey") def showStartScreen(self): ''' the first screen with "press any Key" the device with the first key press will be the first player ''' self._notify.info("Initializing StartScreen") self.wii = [] #StartScreen Node self.startNode = NodePath("StartNode") self.startNode.reparentTo(render) self.startNode.setPos(-5,15,3) #Headline model headline = loader.loadModel("data/models/logo.egg") headline.setX(4.7) headline.setY(-4) headline.setZ(-2) headline.setP(90) headline.reparentTo(self.startNode) #Press any key text presskey = TextNode("PressAnyKey") presskey.setFont(self.font) presskey.setText(_("Press any key!!")) textNodePath = NodePath("PressAnyNode") textNodePath.attachNewNode(presskey) textNodePath.setPos(0,10,-9.5) textNodePath.reparentTo(self.startNode) #Show the start screen self.startNode.show() #LICHT plight = PointLight('plight') plight.setColor(VBase4(10, 10, 10, 1)) plnp = self.startNode.attachNewNode(plight) plnp.setPos(20, -800, 30) self.startNode.setLight(plnp) #Camera self.camera = base.makeCamera(base.win) self._notify.info("StarScreen initialized") # ----------------------------------------------------------------- def fetchAnyKey(self, task): ''' Return the first device with the first key stroke ''' for i in xrange(len(self._devices.devices)): if self._devices.devices[i].boost: #Kill Camera self.camera.node().setActive(False) #Kill Node self.startNode.removeNode() #Start the menu self.menu = MainMenu(self._parent, self.newGame, self._devices.devices[i], self._devices) self.menu.menuMain() return task.done return task.cont # ----------------------------------------------------------------- def newGame(self): ''' The select vehicle screen ''' base.enableParticles() self.countdown = COUNTDOWN_START #the countdown, when its over the game can be started self._notify.info("Initializing new game") #GlobPattern if we need a Panda Class self.vehicle_list = glob.glob("data/models/vehicles/*.egg") for index in range(len(self.vehicle_list)): self.vehicle_list[index] = Filename.fromOsSpecific(self.vehicle_list[index]).getFullpath() self._notify.debug("Vehicle list: %s" %(self.vehicle_list)) self.platform = loader.loadModel("data/models/platform.egg") #Loading-Text that gets displayed when loading a model text = TextNode("Loading") text.setFont(self.font) text.setText(_("Loading...")) text.setAlign(TextProperties.ACenter) self.loading = NodePath("LoadingNode") self.loading.attachNewNode(text) self.loading.setPos(0,0,2) #The countdown, its possible to start the game when its 0 text = TextNode("Countdown") text.setFont(self.font) text.setAlign(TextProperties.ACenter) text.setText(_(str(COUNTDOWN_START))) self.countdown_node = NodePath("Countdown") self.countdown_node.attachNewNode(text) self.countdown_node.setPos(0,0,4) #PreLoad the description that gets displayed when loading a model text = TextNode("name") text.setFont(self.font) text.setText(_("Loading...")) text.setAlign(TextProperties.ACenter) self.attributes = NodePath("AttributeNode") self.attributes.attachNewNode(text) self.unusedDevices = self._devices.devices[:] taskMgr.add(self.collectPlayer, "collectPlayer") #taskMgr.add(self.collectWii, "collectWii") self.screens = [] taskMgr.add(self.selectVehicle, "selectVehicle") self.color_red = Vec4(1,0,0,0) self.color_green = Vec4(0,1,0,0) self._notify.info("New game initialized") # ----------------------------------------------------------------- def selectVehicle(self, task): ''' The vehicle select and rotate task ''' #Set the countdown and hide, if > 3 self.countdown -= globalClock.getDt() if self.countdown <=0: self.countdown_node.getChild(0).node().setText(_("Go")) self.countdown_node.setColor(self.color_green) elif self.countdown <=3: self.countdown_node.getChild(0).node().setText(str(int(round((self.countdown+0.5))))) self.countdown_node.setColor(self.color_red) self.countdown_node.show() else: self.countdown_node.hide() heading = self.loading.getH() -(30 * globalClock.getDt()) self.loading.setH(heading) for player in self._players: if player.vehicle.model != None: player.vehicle.model.setH(heading) if self.player_buttonpressed[self._players.index(player)] < task.time and not player.vehicle.model_loading: if player.device.directions[0] < -0.8: self._parent.menuselect.play() self.countdown = COUNTDOWN_START self.player_buttonpressed[self._players.index(player)] = task.time + self.KEY_DELAY index = self.vehicle_list.index("data/models/vehicles/%s" %(player.vehicle.model.getName()))-1 self._notify.debug("Previous vehicle selected: %s" %(index)) player.vehicle.model_loading = True player.vehicle.model.hide() player.camera.camera.getParent().find("AttributeNode").hide()#Hide the attributes self.loading.instanceTo(player.camera.camera.getParent()) loader.loadModel(self.vehicle_list[index], callback = player.setVehicle) elif player.device.directions[0] > 0.8: self._parent.menuselect.play() self.countdown = COUNTDOWN_START self.player_buttonpressed[self._players.index(player)] = task.time + self.KEY_DELAY index = self.vehicle_list.index("data/models/vehicles/%s" %(player.vehicle.model.getName()))+1 self._notify.debug("Next vehicle selected: %s" %(index)) if index >= len(self.vehicle_list): index = 0 player.vehicle.model_loading = True player.vehicle.model.hide() player.camera.camera.getParent().find("AttributeNode").hide()#Hide the attributes self.loading.instanceTo(player.camera.camera.getParent()) loader.loadModel(self.vehicle_list[index], callback = player.setVehicle) if player.device.directions[1] > 0.8: self.countdown = COUNTDOWN_START self._parent.menuselect.play() self.player_buttonpressed[self._players.index(player)] = task.time + self.KEY_DELAY self._notify.debug("Next color selected") a = player.vehicle.model.findAllTextures() tex = a.findTexture(player.vehicle.model.getName()[:-4]) self.rotateHue(tex, 0.1) elif player.device.directions[1] < -0.8: self._parent.menuselect.play() self.countdown = COUNTDOWN_START self.player_buttonpressed[self._players.index(player)] = task.time + self.KEY_DELAY self._notify.debug("Next color selected") a = player.vehicle.model.findAllTextures() tex = a.findTexture(player.vehicle.model.getName()[:-4]) self.rotateHue(tex, -0.1) return task.cont # ----------------------------------------------------------------- def rotateHue(self, tex, value=0.1): ''' ''' img = PNMImage() tex.store(img) for y in xrange(img.getReadYSize()): for x in xrange(img.getReadXSize()): r, g, b = img.getXel(x,y) h, s, v = colorsys.rgb_to_hsv(r, g, b) h += value if h < 0: h += 360 r, g, b = colorsys.hsv_to_rgb(h, s, v) img.setXel(x,y,r,g,b) tex.load(img) # ----------------------------------------------------------------- def collectWii(self, task): ''' Collect wiimotes task ''' try: print 'Put Wiimote in discoverable mode now (press 1+2)...' wiimote = cwiid.Wiimote() self.wiimoteX.append(wiimote) print len(self.wiimoteX) except: pass return task.cont # ----------------------------------------------------------------- def collectPlayer(self, task): ''' Wait until all players are ready ''' if len(self._players) > 0 and self.player_buttonpressed[0] < task.time: if self._players[0].device.boost and self.countdown <= 0: loading = False for player in self._players: if player.vehicle.model_loading: loading = True break self._notify.debug("Loading vehicle: %s" %(loading)) if not loading: taskMgr.remove("selectVehicle") self.track = trackgen3d.Track3d(1000, 1800, 1600, 1200, 5)#len(self._players)) self.streetPath = render.attachNewNode(self.track.createRoadMesh()) #self.borderleftPath = render.attachNewNode(self.track.createBorderLeftMesh()) self.borderleftPath = render.attachNewNode(self.track.createBorderLeftMesh()) self.borderrightPath = render.attachNewNode(self.track.createBorderRightMesh()) self.borderleftcollisionPath = NodePath(self.track.createBorderLeftCollisionMesh()) self.borderrightcollisionPath = NodePath(self.track.createBorderRightCollisionMesh()) ##self.borderPath = render.attachNewNode(self.track.createBorderMesh()) textures = ["tube", "tube2", "street"] tex = textures[random.randint(0, len(textures)-1)] roadtex = loader.loadTexture('data/textures/'+tex+'.png') bordertex = loader.loadTexture('data/textures/border.png') self.streetPath.setTexture(roadtex) self.borderleftPath.setTexture(bordertex) self.borderrightPath.setTexture(bordertex) #self.streetPath = loader.loadModel('data/models/Street.egg') ##self.streetPath = loader.loadModel('data/models/Street.egg') #tex = loader.loadTexture('data/models/StreetTex.png') #self.nodePath.setTexture(tex) self._parent.startGame(self.streetPath,self.borderleftPath,self.borderrightPath, self.track.trackpoints, self.borderleftcollisionPath, self.borderrightcollisionPath) return task.done for device in self.unusedDevices: if device.boost: self.countdown = COUNTDOWN_START self.player_buttonpressed.append(0) self._parent.addPlayer(device) #Set the PlayerCam to the Vehicle select menu Node vehicleSelectNode = NodePath("VehicleSelectNode") self._players[-1].camera.camera.reparentTo(vehicleSelectNode) #Light, that casts shadows plight = Spotlight('plight') plight.setColor(VBase4(10.0, 10.0, 10.0, 1)) if (base.win.getGsg().getSupportsBasicShaders() != 0): pass ##plight.setShadowCaster(True, 2048, 2048)#enable shadows for this light ##TODO wegen Linux #Light plight.getLens().setFov(80) plnp = vehicleSelectNode.attachNewNode(plight) plnp.setPos(2, -10, 10) plnp.lookAt(0,0,0) vehicleSelectNode.setLight(plnp) ## vehicleSelectNode.setShaderAuto()#enable autoshader so we can use shadows #Light ambilight = AmbientLight('ambilight') ambilight.setColor(VBase4(0.2, 0.2, 0.2, 1)) vehicleSelectNode.setLight(vehicleSelectNode.attachNewNode(ambilight)) self.platform.instanceTo(vehicleSelectNode) #Load the platform #instance shown text self.countdown_node.instanceTo(vehicleSelectNode) #Instance the Countdown self.loading.instanceTo(vehicleSelectNode) #Show the Loading-Text self.attributes.copyTo(vehicleSelectNode).hide() self._players[-1].vehicle.model_loading = True #start loading the model loader.loadModel(self.vehicle_list[0], callback = self._players[-1].setVehicle) self._notify.debug("Loading initial vehicle: %s" %(self.vehicle_list[0])) self.unusedDevices.remove(device) self.player_buttonpressed[-1] = task.time + self.KEY_DELAY #Add the Skybox skybox = loader.loadModel("data/models/skybox.egg") t = Texture() t.load(PNMImage("data/textures/skybox_hangar.png")) skybox.setTexture(t) skybox.setBin("background", 1) skybox.setDepthWrite(0) skybox.setDepthTest(0) skybox.setLightOff() skybox.setScale(10000) skybox.reparentTo(vehicleSelectNode) for player in self._players: if self.player_buttonpressed[self._players.index(player)] < task.time: if player.device.use_item: self.countdown = COUNTDOWN_START self._notify.debug("Removing player: %s" %(player)) self.unusedDevices.append(player.device) self.player_buttonpressed.pop(self._players.index(player)) self._parent.removePlayer(player) return task.cont
path).read() with open(directory + winpath, 'wb') as handler: handler.write(img_data) handler.close() fae = {"path": self.path, "version": self.version} temp_files["image"].append(fae) fileManagerOutputLog.debug("File %s downloaded successfully." % self.path) except URLError: fileManagerOutputLog.warning( "Couldn't download file. Maybe try checking your internet connection?" ) print("") fileManagerOutputLog.debug("Have 8 files to check or download.") print("") download_file = txtfile(path='lib/launcherbase.py', version=3) download_file = txtfile(path='lib/world.py', version=1) download_file = txtfile(path='assets/concrete.egg', version=1) download_file = imagefile(path='assets/concrete.png', version=1) download_file = imagefile(path='assets/generating_level.png', version=1) download_file = txtfile(path='lib/chunk.py', version=1) download_file = txtfile(path='lib/block.py', version=2) gherkin_module = txtfile(path='lib/gherkin.py', version=1) import lib.world as world print("") fileManagerOutputLog.debug("8 files were found or downloaded successfully.")
class PlaneCamera(FSM, DirectObject): """Give this class a plane as argument and it will create some nodes around it which you can parent the camera to (if there are no such nodes yet). Keep in mind, that it only uses base.camera the whole time - no other cams are involved. Usage: plane_camera = PlaneCamera(aeroplane) plane_camera.setView("ThirdPerson") plane_camera.setView("Next") """ def __init__(self, parent): """Arguments: parent -- Aeroplane which the camera should follow """ # Used for debugging. Verbosity is set in config file. # Usually this is called self.notify, but in this case it would # override FSM's own. self.notifier = DirectNotify().newCategory("azure-camera") self.parent = parent # Replaced by a NodePath with all available cameras as children and # plane node as parent. self.cameras = None #if parent.__class__.__name__ is not "Aeroplane": if not isinstance(self.parent, Aeroplane): raise ParamError, "Parent must be an Aeroplane instance, " + \ "but is %s" % type(self.parent) FSM.__init__(self, "PlaneCamera: %s" % self.parent.name) DirectObject.__init__(self) try: self.camera = base.camera except: raise BaseMissing self.cameras = self.parent.node.find("cameras") if self.cameras.isEmpty(): self.createCamNodes() self.updateCamArray() self.sideview_direction = 0 # Set up the default camera self.setView("ThirdPerson") def createCamNodes(self): """Creates a few empty nodes around a plane which the camera might be parented to. It looks if there are cameras inside the model file and uses those if possible. Where everything named "camera CamType" is considered a camera. At least ThirdPerson, FirstPerson and Cockpit should be defined inside the egg file, otherwise some guessed defaults are taken. """ # Look for cameras inside the model (loaded egg file) self.cameras = NodePath("cameras") found_cams = self.parent.node.findAllMatches("**/camera ?*") found_cams.removeDuplicatePaths() found_cams.reparentTo(self.cameras) if not found_cams.isEmpty(): self.notifier.info("Cameras found under model:\n%s" % found_cams) else: self.notifier.info("No cameras found under model.") # FirstPerson camera is a must-have. Set up a guessed one if none # defined yet. if self.cameras.find("camera FirstPerson").isEmpty(): assert self.notifier.debug("No first person camera found in %s. " "Guessing best position." % self.parent.name) first_person = NodePath("camera FirstPerson") # TODO: Guess best position based on bounding box. first_person.setY(5) first_person.reparentTo(cameras) # ThirdPerson camera is a must-have. Set up a guessed one if none # defined yet. if self.cameras.find("camera ThirdPerson").isEmpty(): assert self.notifier.debug("No third person camera found in %s. " "Guessing best position." % self.parent.name) third_person = NodePath("camera ThirdPerson") # TODO: Guess best position based on bounding box. third_person.setPos(0, -30, 5) #third_person.setP(-80) third_person.reparentTo(cameras) # Cockpit needs to be accurate. Don't try to guess it. if self.cameras.find("camera Cockpit").isEmpty(): assert self.notifier.debug("No cockpit camera found in " "%s. Cockpit camera disabled." % self.parent.name) self.sideview_cam = NodePath("camera Sideview") self.sideview_cam.reparentTo(render) # Store the cams at parent node.. # You can edit the camera nodes from outside as well. # If you attach new camera nodes, though, you'll have to call this # function again. self.cameras.reparentTo(self.parent.node) def updateCamArray(self, cameramodes=None): """Set the cameras which next and previous will switch to. Expects a list or tuple. Defaults to all available cameras.""" a = [] if not cameramodes: for c in self.cameras.getChildren(): if c.getName().startswith("camera "): a.append(c.getName().strip("camera ")) self.setStateArray(a) else: self.setStateArray(cameramodes) def getView(self): """Returns the current view mode.""" return self.getCurrentOrNextState() def setView(self, mode, *args): """Convenience function.""" return self.request(mode, args) def defaultEnter(self, *args): """Executed by the FSM every time an undefined state is entered. Note: this function is called AFTER the responsible filter.""" assert self.notifier.debug("Changing state from %s to %s with args: %s." % (self.oldState, self.newState, args)) request = self.newState target_cam = self.cameras.find("camera " + request) if not target_cam.isEmpty(): try: self.camera.reparentTo(target_cam) self.camera.setPosHpr(0, 0, 0, 0, 0, 0) except: self.notifier.warning( "Ok, now this really shouldn't happen! Filter said the " "camera is there and enter can't find it...") def defaultFilter(self, request, args): """Executed by the FSM every time an undefined state is requested.""" assert self.notifier.debug("Requested %s with args: %s" % (request, args)) self.camera.setPosHpr(0, 0, 0, 0, 0, 0) # Always available. if request == "Off": # implemented in FSM.py return (request,) + args if request == "Next": # implemented in FSM.py return self.requestNext(args) if request == "Prev": # implemented in FSM.py return self.requestPrev(args) if request == "Detached": return (request,) + args if request == "Sideview": return (request,) + args # Depending on airplane. if not self.cameras.find("camera " + request).isEmpty(): # TODO(Nemesis13, 26.10.09): add some nice camera transition return (request,) + args assert self.notifier.info("Sorry, no %s camera found." % request) return None def enterOff(self, *args): """Clean up everything by reparenting the camera to the airplane.""" self.camera.reparentTo(self.parent.node) self.camera.setPosHpr(0, 0, 0, 0, 0, 0) def enterSideview(self, *args): self.sideview_direction += 90 self.camera.reparentTo(self.sideview_cam) self.camera.setY(-30) self.sideview_cam.setH(self.sideview_direction) self.addTask(self.updateSideview, "sideview camera", taskChain="world") def exitSideview(self, *args): self.removeTask("sideview camera") def updateSideview(self, task): self.sideview_cam.setPos(self.parent.node.getPos()) return Task.cont def enterDetached(self, *args): """Lets the camera view the plane from far away.""" self.camera.reparentTo(render) self.camera.setPosHpr(0, 0, 10, 0, 0, 0) self.addTask(self.updateDetachedCam, "detached camera", taskChain="world") def exitDetached(self, *args): self.removeTask("detached camera") def updateDetachedCam(self, task): """Updates camera position and rotation for Detached camera.""" try: self.camera.lookAt(self.parent.node) except: self.notifier.warning("Error on detached cam task. Exit.") return Task.done return Task.cont def enterThirdPerson(self, *args): """Lets the camera view the plane from far away.""" self._hist = [] self.camera.reparentTo(self.cameras.find("camera ThirdPerson")) self.addTask(self.updateThirdPersonCam, "third person camera", taskChain="world") #print "entering third person" def exitThirdPerson(self, *args): self.removeTask("third person camera") del self._hist #print "third person exited" def updateThirdPersonCam(self, task): """Updates camera position and rotation for ThirdPerson camera.""" #speed = self.parent.speed() #plane = self.parent.node #self._hist.insert(0, [task.time, camera.getPos(plane)]) #while len(self._hist) > 50: # self._hist.pop() # #for snapshot in self._hist: # if snapshot[0] > task.time: # break #time_delta = snapshot[0] - task.time #self.camera.setPos(plane, snapshot[1]) #print snapshot #self.camera.setPos(render, snapshot[1]) #self.camera.lookAt(plane, (0, 20+1*speed, 0)) #self.camera.setY(5+0.1*speed) #self.camera.setZ(5-0.1*speed) return Task.cont def destroy(self): self.removeAllTasks() self.demand("Off")
pass def close(self): pass def read(self): pass def write(self): pass try: filelistfile = open(directory + normpath("assets/files.dat"), 'rb') files = pickle.load(filelistfile) fileManagerOutputLog.debug("Successfully retrieved the file array.") filelistfile.close() except (EOFError, FileNotFoundError, OSError): try: filelistfile = open(directory + normpath("assets/files.dat"), 'wb') files = { # This is the filearray. It stores all the information needed to find other files, whether "binary": [ # binary or normal text. { # This is a file entry as provided by the downloadfile class. This entry belongs to the file array. "path": "assets/files.dat", "version": 1 } ], "txt": [], "image": [] }
class Game(ShowBase): ''' ''' def __init__(self, *args): ''' ''' #loadPrcFileData("", "fullscreen 0\n win-size 1280 720") #loadPrcFileData("", "want-pstats 1\n pstats-host 127.0.0.1\n pstats-tasks 1\n task-timer-verbose 1") ## loadPrcFileData("", "sync-video #f") loadPrcFileData( "", "default-directnotify-level debug\n notify-level-x11display fatal\n notify-level-Game debug\n notify-level-Menu debug\n notify-level-Vehicle debug" ) ShowBase.__init__(self) ##base.toggleWireframe() self._notify = DirectNotify().newCategory("Game") self._notify.info("New Game-Object created: %s" % (self)) base.setBackgroundColor(0, 0, 0) base.setFrameRateMeter(True) #Show the Framerate base.camNode.setActive(False) #disable default cam self.disableMouse() #disable manual camera-control ## render.setShaderAuto() self.music = base.loader.loadSfx("data/music/track1.ogg") self.menuselect = base.loader.loadSfx("data/sounds/menuselect.wav") #Laps self.laps = 3 #the Laps self.starttime = 0 #Time the Game starts self.winingPlayer = 0 # load the settings self.settings = settings.Settings() self.settings.loadSettings("user/config.ini") gettext.install( "ragetracks", "data/language") #, unicode=True) #installs the system language #trans = gettext.translation("ragetracks", "data/language", ["de"]) #installs choosen language #trans.install() #usage: print _("Hallo Welt") #Fullscreen if self.settings.fullscreen: wp = WindowProperties() wp.setFullscreen(self.settings.fullscreen) wp.setOrigin(0, 0) wp.setTitle("RageTracks") wp.setSize(int(base.pipe.getDisplayWidth()), int(base.pipe.getDisplayHeight())) base.win.requestProperties(wp) #enable anti-aliasing if self.settings.antialias: loadPrcFileData("", "framebuffer-multisample 1\n multisamples 2") render.setAntialias(AntialiasAttrib.MMultisample) #Initialize needed variables and objects self.players = [] #holds the player objects self.TRACK_GRIP = 0.5 self.LINEAR_FRICTION = 0.02 self.ANGULAR_FRICTION = 0.02 self.splitscreen = splitscreen.SplitScreen(0) #Initialize Physics (ODE) self.world = OdeWorld() self.deltaTimeAccumulator = 0.0 #this variable is necessary to track the time for the physics self.stepSize = 1.0 / 100.0 # This stepSize makes the simulation run at 60 frames per second #Initialize Collisions (ODE) self.space = OdeSimpleSpace() #Initialize the surface-table, it defines how objects interact with each other self.world.initSurfaceTable(1) self.world.setSurfaceEntry(0, 0, 150, 100.0, 100.1, 0.9, 0.00001, 0.0, 0.002) self.space.setAutoCollideWorld(self.world) self.contactgroup = OdeJointGroup() self.space.setAutoCollideJointGroup(self.contactgroup) # initialize the input devices self.devices = inputdevice.InputDevices( self.settings.getInputSettings()) #render.setShaderAuto() startgame = True #Start the Game for arg in sys.argv: if arg == "--ep": startgame = False if sys.argv[sys.argv.index(arg) + 1] == "startGame": player = self.addPlayer(self.devices.devices[0]) import glob self.vehicle_list = glob.glob("data/models/vehicles/*.egg") #start loading the model base.enableParticles() n = 0 if len(sys.argv) >= sys.argv.index(arg) + 3: try: n = int(sys.argv[sys.argv.index(arg) + 2]) except: n = 0 if n >= len(self.vehicle_list): n = 0 self.players[0].setVehicle( loader.loadModel(self.vehicle_list[n])) taskMgr.add(self.devices.fetchEvents, "fetchEvents") track = trackgen3d.Track3d(1000, 1800, 1600, 1200, 5) #len(self._players)) streetPath = render.attachNewNode(track.createRoadMesh()) borderleftPath = render.attachNewNode( track.createBorderLeftMesh()) borderrightPath = render.attachNewNode( track.createBorderRightMesh()) borderleftcollisionPath = NodePath( track.createBorderLeftCollisionMesh()) borderrightcollisionPath = NodePath( track.createBorderRightCollisionMesh()) textures = ["tube", "tube2", "street"] tex = textures[random.randint(0, len(textures) - 1)] roadtex = loader.loadTexture('data/textures/' + tex + '.png') bordertex = loader.loadTexture('data/textures/border.png') streetPath.setTexture(roadtex) borderleftPath.setTexture(bordertex) borderrightPath.setTexture(bordertex) self.startGame(streetPath, borderleftPath, borderrightPath, track.trackpoints, borderleftcollisionPath, borderrightcollisionPath) if arg == "--PSt": PStatClient.connect( ) #activate to start performance measuring with pstats if arg == "--wire": base.toggleWireframe() if startgame: self.music.play() myMenu = Menu(self) taskMgr.add(self.devices.fetchEvents, "fetchEvents") myMenu.showStartScreen() base.accept("tab-up", self.takeScreenshot) # ----------------------------------------------------------------- def takeScreenshot(self): ''' ''' self.screenshot(source=base.win) # ----------------------------------------------------------------- def addPlayer(self, device): ''' creates a new player object, initializes it and sorts the cameras on the screen ''' self._notify.info("Adding Player, Device: %s" % (device)) screen = self.splitscreen.addCamera() camera = PlayerCam(screen) #Create a new player object self.players.append( player.Player(len(self.players), self.world, self.space, device, camera)) self._notify.info("Player added: %s" % (self.players[-1])) # ----------------------------------------------------------------- def removePlayer(self, player): ''' deletes a player object and sorts the cameras on the screem ''' #delete the cam self.splitscreen.removeCamera(player.camera.camera) #delete the player self.players.remove(player) ##all objects must be deleted! self._notify.info("Player removed: %s" % (player)) # ----------------------------------------------------------------- def createTrackpointTree(self, trackpoints): ''' Create a tree out of the trackpoints ''' self.track_tupel_list = [] #Change from Vec3 to tupel for point in self.trackpoints: self.track_tupel_list.append( (point.getX(), point.getY(), point.getZ())) self.list4tree = self.track_tupel_list[:] return KDTree.construct_from_data(self.list4tree) # ----------------------------------------------------------------- def startGame(self, track, borderl, borderr, trackpoints, borderlcoll, borderrcoll): ''' Start the game ''' #Create the KDTree for the position determination self.trackpoints = trackpoints #The mid points of the street for position calculation self.TrackpointTree = self.createTrackpointTree(self.trackpoints) self._notify = DirectNotify().newCategory("Game") self._notify.info("Initializing start game") #Initialize needed variables self.sparks = [] counter = 0 for player in self.players: player.activateGameCam() self.players[counter].vehicle.physics_model.setPosition( 0, -20 * counter, 10) self.players[counter].vehicle.model.setScale(2) self.players[counter].vehicle.model.setH(0) self.players[counter].vehicle.model.setP(0) self.players[counter].vehicle.model.setR(0) self.players[counter].vehicle.physics_model.setQuaternion( self.players[counter].vehicle.model.getQuat(render)) ## print "#####!!!!!####", self.players[counter].vehicle.getBoostStrength() self.players[counter].vehicle.setBoostStrength(1000) counter += 1 #Add the Skybox self.skybox = self.loader.loadModel("data/models/skybox.egg") t = Texture() boxes = ["space", "city", "ocean"] box = boxes[random.randint(0, len(boxes) - 1)] t.load(PNMImage("data/textures/skybox_" + box + ".png")) self.skybox.setTexture(t) self.skybox.setBin("background", 1) self.skybox.setDepthWrite(0) self.skybox.setDepthTest(0) self.skybox.setLightOff() self.skybox.setScale(10000) self.skybox.reparentTo(render) #Create the Track self.track = track self.track.reparentTo(render) self.borderl = borderl self.borderl.reparentTo(render) self.borderr = borderr self.borderr.reparentTo(render) self.borderlcoll = borderlcoll self.borderrcoll = borderrcoll ## self.borderlcoll.reparentTo(render) ## self.borderrcoll.reparentTo(render) ## roadtex = loader.loadTexture('data/textures/street.png') #### roadtex = loader.loadTexture('data/textures/tube.png') ## bordertex = loader.loadTexture('data/textures/border.png') ## self.track.setTexture(roadtex) ## self.borderl.setTexture(bordertex) ## self.borderr.setTexture(bordertex) self.rings = [] y = 100 for i in xrange(4): ring = loader.loadModel("data/models/ring.egg") ring.setScale(34) #ring.setZ(-15) ring.setY(y) y += 30 ring.setTransparency(TransparencyAttrib.MAlpha) ring.setLightOff() ring.reparentTo(render) self.rings.append(ring) taskMgr.add(self.turnRings, "turnRings") #add collision with the map self.groundGeom = OdeTriMeshGeom(self.space, OdeTriMeshData(self.track, True)) self.groundGeom.setCollideBits(0) self.groundGeom.setCategoryBits(1) self.borderl = OdeTriMeshGeom(self.space, OdeTriMeshData(self.borderlcoll, True)) self.borderl.setCollideBits(0) self.borderl.setCategoryBits(0) self.borderr = OdeTriMeshGeom(self.space, OdeTriMeshData(self.borderrcoll, True)) self.borderr.setCollideBits(0) self.borderr.setCategoryBits(0) #Create the Plane that you get hit by if you fall down self.plane = OdePlaneGeom(self.space, 0, 0, 1, -250) self.plane.setCollideBits(0) self.plane.setCategoryBits(4) #Load the Lights ambilight = AmbientLight('ambilight') ambilight.setColor(VBase4(0.2, 0.2, 0.2, 1)) render.setLight(render.attachNewNode(ambilight)) dlight = DirectionalLight('dlight') dlight.setColor(VBase4(10.0, 10.0, 10.0, 1)) if (base.win.getGsg().getSupportsBasicShaders() != 0): pass ##dlight.setShadowCaster(True, 2048, 2048) #enable shadows for this light ##TODO wegen Linux dlnp = render.attachNewNode(dlight) dlnp.setHpr(0, -60, 0) render.setLight(dlnp) self.space.setCollisionEvent("ode-collision") base.accept("ode-collision", self.onCollision) #start the gametask self._notify.debug("Starting gameTask") taskMgr.add(self.gameTask, "gameTask") self._notify.debug("Start Pos Calc") self.pos_vehicle = 0 taskMgr.add(self.calculatePos, "calculatePos") taskMgr.add(self.updatePos, "updatePos") self.world.setGravity(0, 0, -90.81) self._notify.info("Start game initialized") #set up the collision event self.starttime = time.time() # ----------------------------------------------------------------- def onCollision(self, entry): ''' Handles Collision-Events ''' geom1 = entry.getGeom1() geom2 = entry.getGeom2() body1 = entry.getBody1() body2 = entry.getBody2() for player in self.players: ## if geom1.compareTo(player.vehicle.getFrontRay().getRay()) or geom2.compareTo(player.vehicle.getFrontRay().getRay()): ## ###slipstream doesnt work but why? ## #if player.device.boost: ## player.vehicle.setBoost(player.vehicle.getBoostStrength()*0.2) #workaround until panda 1.7.1 #if the player collides with the ground plane he will get reset to the starting position if geom1.compareTo( self.plane ) == 0 and player.vehicle.physics_model.compareTo( body2) == 0 or geom2.compareTo( self.plane ) == 0 and player.vehicle.physics_model.compareTo( body1) == 0: player.vehicle.physics_model.setPosition(0, 0, 20) player.vehicle.physics_model.setLinearVel(0, 0, 0) player.vehicle.physics_model.setTorque(0, 0, 0) player.vehicle.physics_model.setRotation( Mat3.rotateMat(0, (Vec3(0, 0, 1)))) return #Decrease energy on collision elif player.vehicle.physics_model.compareTo( body1) == 0 or player.vehicle.physics_model.compareTo( body2) == 0: player.vehicle.energy -= player.vehicle.physics_model.getLinearVel( ).length() * 0.1 player.updateOSD() # ----------------------------------------------------------------- def onRayCollision(self, entry, player): ''' Handles Collision-Events with the street ''' geom1 = entry.getGeom1() geom2 = entry.getGeom2() body1 = entry.getBody1() body2 = entry.getBody2() ray = player.vehicle.ray.getRay() normal = Vec3(entry.getContactGeom(0).getNormal()) normal.normalize() player.vehicle.streetnormal = normal player.vehicle.physics_model.setGravityMode( 0) #disable gravity if on the track mass = player.vehicle.physics_model.getMass().getMagnitude() force_pos = ray.getPosition() contact = entry.getContactPoint(0) force_dir = force_pos - contact linear_velocity = player.vehicle.physics_model.getLinearVel() z_direction = player.vehicle.collision_model.getQuaternion().xform( Vec3(0, 0, 1)) actual_speed = Vec3(linear_velocity[0] * z_direction[0], linear_velocity[1] * z_direction[1], linear_velocity[2] * z_direction[2]) acceleration = ( (ray.getLength() / 2) - force_dir.length() ) * actual_speed.length() * 2.5 #calculate the direction player.vehicle.hit_ground = True player.vehicle.collision_model.setCollideBits(6) #force_dir.normalize() #Change the angle of the vehicle so it matches the street upvec = Vec3(player.vehicle.collision_model.getQuaternion().xform( Vec3(0, 0, 1))) player.vehicle.physics_model.addTorque( upvec.cross(normal) * mass * upvec.angleDeg(normal) - player.vehicle.physics_model.getAngularVel() * mass) if upvec.cross(normal).length() != 0: ## rotation = Mat3.rotateMat(upvec.angleDeg(normal),upvec.cross(normal)) ## protation=player.vehicle.physics_model.getRotation() ## protation*=rotation ## player.vehicle.collision_model.setRotation(protation) ## upvec = Vec3(player.vehicle.collision_model.getQuaternion().xform( Vec3(0, 0, 1))) player.vehicle.collision_model.setPosition(contact + (upvec * force_dir.length())) #checks if the vehicle is moving to or away from the road if (z_direction + actual_speed).length() < actual_speed.length(): goes_up = True else: goes_up = False needs_boost = 0 #calculates the needed boost based on the actual moving direction if goes_up: if actual_speed.length() < acceleration: needs_boost = acceleration - actual_speed.length() else: needs_boost = acceleration + actual_speed.length() else: if -actual_speed.length() < acceleration: needs_boost = acceleration + actual_speed.length() else: needs_boost = acceleration - actual_speed.length() #push the vehicle if needs_boost > 0: force_dir = Vec3(normal[0] * acceleration, normal[1] * acceleration, normal[2] * acceleration) player.vehicle.physics_model.addForce(force_dir * mass) #pull the vehicle elif needs_boost: force_dir = Vec3(normal[0] * acceleration, normal[1] * acceleration, normal[2] * acceleration) player.vehicle.physics_model.addForce(force_dir * mass) return # ----------------------------------------------------------------- def onFrontRayCollision(self, entry, player): ''' handles extreme changes in height collision with the street the vehicle needs to get lifted ''' normal = entry.getContactGeom(0).getNormal() mass = player.vehicle.physics_model.getMass().getMagnitude() speed = player.vehicle.speed #if speed > 5: speed = 5 upvec = Vec3(player.vehicle.collision_model.getQuaternion().xform( Vec3(0, 0, 1))) player.vehicle.physics_model.addTorque( upvec.cross(normal) * mass * 3 * upvec.angleDeg(Vec3(normal)) - player.vehicle.physics_model.getAngularVel() * mass) # ----------------------------------------------------------------- def onBorderCollision(self, entry, player): ''' handles collisions with the border ''' normal = entry.getContactGeom(0).getNormal() #player.vehicle.physics_model.addForce(player.vehicle.speed*player.vehicle.weight) #return needed_rotation = 90 - Vec3(normal).angleDeg(player.vehicle.direction) rotation = Mat3.rotateMat(needed_rotation, player.vehicle.direction) force = rotation.xform(normal) player.vehicle.physics_model.addTorque( player.vehicle.direction.cross(force) * 100 - player.vehicle.physics_model.getAngularVel()) player.vehicle.physics_model.addForce( force * player.vehicle.physics_model.getLinearVel().length() * player.vehicle.weight * 50) player.vehicle.physics_model.addForce( -(player.vehicle.physics_model.getLinearVel() * player.vehicle.weight * 50)) # ----------------------------------------------------------------- def calculatePos(self, task): ''' Appropriate the players position ''' task.delayTime = 0.1 ##TODO set value ca. 0.5 self.players[self.pos_vehicle].pre_position = self.players[ self.pos_vehicle].position self.pos_vehicle = (self.pos_vehicle + 1) % len(self.players) pos = self.TrackpointTree.query( query_point=(self.players[self.pos_vehicle].getVehicle().getPos()), t=1) self.players[self.pos_vehicle].position = self.track_tupel_list.index( pos[0]) #print (self.players[self.pos_vehicle].position - self.players[self.pos_vehicle].pre_position) #updateLaps if ((self.players[self.pos_vehicle].position - self.players[self.pos_vehicle].pre_position) <= -800): self.players[self.pos_vehicle].lap += 1 #Check if one Wins if (self.players[self.pos_vehicle].lap == self.laps + 1): #+1 because it starts at 1 print "Player", self.players[ self. pos_vehicle].number, "Time:", time.time() - self.starttime self.players[ self.pos_vehicle].time = time.time() - self.starttime self.winingPlayer += 1 if self.winingPlayer >= 3 or self.winingPlayer >= len( self.players): print "Game Finish" self._notify.debug(self.players[self.pos_vehicle].lap) if ((self.players[self.pos_vehicle].position - self.players[self.pos_vehicle].pre_position) >= 800): self.players[self.pos_vehicle].lap -= 1 #self._notify.debug( self.players[self.pos_vehicle].lap ) #self._notify.debug( ("Player", self.pos_vehicle,":", self.track_tupel_list.index(pos[0]))) #self._notify.debug( self.players[self.pos_vehicle].getVehicle().getPos()) return task.again # ----------------------------------------------------------------- def updatePos(self, task): ''' Set the rank for each player ''' task.delayTime = 0.1 ##TODO set value ca. 0.5 positionen = [] for player in self.players: positionen.append(player.position) positionen.sort() for player in self.players: player.rank = positionen.index(player.position) #self._notify.debug( ("PlayerRank", player.rank )) return task.again # ----------------------------------------------------------------- def gameTask(self, task): ''' this task runs once per frame if the game is running And calculate the physics ''' #self.space.autoCollide() # Setup the contact joints self.deltaTimeAccumulator += globalClock.getDt() while self.deltaTimeAccumulator > self.stepSize: # Step the simulation for player in self.players: player.doStep() #refresh player specific things (rays) #get the player input and set the forces if player.device.boost: player.vehicle.setBoost() else: player.vehicle.stopBlowout() if player.device.directions[ 0] != 0 or player.device.directions[1] != 0: player.vehicle.direction = player.device.directions linear_velocity = player.vehicle.physics_model.getLinearVel() angular_velocity = player.vehicle.physics_model.getAngularVel() mass = player.vehicle.physics_model.getMass().getMagnitude() #calculate airresistance to get energy out of the ode-system player.vehicle.physics_model.addForce( linear_velocity * -self.LINEAR_FRICTION * mass) player.vehicle.physics_model.addTorque( angular_velocity * -self.ANGULAR_FRICTION * mass) #calculate the ray col = OdeUtil.collide(player.vehicle.ray.getRay(), self.groundGeom) if not col.isEmpty(): self.onRayCollision( col, player ) #handles collisions from the ray with the street col = OdeUtil.collide(player.vehicle.frontray.getRay(), self.groundGeom) if not col.isEmpty(): self.onFrontRayCollision(col, player) #Collision with the border col = OdeUtil.collide(player.vehicle.collision_model, self.borderr) if not col.isEmpty(): self.onBorderCollision(col, player) else: col = OdeUtil.collide(player.vehicle.collision_model, self.borderl) if not col.isEmpty(): self.onBorderCollision(col, player) self.deltaTimeAccumulator -= self.stepSize # Remove a stepSize from the accumulator until the accumulated time is less than the stepsize self.space.autoCollide() # Setup the contact joints self.world.quickStep(self.stepSize) self.contactgroup.empty() # Clear the contact joints for player in self.players: # set new rank player.updatePlayer() return task.cont # ----------------------------------------------------------------- def turnRings(self, task): ''' ''' speeds = [.2, -.5, .7, -.3] for i in xrange(len(self.rings)): self.rings[i].setR(self.rings[i].getR() + speeds[i]) return task.cont
if run_client: # We must import this before panda due to talmoc issues in linux from cefpython3 import cefpython from direct.directnotify.DirectNotify import DirectNotify from panda3d.core import loadPrcFileData, Shader import spacedrive from networking import server_net, client_net loadPrcFileData("", "notify-level-ITF debug") log = DirectNotify().newCategory("ITF") # After initial setup we can now start sandbox log.debug("Loading space drive") spacedrive.init(run_server=run_server, run_client=(run_client or run_menu), log_level='debug', window_title='Into The Fire') import solarSystem def main_menu(): """Main menu management.""" import sandbox from panda3d.core import Vec3 from spacedrive.renderpipeline import DirectionalLight, PointLight import math import gui_manager
class SplitScreen(object): ''' This class manages multiple display regions and handles adding and removing them from screen. ''' def __init__(self, cam_count=0): ''' @param cam_count: (int) number of cameras which should be added ''' self._notify = DirectNotify().newCategory("SplitScreen") self._notify.info("New SplitScreen-Object created: %s" % (self)) self.regions = [] # the regions the screen is separated into self.cameras = [] # the cameras (empty ones are None) self.filters = [] # the shader filters applied to each camera self.cameraPosPre = [] # the position of the cameras before change self.steps = 1 self.shaders_on = False if cam_count > 0: # add cameras, if needed self.addCameras(cam_count) # ----------------------------------------------------------------- def addCamera(self): ''' adds a camera for a new player (or an additional view) @return: returns the added camera object ''' self._notify.debug("Creating new camera") unused = self.getUnusedRegion() # if there is an unused camera slot, use it if unused != -1: self.setShaderOff() self.cameras[unused] = self.createCamera(self.regions[unused]) self.setShaderOn() # if there is no free slot, we have to recalculate the regions else: self.regions = self.calculateRegions(len(self.regions) + 1) # recalc self.cameras.append(self.createCamera( self.regions[unused])) # add a cam # if there are empty slots, they're filled with None for i in xrange(len(self.regions) - len(self.cameras)): self.cameras.append(None) unused -= 1 self.refreshCameras() # rearrange the regions # if there was an unused slot, the camera is now in it # if not, unused is -1 which points to the last element of the list # so unused points always to the newest cam self._notify.debug("New regions: %s , New cameras: %s" % (self.regions, self.cameras)) self._notify.debug("New camera created") return self.cameras[unused] # ----------------------------------------------------------------- def removeCamera(self, camera): ''' removes a camera out of the list @param camera: (Camera NodePath) camera to remove ''' self._notify.debug("Removing camera: %s" % (camera)) # get the index of the camera i = self.cameras.index(camera) self.filters[i].delBloom() # deactivate the shader self.filters.pop(i) # delete the filter self.cameras.pop(i) # delete the camera self.regions.pop(i) # delete the region # delete all empty cameras try: while True: self.cameras.remove(None) except: pass # delete all empty filters try: while True: self.filters.remove(None) except: pass # the regions have to be recalculated and the cameras have to be rearranged self.regions = self.calculateRegions(len(self.cameras)) self.refreshCameras() self._notify.debug("New regions: %s , New cameras: %s" % (self.regions, self.cameras)) # ----------------------------------------------------------------- def addCameras(self, cam_count): ''' adds multiple cameras @param cam_count: (int) number of cams to add @return: (list) returns all recently added cameras ''' cams = [] # this will be returned unused = self.getUnusedRegions() # this stores all unused slots # if there are enough unused camera slots, use it if len(unused) >= cam_count: for i in unused: self.cameras[i] = self.createCamera( self.regions[i]) # add the cam self.cams.append(self.cameras[i]) # if there are not enough free slots, we have to recalculate the regions else: self.regions = self.calculateRegions( len(self.cameras) - len(unused) + cam_count) # first fill the unused slots for i in unused: self.cameras[i] = self.createCamera( self.regions[i]) # add the cam self.cams.append(self.cameras[i]) # then add all other new cams at the end for i in xrange(cam_count - len(unused)): cam = self.createCamera(self.regions[i]) self.cameras.append(cam) # add the cam cams.append(cam) # if there are empty slots, they're filled with None for i in xrange(len(self.regions) - len(self.cameras)): self.cameras.append(None) # refresh all cameras self.refreshCameras() return cams # return all added cams # ----------------------------------------------------------------- def refreshCameras(self): ''' updates the size of all cameras, when the count of the regions has changed ''' taskMgr.remove("AnimateRegion") self.cameraPosPre = [] # those are the old positions # first deactivate all shaders self.setShaderOff() # then save all old regions for i in xrange(len(self.cameras)): if self.cameras[i] != None: # add the old regions to the list region = self.cameras[i].node().getDisplayRegion(0) self.cameraPosPre.append((region.getLeft(), region.getRight(), region.getBottom(), region.getTop())) #update the aspect of the camera self.cameras[i].node().getLens().setAspectRatio( ((self.regions[i][1] - self.regions[i][0]) / (self.regions[i][3] - self.regions[i][2]))) height = self.cameras[i].node().getDisplayRegion( 0).getPixelHeight() width = self.cameras[i].node().getDisplayRegion( 0).getPixelWidth() ratio = float(width) / float(height) self.cameras[i].node().getLens().setFov(45 * ratio) # start the animation in a new task taskMgr.add(self.animateRegion, "AnimateRegion") ## #Old Code without animation ## for i in xrange(len(self.cameras)): ## if self.cameras[i] != None: ## print self.cameras[i].node().getDisplayRegion(0), self.cameras[i].node().getDisplayRegions ## self.cameras[i].node().getDisplayRegion(0).setDimensions(self.regions[i][0], self.regions[i][1], self.regions[i][2], self.regions[i][3]) ## self._notify.debug("Aspect Ratio: %s:%s" %(self.regions[i][1]-self.regions[i][0],self.regions[i][3]-self.regions[i][2])) ## ## #self.cameras[i].node().getLens().setFov(45*((self.regions[i][1]-self.regions[i][0])/(self.regions[i][3]-self.regions[i][2]))) ## self.cameras[i].node().getLens().setAspectRatio(((self.regions[i][1]-self.regions[i][0])/(self.regions[i][3]-self.regions[i][2]))) ## ## # reactivate the shaders ## self.setShaderOn() #----------------------------------------------------------------- ##TODO Use task.time instead of self.steps (Frames) def animateRegion(self, task): ''' animates the subwindows to their new positions and reactivates the shaders after that ''' # if the animation is finished, we set the positions to final values if task.time >= self.steps: for i in xrange(len(self.cameraPosPre)): self.cameras[i].node().getDisplayRegion(0).setDimensions( self.calTheDiff(self.cameraPosPre[i][0], self.regions[i][0], self.steps), self.calTheDiff(self.cameraPosPre[i][1], self.regions[i][1], self.steps), self.calTheDiff(self.cameraPosPre[i][2], self.regions[i][2], self.steps), self.calTheDiff(self.cameraPosPre[i][3], self.regions[i][3], self.steps)) self.cameras[i].node().getLens().setAspectRatio( ((self.regions[i][1] - self.regions[i][0]) / (self.regions[i][3] - self.regions[i][2]))) height = self.cameras[i].node().getDisplayRegion( 0).getPixelHeight() width = self.cameras[i].node().getDisplayRegion( 0).getPixelWidth() ratio = float(width) / float(height) self.cameras[i].node().getLens().setFov(45 * ratio) # reactivate the shaders self.setShaderOn() # end the task return task.done # animate the screens for i in xrange(len(self.cameraPosPre)): self.cameras[i].node().getDisplayRegion(0).setDimensions( self.calTheDiff(self.cameraPosPre[i][0], self.regions[i][0], task.time), self.calTheDiff(self.cameraPosPre[i][1], self.regions[i][1], task.time), self.calTheDiff(self.cameraPosPre[i][2], self.regions[i][2], task.time), self.calTheDiff(self.cameraPosPre[i][3], self.regions[i][3], task.time)) self.cameras[i].node().getLens().setAspectRatio( ((self.regions[i][1] - self.regions[i][0]) / (self.regions[i][3] - self.regions[i][2]))) height = self.cameras[i].node().getDisplayRegion( 0).getPixelHeight() width = self.cameras[i].node().getDisplayRegion(0).getPixelWidth() ratio = float(width) / float(height) self.cameras[i].node().getLens().setFov(45 * ratio) # continue the task return task.cont def calTheDiff(self, alt, neu, tasktime): return alt + ((neu - alt) / self.steps) * tasktime #----------------------------------------------------------------- def getUnusedRegion(self): ''' looks for an unused region to add a new player into it @return: (int) the index of an empty camera region ''' for i in xrange(len(self.cameras)): if self.cameras[i] == None: return i else: return -1 # ----------------------------------------------------------------- def getUnusedRegions(self): ''' looks for all unused regions to add a new player into it @return: (list) the indices of all empty camera regions ''' unused = [] for i in xrange(len(self.cameras)): if self.cameras[i] == None: unused.append(i) return unused # ----------------------------------------------------------------- def createCamera(self, region): ''' Create a camera for a new player ''' camera = base.makeCamera(base.win, displayRegion=region) camera.node().getLens().setAspectRatio( ((region[1] - region[0]) / (region[3] - region[2]))) height = camera.node().getDisplayRegion(0).getPixelHeight() width = camera.node().getDisplayRegion(0).getPixelWidth() ratio = float(width) / float(height) camera.node().getLens().setFov(45 * ratio) camera.setPos(0, -8, 3) #set its position. return camera # ----------------------------------------------------------------- def calculateRegions(self, count): ''' Calculates the window size and position for a count of n screens @return: (tuple) the display region in panda format (x1,x2,y1,y2) x is left-right, y is bottom-up ''' regions = [ ] #this list stores the display regions for the several screens separations = ceil( sqrt(count)) # how often has the screen to be separated? # this is executed if a squarical order isn't senseful if ((separations**2) - separations >= count): for y in range(int(separations - 1), 0, -1): for x in range(1, int(separations + 1), 1): regions.append( ((x - 1) / separations, x / separations, (y - 1) / (separations - 1), y / (separations - 1))) # this is executed if the player count is near a square number e.g. 9 or 16 else: for y in range(int(separations), 0, -1): for x in range(1, int(separations + 1), 1): regions.append(((x - 1) / separations, x / separations, (y - 1) / separations, y / separations)) return regions # ----------------------------------------------------------------- def setShaderOn(self): ''' activates all shaders ''' # activate the shaders self.filters = [] for i in xrange(len(self.cameras)): if self.cameras[i] != None: self.filters.append(CommonFilters(base.win, self.cameras[i])) self.filters[i].setBloom(blend=(0, 0, 0, 1), desat=-0.8, intensity=4.0, size="big") else: self.filters.append(None) self.shaders_on = True # ----------------------------------------------------------------- def setShaderOff(self): ''' deactivates all shaders ''' # deactivate the shaders for filters in self.filters: if filters != None: filters.delBloom() del filters self.filters = [] self.shaders_on = False
class Vehicle(object): ''' ''' def __init__(self, ode_world, ode_space, name="standard"): ''' ''' self._notify = DirectNotify().newCategory("Vehicle") self._notify.info("New Vehicle-Object created: %s" % (self)) self._ode_world = ode_world self._ode_space = ode_space self._model = None self._physics_model = None self._physics_mass = None self._collision_model = None self._speed = 0.0 # the actual speed of the vehicle (forward direction) self._direction = Vec3(0, 0, 0) # the direction the car is heading self._boost_direction = Vec3(0, 0, 0) self._boost_strength = 10.0 # the boost propertys of the vehicle self._control_strength = 1.5 # impact on the steering behaviour self._grip_strength = 0.5 # impact on the steering behaviour self._track_grip = 0.8 # impact on the steering behaviour self._energy = 100.0 self._armor = 100.0 self._max_energy = 100.0 self._max_armor = 100.0 self._weight = 10.0 self._description = "The best vehicle ever" self._name = "The flying egg" self._brake_strength = 10.0 self._hit_ground = True self._model_loading = False self._blowout = [] self._blowout_on = False self._streetnormal = Vec3(0, 0, 1) # set up the propertys of the vehicle that schould be loaded # the methods get called because the data is immutable and # wouldnt get updated when calling the objects directly # the last entry is the function to convert the string self._tags = [["name", self.setName, str], ["description", self.setDescription, str], ["control_strength", self.setControlStrength, float], ["grip_strength", self.setGripStrength, float], ["track_grip", self.setTrackGrip, float], ["max_energy", self.setMaxEnergy, float], ["max_armor", self.setMaxArmor, float], ["weight", self.setWeight, float], ["brake_strength", self.setBrakeStrength, float], ["boost_strength", self.setBoostStrength, float]] # --------------------------------------------------------- def setVehicle(self, model): ''' Choose what vehicle the player has chosen. This method initializes all data of this vehicle ''' self.cleanResources() self._notify.debug("Set new vehicle: %s" % model) # Load the attributes of the vehicle attributes = model.find("**/Attributes") if attributes.isEmpty(): self._notify.warning("No Attribute-Node found") for tag in self._tags: value = attributes.getNetTag(tag[0]) if value: self._notify.debug("%s: %s" % (tag[0], value)) # translate the value if its a string if type(tag[2](value)) == str: tag[1](_(tag[2](value))) else: tag[1](tag[2](value)) else: self._notify.warning("No value defined for tag: %s" % (tag[0])) self._weight = 10 # for testing purposes blowout = model.find("**/Blowout") if not blowout.isEmpty(): self._notify.debug("Loading Blowout-Particles") for node in blowout.getChildren(): particle = ParticleEffect() self._blowout.append(particle) particle.loadConfig('./data/particles/blowout_test.ptf') try: # try to read out the particle scale scale = float(node.getTag("scale")) except Exception: # default is 0.5 scale = .5 renderer = particle.getParticlesList()[0].getRenderer() renderer.setInitialXScale(scale) renderer.setInitialYScale(scale) particle.setLightOff() particle.start(node) particle.softStop() else: self._notify.warning("No Blowout-Node found") if self._model is not None: heading = self._model.getH() self._model.removeNode() else: heading = 160 # display the attributes text = model.getParent().find("AttributeNode") if not text.isEmpty(): node = text.find("name") if not node.isEmpty(): node = node.node() node.setText(self._name) node.update() text.show() node = text.find("description") if not node.isEmpty(): node = node.node() node.setText(self._name) node.update() text.show() self._model = model self._model.setPos(0, 0, 2) self._model.setHpr(heading, 0, 0) # #Test # plightCenter = NodePath( 'plightCenter' ) # plightCenter.reparentTo( render ) # self.interval = plightCenter.hprInterval(12, Vec3(360, 0, 0)) # self.interval.loop() # # plight = PointLight('plight') # plight.setColor(VBase4(0.8, 0.8, 0.8, 1)) # plight.setAttenuation(Vec3(1,0,0)) # plnp = plightCenter.attachNewNode(plight) # plnp.setPos(5, 5, 10) # render.setLight(plnp) # # alight = AmbientLight('alight') # alight.setColor(VBase4(0,0,0, 1)) # alnp = render.attachNewNode(alight) # render.setLight(alnp) # GlowTextur # self.glowSize=10 # self.filters = CommonFilters(base.win, self._model) # self.filters.setBloom(blend=(0,self.glowSize,0,0) ,desat=1, intensity=1, size='medium') # tex = loader.loadTexture( 'data/textures/glowmap.png' ) # ts = TextureStage('ts') # ts.setMode(TextureStage.MGlow) # self._model.setTexture(ts, tex) # Initialize the physics-simulation for the vehicle self._physics_model = OdeBody(self._ode_world) self._physics_model.setPosition(self._model.getPos(render)) self._physics_model.setQuaternion(self._model.getQuat(render)) # Initialize the mass of the vehicle physics_mass = OdeMass() physics_mass.setBoxTotal(self._weight, 1, 1, 1) self._physics_model.setMass(physics_mass) # Initialize the collision-model of the vehicle # for use with blender models # try: # col_model = loader.loadModel("data/models/vehicles/%s.collision" %(self._model.getName())) # self.collision_model = OdeTriMeshGeom(self._ode_space, OdeTriMeshData(col_model, True)) # self._notify.info("Loading collision-file: %s" %("data/models/vehicles/%s.collision" %(self._model.getName()))) # for fast collisions # except: # self._notify.warning("Could not load collision-file. Using standard collision-box") # self.collision_model = OdeTriMeshGeom(self._ode_space, OdeTriMeshData(model, False)) # self._collision_model = OdeBoxGeom(self._ode_space, 3,3,2) self._collision_model = OdeBoxGeom(self._ode_space, 3, 3, 2) self._collision_model.setBody(self._physics_model) self._collision_model.setCollideBits(7) self._collision_model.setCategoryBits(2) # Add collision-rays for the floating effect self._ray = CollisionRay(Vec3(5, 0, 0), Vec3(0, 0, -1), self._ode_space, parent=self._collision_model, collide_bits=0, length=20.0) # This one is used for the floating effect but also for slipstream self._frontray = CollisionRay(Vec3(0, 0, 0), Vec3(1, 0, 0), self._ode_space, parent=self._collision_model, collide_bits=0, length=15.0) # Overwrite variables for testing purposes self._grip_strength = 0.9 self._track_grip = 0.2 self._boost_strength = 1400 self._control_strength = 2.5 # Loading finished self._model_loading = False def toggleGlow(self): self.glowSize += .1 print((self.glowSize)) if (self.glowSize == 4): self.glowSize = 0 self.filters.setBloom(blend=(0, self.glowSize, 0, 0), desat=-2, intensity=3, size='medium') def boggleGlow(self): self.glowSize -= .1 print((self.glowSize)) if (self.glowSize == 4): self.glowSize = 0 self.filters.setBloom(blend=(0, self.glowSize, 0, 0), desat=-2, intensity=3.0, size='medium') # --------------------------------------------------------- def setPos(self, x, y, z): ''' ''' self._model.setPos(x, y, z) def getPos(self): ''' ''' return self._model.getPos() position = property(fget=getPos, fset=setPos) # --------------------------------------------------------- def startBlowout(self): ''' ''' if not self._blowout_on: self._blowout_on = True for particle in self._blowout: particle.softStart() def stopBlowout(self): ''' ''' if self._blowout_on: self._blowout_on = False for particle in self._blowout: particle.softStop() # --------------------------------------------------------- def setModel(self, model): ''' ''' self._model = model def getModel(self): ''' ''' return self._model model = property(fget=getModel, fset=setModel) # --------------------------------------------------------- def setCollisionModel(self, model): ''' ''' self._collision_model = model def getCollisionModel(self): ''' ''' return self._collision_model collision_model = property(fget=getCollisionModel, fset=setCollisionModel) # --------------------------------------------------------- def setPhysicsModel(self, model): ''' ''' self._physics_model = model def getPhysicsModel(self): ''' ''' return self._physics_model physics_model = property(fget=getPhysicsModel, fset=setPhysicsModel) # --------------------------------------------------------- def setBoost(self, strength=1): ''' Boosts the vehicle by indicated strength ''' self.startBlowout() if self._hit_ground: direction = self._streetnormal.cross( self._collision_model.getQuaternion().xform(Vec3(1, 0, 0))) self._physics_model.addForce( direction * ((self._boost_strength * self.physics_model.getMass().getMagnitude() * strength))) self._hit_ground = False self._collision_model.setCollideBits(7) else: direction = self._streetnormal.cross( self._collision_model.getQuaternion().xform(Vec3(1, 0, 0))) self._physics_model.addForce( direction * self._boost_strength * 0.5 * self.physics_model.getMass().getMagnitude() * strength) # --------------------------------------------------------- def setDirection(self, dir): ''' Steers the vehicle into the target-direction ''' rel_direction = self._collision_model.getQuaternion().xform( Vec3(dir[1], 0, dir[0])) # rel_position = self._collision_model.getQuaternion().xform(Vec3(5,0,0)) # force = Vec3(rel_direction[0]*self.direction[0]*self._control_strength*self.speed,rel_direction[1]*self.direction[1]*self._control_strength*self.speed,rel_direction[2]*self.direction[2]*self._control_strength*self.speed) self._physics_model.addTorque( -rel_direction * self._control_strength * self.physics_model.getMass().getMagnitude()) def getDirection(self): return self._direction direction = property(fget=getDirection, fset=setDirection) # --------------------------------------------------------- def getBoostDirection(self): return self._boost_direction boost_direction = property(fget=getBoostDirection) # --------------------------------------------------------- def getSpeed(self): return self._speed speed = property(fget=getSpeed) # --------------------------------------------------------- def setEnergy(self, energy): ''' Boosts the vehicle by indicated strength ''' self._energy = energy def getEnergy(self): return self._energy energy = property(fget=getEnergy, fset=setEnergy) # --------------------------------------------------------- def setModelLoading(self, bool): ''' ''' self._model_loading = bool def getModelLoading(self): return self._model_loading model_loading = property(fget=getModelLoading, fset=setModelLoading) # --------------------------------------------------------- def doStep(self): ''' Needs to get executed every Ode-Step ''' # refresh variables linear_velocity = self._physics_model.getLinearVel() direction = self._streetnormal.cross( self._collision_model.getQuaternion().xform(Vec3(1, 0, 0))) self._boost_direction[0], self._boost_direction[ 1], self._boost_direction[2] = self.physics_model.getLinearVel( )[0], self.physics_model.getLinearVel( )[1], self.physics_model.getLinearVel()[2] # This needs to be done, so we dont create a new object but only change the existing one. else the camera wont update self._direction[0], self._direction[1], self._direction[2] = direction[ 0], direction[1], direction[2] xy_direction = self.collision_model.getQuaternion().xform(Vec3( 1, 1, 0)) self._speed = Vec3(linear_velocity[0] * xy_direction[0], linear_velocity[1] * xy_direction[1], linear_velocity[2] * xy_direction[2]).length() # calculate air resistance self._physics_model.addForce( -linear_velocity * linear_velocity.length() / 10) # *((self._speed/2)*(self._speed/2)))#+linear_velocity) # calculate delayed velocity changes linear_velocity.normalize() self._direction.normalize() self._physics_model.addForce( self._direction * (self._speed * self._grip_strength * self.physics_model.getMass().getMagnitude())) # +linear_velocity) self._physics_model.addForce( -linear_velocity * (self._speed * self._grip_strength * self.physics_model.getMass().getMagnitude())) # +linear_velocity) # calculate the grip self._physics_model.addTorque( self._physics_model.getAngularVel() * -self._track_grip * self.physics_model.getMass().getMagnitude()) # refresh the positions of the collisionrays self._ray.doStep() self._frontray.doStep() self._physics_model.setGravityMode(1) # --------------------------------------------------------- def getRay(self): return self._ray ray = property(fget=getRay) # --------------------------------------------------------- def getFrontRay(self): return self._frontray frontray = property(fget=getFrontRay) # ----------------------------------------------------------------- def getHitGround(self): return self._hit_ground def setHitGround(self, value): if type(value) != bool: raise TypeError("Type should be %s not %s" % (bool, type(value))) self._hit_ground = value hit_ground = property(fget=getHitGround, fset=setHitGround) # ----------------------------------------------------------------- def getControlStrength(self): return self._control_strength def setControlStrength(self, value): self._control_strength = value control_strength = property(fget=getControlStrength, fset=setControlStrength) # ----------------------------------------------------------------- def getBoostStrength(self): return self._boost_strength def setBoostStrength(self, value): self._boost_strength = value boost_strength = property(fget=getBoostStrength, fset=setBoostStrength) # ----------------------------------------------------------------- def getGripStrength(self): return self._grip_strength def setGripStrength(self, value): self._grip_strength = value grip_strength = property(fget=getGripStrength, fset=setGripStrength) # ----------------------------------------------------------------- def getTrackGrip(self): return self._track_grip def setTrackGrip(self, value): self._track_grip = value track_grip = property(fget=getTrackGrip, fset=setTrackGrip) # ----------------------------------------------------------------- def getMaxEnergy(self): return self._max_energy def setMaxEnergy(self, value): self._max_energy = value max_energy = property(fget=getMaxEnergy, fset=setMaxEnergy) # ----------------------------------------------------------------- def getMaxArmor(self): return self._max_armor def setMaxArmor(self, value): self._max_armor = value max_armor = property(fget=getMaxArmor, fset=setMaxArmor) # ----------------------------------------------------------------- def getWeight(self): return self._weight def setWeight(self, value): self._weight = value weight = property(fget=getWeight, fset=setWeight) # ----------------------------------------------------------------- def getDescription(self): return self._description def setDescription(self, value): self._description = value description = property(fget=getDescription, fset=setDescription) # ----------------------------------------------------------------- def getBrakeStrength(self): return self._brake_strength def setBrakeStrength(self, value): self._brake_strength = value brake_strength = property(fget=getBrakeStrength, fset=setBrakeStrength) # ----------------------------------------------------------------- def getName(self): return self._name def setName(self, value): self._name = value name = property(fget=getName, fset=setName) # ----------------------------------------------------------------- def getStreetNormal(self): return self._streetnormal def setStreetNormal(self, value): self._streetnormal = value streetnormal = property(fget=getStreetNormal, fset=setStreetNormal) # ----------------------------------------------------------------- def cleanResources(self): ''' Removes old nodes, gets called when a new vehcile loads ''' for node in self._blowout: node.removeNode() self._blowout = [] if self._model is not None: for node in self._model.getChildren(): node.removeNode() self._model.removeNode() self._model = None # self._physics_model.destroy() # self._collision_model.destroy() # temporary fix because destroy() doesnt work self._physics_model.disable() self._collision_model.disable() self._notify.info("Vehicle-Object cleaned: %s" % (self)) def __del__(self): ''' Destroy unused nodes ''' for node in self._blowout: node.removeNode() if self._model is not None: for node in self._model.getChildren(): node.removeNode() self._model.removeNode() self._model = None self._physics_model.destroy() self._collision_model.destroy() # temporary fix because destroy() doesnt work self._physics_model.disable() self._collision_model.disable() self._notify.info("Vehicle-Object deleted: %s" % (self))
class PlaneView(FSM, DirectObject): """Give this class a plane as argument and it will create some nodes around it which you can parent the camera to (if there are no such nodes yet). Usage: plane_camera = PlaneCamera(aeroplane, camera) plane_camera.setView("ThirdPerson") plane_camera.setView("Next") """ def __init__(self, camera, parent): """Arguments: camera -- Camera to be used parent -- Aeroplane which the camera should follow """ self.notifier = DirectNotify().newCategory("azure-camera") self.camera = camera self.parent = parent # This gets replaced by a NodePath with all available cameras as # children and plane node as parent in createCamNodes() self.cameras = None #if parent.__class__.__name__ is not "Aeroplane": if not isinstance(self.parent, Aeroplane): raise ParamError, "Parent must be an Aeroplane instance, " + \ "but is %s" % type(self.parent) FSM.__init__(self, "PlaneCamera: %s" % self.parent.name) DirectObject.__init__(self) self.cameras = self.parent.node.find("cameras") if self.cameras.isEmpty(): self.createCamNodes() self.updateCamArray() self.sideview_direction = 0 # Set up the default camera self.setView("ThirdPerson") def createCamNodes(self): """Creates a few empty nodes around a plane which the camera might be parented to. It looks if there are cameras inside the model file and uses those if possible. Where everything named "camera CamType" is considered a camera. At least ThirdPerson, FirstPerson and Cockpit should be defined inside the egg file, otherwise some guessed defaults are taken. """ # Look for cameras inside the model (loaded egg file) self.cameras = NodePath("cameras") found_cams = self.parent.node.findAllMatches("**/camera ?*") found_cams.removeDuplicatePaths() found_cams.reparentTo(self.cameras) if not found_cams.isEmpty(): self.notifier.info("Cameras found under model:\n%s" % found_cams) else: self.notifier.info("No cameras found under model.") # FirstPerson camera is a must-have. Set up a guessed one if none # defined yet. if self.cameras.find("camera FirstPerson").isEmpty(): assert self.notifier.debug("No first person camera found in %s. " "Guessing best position." % self.parent.name) first_person = NodePath("camera FirstPerson") # TODO: Guess best position based on bounding box. first_person.setY(5) first_person.reparentTo(cameras) # ThirdPerson camera is a must-have. Set up a guessed one if none # defined yet. if self.cameras.find("camera ThirdPerson").isEmpty(): assert self.notifier.debug("No third person camera found in %s. " "Guessing best position." % self.parent.name) third_person = NodePath("camera ThirdPerson") # TODO: Guess best position based on bounding box. third_person.setPos(0, -30, 5) #third_person.setP(-80) third_person.reparentTo(cameras) # Cockpit needs to be accurate. Don't try to guess it. if self.cameras.find("camera Cockpit").isEmpty(): assert self.notifier.debug("No cockpit camera found in " "%s. Cockpit camera disabled." % self.parent.name) self.sideview_cam = NodePath("camera Sideview") self.sideview_cam.reparentTo(render) # Store the cams at parent node.. # You can edit the camera nodes from outside as well. # If you attach new camera nodes, though, you'll have to call this # function again. self.cameras.reparentTo(self.parent.node) def updateCamArray(self, cameramodes=None): """Set the cameras which next and previous will switch to. Expects a list or tuple. Defaults to all available cameras.""" a = [] if not cameramodes: for c in self.cameras.getChildren(): if c.getName().startswith("camera "): a.append(c.getName().strip("camera ")) self.setStateArray(a) else: self.setStateArray(cameramodes) def getView(self): """Returns the current view mode.""" return self.getCurrentOrNextState() def setView(self, mode, *args): """Convenience function.""" return self.request(mode, args) def defaultEnter(self, *args): """Executed by the FSM every time an undefined state is entered. Note: this function is called AFTER the responsible filter.""" assert self.notifier.debug("Changing state from %s to %s with args: %s." % (self.oldState, self.newState, args)) request = self.newState target_cam = self.cameras.find("camera " + request) if not target_cam.isEmpty(): try: self.camera.reparentTo(target_cam) self.camera.setPosHpr(0, 0, 0, 0, 0, 0) except: self.notifier.warning( "Ok, now this really shouldn't happen! Filter said the " "camera is there and enter can't find it...") def defaultFilter(self, request, args): """Executed by the FSM every time an undefined state is requested.""" assert self.notifier.debug("Requested %s with args: %s" % (request, args)) self.camera.setPosHpr(0, 0, 0, 0, 0, 0) # Always available. if request == "Off": # implemented in FSM.py return (request,) + args if request == "Next": # implemented in FSM.py return self.requestNext(args) if request == "Prev": # implemented in FSM.py return self.requestPrev(args) if request == "Detached": return (request,) + args if request == "Sideview": return (request,) + args # Depending on airplane. if not self.cameras.find("camera " + request).isEmpty(): return (request,) + args assert self.notifier.info("Sorry, no %s camera found." % request) return None def enterOff(self, *args): """Clean up everything by reparenting the camera to the airplane.""" self.camera.reparentTo(self.parent.node) self.camera.setPosHpr(0, 0, 0, 0, 0, 0) def enterSideview(self, *args): self.sideview_direction += 90 self.camera.reparentTo(self.sideview_cam) self.camera.setY(-30) self.sideview_cam.setH(self.sideview_direction) #self.addTask(self.updateSideview, "sideview camera", taskChain="world") self.task = self.updateSideview def exitSideview(self, *args): #self.removeTask("sideview camera") self.task = lambda x: Task.cont def updateSideview(self, task): self.sideview_cam.setPos(self.parent.node.getPos()) return Task.cont def enterDetached(self, *args): """Lets the camera view the plane from far away.""" self.camera.reparentTo(render) self.camera.setPosHpr(0, 0, 10, 0, 0, 0) #self.addTask(self.updateDetachedCam, "detached camera", # taskChain="world") self.task = self.updateDetachedCam def exitDetached(self, *args): #self.removeTask("detached camera") self.task = lambda x: Task.cont def updateDetachedCam(self, task): """Updates camera position and rotation for Detached camera.""" try: self.camera.lookAt(self.parent.node) except: self.notifier.warning("Error on detached cam task. Exit.") return Task.done return Task.cont def enterThirdPerson(self, *args): """Lets the camera view the plane from far away.""" self._hist = [] self.camera.reparentTo(self.cameras.find("camera ThirdPerson")) self.cameras.find("camera ThirdPerson").setPos(0, -30, 0) #self.addTask(self.updateThirdPersonCam, "third person camera", # taskChain="world") self.task = self.updateThirdPersonCam #print "entering third person" def exitThirdPerson(self, *args): #self.removeTask("third person camera") self.task = lambda x: Task.cont del self._hist def updateThirdPersonCam(self, task): """Updates camera position and rotation for ThirdPerson camera.""" speed = self.parent.physics.speed() velocity = self.parent.physics.velocity() #v = Point3(self.parent.physics.angVelVector()) v = Point3(self.parent.physics.angVelBodyHpr()) print round(v.getX(), 2), round(v.getY(), 2), round(v.getZ(), 2) #self.segs = LineSegs("lines"); #self.segs.setColor(1,1,1,1) #self.segs.drawTo(-1, 0) #self.segsnode = self.segs.create() #render2d.attachNewNode(self.segsnode) vec = Point3(self.parent.physics.angVelBodyHpr()) # Y hiervon ist pitch, Z ist roll #print round(vec.getY(), 2), round(vec.getZ(), 2) self.camera.lookAt(self.parent.node) self.camera.setR(self.camera, vec.getZ()*3) #self.camera.setPos(abs(v.getX()), 0, vec.getY()) #plane = self.parent.node #self._hist.insert(0, [task.time, camera.getPos(plane)]) #while len(self._hist) > 50: # self._hist.pop() # #for snapshot in self._hist: # if snapshot[0] > task.time: # break #time_delta = snapshot[0] - task.time #self.camera.setPos(plane, snapshot[1]) #print snapshot #self.camera.setPos(render, snapshot[1]) #self.camera.lookAt(plane, (0, 20+1*speed, 0)) #self.camera.setY(5+0.1*speed) #self.camera.setZ(5-0.1*speed) return Task.cont def update(self, task): return self.task(task) def destroy(self): self.removeAllTasks() self.demand("Off")