def __init__(self): ShowBase.__init__(self) # Disable the camera trackball controls. self.disableMouse() self.setBackgroundColor(0.5,0.5,0.5) self.terrain = Terrain(self) # Add a fog fog = Fog("A linear-mode Fog node") fog.setMode(Fog.MExponential) fog.setColor(0.5,0.5,0.5) fog.setExpDensity(0.01) self.render.setFog(fog) # helper maps for storing times self.lastPositioningTime = {} self.lastTurningTime = {} # Load and transform the panda actor. self.pandaActor = Actor("models/panda-model", {"walk": "models/panda-walk4"}) self.pandaActor.setScale(0.005, 0.005, 0.005) self.pandaActor.reparentTo(self.render) # setup keyboard events self.accept('arrow_up', self.pandaGo, [1]) self.accept('arrow_up-up', self.pandaStop, [1]) self.accept('arrow_down', self.pandaGo, [-1]) self.accept('arrow_down-up', self.pandaStop, [-1]) self.accept('arrow_left', self.pandaTurn, [-1]) self.accept('arrow_left-up', self.pandaStopTurning, [-1]) self.accept('arrow_right', self.pandaTurn, [1]) self.accept('arrow_right-up', self.pandaStopTurning, [1]) # add task for moving the camera self.taskMgr.add(self.spinCameraByPanda, "SpinCameraTask")
def gameLoop(self, task): #Compensate for inconsistent update intervals dt = globalClock.getDt() if self.GAME_MODE == MAIN_MENU: if not self.mode_initialized: self.buildMainMenu() self.mode_initialized = True if self.GAME_MODE == IN_GAME_MENU: if not self.mode_initialized: #Fog out background inGameMenuFogColor = (50, 150, 50) inGameMenuFog = Fog("inGameMenuFog") inGameMenuFog.setMode(Fog.MExponential) inGameMenuFog.setColor(*inGameMenuFogColor) inGameMenuFog.setExpDensity(.01) render.setFog(inGameMenuFog) self.buildInGameMenu() self.mode_initialized = True if self.GAME_MODE == PLAY: if not self.mode_initialized: props = WindowProperties() props.setCursorHidden(True) base.win.requestProperties(props) self.last_mouse_x = self.win.getPointer(0).getX() self.last_mouse_y = self.win.getPointer(0).getY() self.mode_initialized = True if self.play_mode == TERRAIN: self.maintainTurrets() self.avatar.move(dt) elif self.play_mode == SPACE: self.asteroidManager.maintainAsteroidField(self.avatar.objectNP.getPos(), self.avatar.speed, dt) #Handle keyboard input self.avatar.handleKeys(self.keys, self.play_mode) ########## Mouse-based viewpoint rotation ########## mouse_pos = self.win.getPointer(0) current_mouse_x = mouse_pos.getX() current_mouse_y = mouse_pos.getY() #Side to side if self.play_mode == TERRAIN: mouse_shift_x = current_mouse_x - self.last_mouse_x self.last_mouse_x = current_mouse_x if current_mouse_x < 5 or current_mouse_x >= (self.win_center_x * 1.5): base.win.movePointer(0, self.win_center_x, current_mouse_y) self.last_mouse_x = self.win_center_x yaw_shift = -((mouse_shift_x) * Camera.ROT_RATE[0]) self.avatar.yawRot += yaw_shift self.avatar.objectNP.setH(self.avatar.yawRot) #Up and down mouse_shift_y = current_mouse_y - self.last_mouse_y self.last_mouse_y = current_mouse_y if current_mouse_y < 5 or current_mouse_y >= (self.win_center_y * 1.5): base.win.movePointer(0, current_mouse_x, self.win_center_y) self.last_mouse_y = self.win_center_y pitch_shift = -((mouse_shift_y) * Camera.ROT_RATE[1]) self.mainCamera.pitchRot += pitch_shift if self.mainCamera.pitchRot > Camera.FLEX_ROT_MAG[0]: self.mainCamera.pitchRot = Camera.FLEX_ROT_MAG[0] elif self.mainCamera.pitchRot < -Camera.FLEX_ROT_MAG[0]: self.mainCamera.pitchRot = -Camera.FLEX_ROT_MAG[0] xy_plane_cam_dist = Camera.AVATAR_DIST cam_x_adjust = xy_plane_cam_dist*sin(radians(self.avatar.yawRot)) cam_y_adjust = xy_plane_cam_dist*cos(radians(self.avatar.yawRot)) cam_z_adjust = Camera.ELEVATION self.mainCamera.camObject.setH(self.avatar.yawRot) self.mainCamera.camObject.setP(self.mainCamera.pitchRot) self.mainCamera.camObject.setPos(self.avatar.objectNP.getX() + cam_x_adjust, self.avatar.objectNP.getY() - cam_y_adjust, self.avatar.objectNP.getZ() + cam_z_adjust) #Find collisions #self.cTrav.traverse(render) #print self.environ.getBounds() return Task.cont
class ToontownFogManager: def __init__(self): self.fog = None self.fogMode = None print("Initialized Fog Manager") self.fog = Fog("ActiveFog") self.fog.setColor(1, 1, 1) self.fogTypes = { 0: Fog.MExponential, 1: Fog.MExponentialSquared, 2: Fog.MLinear } def setFog(self, np): np.setFog(self.fog) def setColor(self, r, g, b): self.fog.setColor(r, g, b) def clearFog(self): return render.clearFog() def getFogMode(self): self.fogMode = self.fog.getMode() def setFogMode(self, id): mode = self.fog.setMode(self.fogTypes[id]) self.fogMode = mode ## For Exponential Fog def setDensity(self, density): """ Determines the density value used for exponential fog calculations. :param float density: A value between [0, 1] """ self.fog.setExpDensity(density) ## For Linear Fog def setLinearRange(self, range, opacity): """ Specifies the effects of the fog in linear distance units. This is only used if the mode is M_linear. This specifies a fog that begins at distance onset units from the origin, and becomes totally opaque at distance <opaque units> from the origin, along the forward axis (usually Y). This function also implicitly sets the mode the M_linear, if it is not already set. :param float opacity: [0, 1] """ self.fog.setLinearRange(range, opacity) def setLinearFallback(self, angle, onset, opaque): """ :param float angle: the minimum viewing angle (angle between the camera direction and fog direction) at which the fallback effect will be employed. :type onset: float :param float opaque: [0, 1] Defines how the fog should be rendered when the fog effect is diminished in this way. Onset and opaque specify camera-relative onset and opaque distances that will be fallen back on, overriding the Fog node's own onset and opaque distances. The linear fallback workaround will only look good in certain situations, for example when the fog is deep inside a dark cave. So in general, exponential mode fog is more useful than the default linear mode fog. """ self.fog.setLinearFallback(angle, onset, opaque)
def gameLoop(self, task): #Compensate for inconsistent update intervals dt = globalClock.getDt() if self.GAME_MODE == MAIN_MENU: if not self.mode_initialized: self.buildMainMenu() self.mode_initialized = True if self.GAME_MODE == IN_GAME_MENU: if not self.mode_initialized: inGameMenuFogColor = (50, 150, 50) inGameMenuFog = Fog("inGameMenuFog") inGameMenuFog.setMode(Fog.MExponential) inGameMenuFog.setColor(*inGameMenuFogColor) inGameMenuFog.setExpDensity(.01) render.setFog(inGameMenuFog) self.buildInGameMenu() self.mode_initialized = True if self.GAME_MODE == NORMAL: if not self.mode_initialized: props = WindowProperties() props.setCursorHidden(True) base.win.requestProperties(props) self.last_mouse_x = self.win.getPointer(0).getX() self.last_mouse_y = self.win.getPointer(0).getY() self.mode_initialized = True #Handle keyboard input self.avatar.handleKeys(self.keys) self.avatar.move(dt) #Mouse-based viewpoint rotation mouse_pos = self.win.getPointer(0) current_mouse_x = mouse_pos.getX() current_mouse_y = mouse_pos.getY() mouse_shift_x = current_mouse_x - self.last_mouse_x mouse_shift_y = current_mouse_y - self.last_mouse_y self.last_mouse_x = current_mouse_x self.last_mouse_y = current_mouse_y if current_mouse_x < 5 or current_mouse_x >= (self.win_center_x * 1.5): base.win.movePointer(0, self.win_center_x, current_mouse_y) self.last_mouse_x = self.win_center_x if current_mouse_y < 5 or current_mouse_y >= (self.win_center_y * 1.5): base.win.movePointer(0, current_mouse_x, self.win_center_y) self.last_mouse_y = self.win_center_y yaw_shift = -((mouse_shift_x) * Camera.ROT_RATE[0]) pitch_shift = -((mouse_shift_y) * Camera.ROT_RATE[1]) self.avatar.yawRot += yaw_shift self.mainCamera.pitchRot += pitch_shift if self.mainCamera.pitchRot > Camera.MAX_PITCH_ROT: self.mainCamera.pitchRot = Camera.MAX_PITCH_ROT elif self.mainCamera.pitchRot < Camera.MIN_PITCH_ROT: self.mainCamera.pitchRot = Camera.MIN_PITCH_ROT self.avatar.objectNP.setH(self.avatar.yawRot) self.mainCamera.camObject.setH(self.avatar.yawRot) self.mainCamera.camObject.setP(self.mainCamera.pitchRot) if self.NAVIGATION_MODE == TERRAIN: xy_plane_cam_dist = Camera.AVATAR_DIST cam_z_adjust = Camera.ELEVATION elif self.NAVIGATION_MODE == SPACE: xy_plane_cam_dist = Camera.AVATAR_DIST*cos(radians(self.pitchRot)) cam_z_adjust = Camera.AVATAR_DIST*sin(radians(self.pitchRot)) cam_x_adjust = xy_plane_cam_dist*sin(radians(self.avatar.yawRot)) cam_y_adjust = xy_plane_cam_dist*cos(radians(self.avatar.yawRot)) self.mainCamera.camObject.setPos(self.avatar.objectNP.getX() + cam_x_adjust, self.avatar.objectNP.getY() - cam_y_adjust, self.avatar.objectNP.getZ() + cam_z_adjust) #Find collisions self.cTrav.traverse(render) return Task.cont
def gameLoop(self, task): #Compensate for inconsistent update intervals dt = globalClock.getDt() if self.GAME_MODE == MAIN_MENU: if not self.mode_initialized: self.buildMainMenu() self.mode_initialized = True if self.GAME_MODE == IN_GAME_MENU: if not self.mode_initialized: inGameMenuFogColor = (50, 150, 50) inGameMenuFog = Fog("inGameMenuFog") inGameMenuFog.setMode(Fog.MExponential) inGameMenuFog.setColor(*inGameMenuFogColor) inGameMenuFog.setExpDensity(.01) render.setFog(inGameMenuFog) self.buildInGameMenu() self.mode_initialized = True if self.GAME_MODE == NORMAL: if not self.mode_initialized: props = WindowProperties() props.setCursorHidden(True) base.win.requestProperties(props) self.last_mouse_x = self.win.getPointer(0).getX() self.last_mouse_y = self.win.getPointer(0).getY() self.mode_initialized = True #Handle keyboard input self.avatar.handleKeys(self.keys) self.avatar.move(dt) #Mouse-based viewpoint rotation mouse_pos = self.win.getPointer(0) current_mouse_x = mouse_pos.getX() current_mouse_y = mouse_pos.getY() mouse_shift_x = current_mouse_x - self.last_mouse_x mouse_shift_y = current_mouse_y - self.last_mouse_y self.last_mouse_x = current_mouse_x self.last_mouse_y = current_mouse_y if current_mouse_x < 5 or current_mouse_x >= (self.win_center_x * 1.5): base.win.movePointer(0, self.win_center_x, current_mouse_y) self.last_mouse_x = self.win_center_x if current_mouse_y < 5 or current_mouse_y >= (self.win_center_y * 1.5): base.win.movePointer(0, current_mouse_x, self.win_center_y) self.last_mouse_y = self.win_center_y yaw_shift = -((mouse_shift_x) * Camera.ROT_RATE[0]) pitch_shift = -((mouse_shift_y) * Camera.ROT_RATE[1]) self.avatar.yawRot += yaw_shift self.mainCamera.pitchRot += pitch_shift if self.mainCamera.pitchRot > Camera.MAX_PITCH_ROT: self.mainCamera.pitchRot = Camera.MAX_PITCH_ROT elif self.mainCamera.pitchRot < Camera.MIN_PITCH_ROT: self.mainCamera.pitchRot = Camera.MIN_PITCH_ROT self.avatar.objectNP.setH(self.avatar.yawRot) self.mainCamera.camObject.setH(self.avatar.yawRot) self.mainCamera.camObject.setP(self.mainCamera.pitchRot) if self.NAVIGATION_MODE == TERRAIN: xy_plane_cam_dist = Camera.AVATAR_DIST cam_z_adjust = Camera.ELEVATION elif self.NAVIGATION_MODE == SPACE: xy_plane_cam_dist = Camera.AVATAR_DIST * cos( radians(self.pitchRot)) cam_z_adjust = Camera.AVATAR_DIST * sin(radians(self.pitchRot)) cam_x_adjust = xy_plane_cam_dist * sin(radians(self.avatar.yawRot)) cam_y_adjust = xy_plane_cam_dist * cos(radians(self.avatar.yawRot)) self.mainCamera.camObject.setPos( self.avatar.objectNP.getX() + cam_x_adjust, self.avatar.objectNP.getY() - cam_y_adjust, self.avatar.objectNP.getZ() + cam_z_adjust) #Find collisions self.cTrav.traverse(render) return Task.cont
def gameLoop(self, task): dt = globalClock.getDt() self.processKeys() if self.gameMode["display"] == MAIN_MENU: if not self.mode_initialized: self.buildMainMenu() self.mode_initialized = True if self.gameMode["display"] == IN_GAME_MENU: if not self.mode_initialized: #Fog out background inGameMenuFogColor = (50, 150, 50) inGameMenuFog = Fog("inGameMenuFog") inGameMenuFog.setMode(Fog.MExponential) inGameMenuFog.setColor(*inGameMenuFogColor) inGameMenuFog.setExpDensity(.01) render.setFog(inGameMenuFog) self.buildInGameMenu() self.mode_initialized = True if self.gameMode["display"] == DEAD: if not self.mode_initialized: self.buildDeathScreen() self.mode_initialized = True if self.gameMode["display"] == PLAY: alive = self.avatar.states["alive"] if not self.mode_initialized: self.toggleCursor(True) self.last_mouse_x = self.win.getPointer(0).getX() self.last_mouse_y = self.win.getPointer(0).getY() self.mode_initialized = True if self.gameMode["play"] == TERRAIN: if alive: self.maintainTurrets() self.avatar.move(dt) else: self.switchDisplayMode(DEAD) elif self.gameMode["play"] == SPACE: if alive: self.asteroidManager.maintainAsteroidField( self.avatar.objectNP.getPos(), self.avatar.speed, Camera.AVATAR_DIST, dt) else: self.switchDisplayMode(DEAD) if alive: #Handle keyboard input self.avatar.handleKeys(self.keys, self.gameMode["play"]) ########## Mouse-based viewpoint rotation ########## mouse_pos = self.win.getPointer(0) current_mouse_x = mouse_pos.getX() current_mouse_y = mouse_pos.getY() #Side to side if self.gameMode["play"] == TERRAIN: mouse_shift_x = current_mouse_x - self.last_mouse_x self.last_mouse_x = current_mouse_x if current_mouse_x < 5 or current_mouse_x >= ( self.win_center_x * 1.5): base.win.movePointer(0, self.win_center_x, current_mouse_y) self.last_mouse_x = self.win_center_x yaw_shift = -((mouse_shift_x) * Camera.ROT_RATE[0]) self.avatar.yawRot += yaw_shift self.avatar.objectNP.setH(self.avatar.yawRot) #Up and down mouse_shift_y = current_mouse_y - self.last_mouse_y self.last_mouse_y = current_mouse_y if current_mouse_y < 5 or current_mouse_y >= ( self.win_center_y * 1.5): base.win.movePointer(0, current_mouse_x, self.win_center_y) self.last_mouse_y = self.win_center_y pitch_shift = -((mouse_shift_y) * Camera.ROT_RATE[1]) self.mainCamera.pitchRot += pitch_shift if self.mainCamera.pitchRot > Camera.FLEX_ROT_BOUND[0]: self.mainCamera.pitchRot = Camera.FLEX_ROT_BOUND[0] elif self.mainCamera.pitchRot < -Camera.FLEX_ROT_BOUND[0]: self.mainCamera.pitchRot = -Camera.FLEX_ROT_BOUND[0] xy_plane_cam_dist = Camera.AVATAR_DIST cam_x_adjust = xy_plane_cam_dist * sin( radians(self.avatar.yawRot)) cam_y_adjust = xy_plane_cam_dist * cos( radians(self.avatar.yawRot)) cam_z_adjust = Camera.ELEVATION self.mainCamera.camObject.setH(self.avatar.yawRot) self.mainCamera.camObject.setP(self.mainCamera.pitchRot) self.mainCamera.camObject.setPos( self.avatar.objectNP.getX() + cam_x_adjust, self.avatar.objectNP.getY() - cam_y_adjust, self.avatar.objectNP.getZ() + cam_z_adjust) #Find collisions self.cTrav.traverse(render) return Task.cont