def setDrawbridgesLerpR(self, r): angle = 0 if r == 1: angle = -70 for drawbridge in self.drawbridges: if drawbridge.getR() == angle: return None ival = LerpHprInterval(drawbridge, 4.0, Vec3(drawbridge.getH(), drawbridge.getP(), angle)) ival.start()
def setDrawbridgesLerpR(self, r): angle = 0 if r == 1: angle = -70 for drawbridge in self.drawbridges: if drawbridge.getR() == angle: return ival = LerpHprInterval( drawbridge, 4.0, Vec3(drawbridge.getH(), drawbridge.getP(), angle)) ival.start()
def init_game(self): configs.IN_MENU = False interval = LerpHprInterval(self.cameraPivot, 1, (0,10,0), (0,0,0)) interval2 = LerpPosInterval(self.cameraPivot, 1, (0,0,-0.05), (0,0,0)) interval.start() interval2.start() for entity_id, entity in configs.ENTITIES.items(): if entity['CATEGORY'] == 'button': entity['BUTTON'].destroy() try: entity['R_NODE'].removeNode() except: print "nothing to remove" try: entity['L_NODE'].removeNode() except: print "nothing to remove" del(configs.ENTITIES[entity_id]) used_positions = [] for p in range(25): position = (random.uniform(-1.6, 1.6), 0, random.uniform(-0.8, 0.8)) heading = random.randint(0,360) empty_space = False while empty_space == False: empty_space = True for other_position in used_positions: distance = pow(pow((other_position[0] - position[0]),2) + pow((other_position[2] - position[2]),2),0.5) if distance < 0.25: empty_space = False position = (random.uniform(-1.6, 1.6), 0, random.uniform(-0.8, 0.8)) btn_left = configs.BUTTONS_MAP[p][0] btn_right = configs.BUTTONS_MAP[p][1] if (btn_right == '' or btn_left == '') and configs.ARDUINO_PLAYER_ON[p] == 0: continue used_positions += [position] init_player(self, btn_left, btn_right, p, pos=position, heading=heading) configs.BONUS_START_TIME[2] = globalClock.getFrameTime() if used_positions == []: self.init_menu() else: self.countDown = taskMgr.add(countDown, "countDown", extraArgs=[self, 5], appendTask=True) self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")
class CameraController: """Object to configure camera and its controls.""" def __init__(self): self._np = None # the main camera node self._target = Vec3(2, 0, MAX_Z) # the current movement final pos self._move_int = None # current move interval self._turn_int = None # current rotation interval self._with_mouse_move = None # camera is on move by mouse self._with_mouse_move_x = None # camera position before toggling centered view self._last_pos = None self._last_hpr = None self._last_np_pos = None self._last_np_hpr = None self._is_centered = False base.camLens.setNear(0.25) # noqa: F821 base.camLens.setFar(8) # noqa: F821 def _disable_ctrl_keys(self): """Ignore all the camera control keys.""" for key in ( "arrow_up", "arrow_down", "arrow_left", "arrow_right", "arrow_up-up", "arrow_down-up", "arrow_left-up", "arrow_right-up", "alt-arrow_left", "alt-arrow_right", "alt-arrow_up", "alt-arrow_down", "+", "-", "+-up", "--up", "wheel_up", "wheel_down", "mouse2", "mouse2-up", ): base.ignore(key) # noqa: F821 def _move(self, x, y, time): """Start camera movement with an interval (on key press). Args: x (float): Translation along x axis. y (float): Translation along y axis. time (float): Interval length. """ if self._move_int is not None: self._move_int.pause() if x: # if camera moves forward, consider Z coordinate # to calibrate move limits when zoomed if x == 1: x -= MAX_Z - base.cam.getZ() # noqa: F821 self._target.setX(x) else: self._target.setY(y) self._move_int = LerpPosInterval( base.cam, time, self._target, other=self._np # noqa: F821 ) self._move_int.start() def _move_with_mouse(self, task): """Implement moving camera with mouse. If mouse pointer touches a screen edge, move the camera in this direction. """ if not base.mouseWatcherNode.hasMouse(): # noqa: F821 return task.again x = base.mouseWatcherNode.getMouseX() # noqa: F821 if abs(x) > 0.98: self._with_mouse_move = True self._move(*(MAX_RIGHT_MOVE if x > 0 else MAX_LEFT_MOVE)) self._with_mouse_move_x = False return task.again z = base.mouseWatcherNode.getMouseY() # noqa: F821 if abs(z) > 0.98: self._with_mouse_move = True self._move(*MAX_UP_MOVE if z > 0 else MAX_DOWN_MOVE) self._with_mouse_move_x = True return task.again if self._with_mouse_move: self._stop(self._with_mouse_move_x) self._with_mouse_move = False return task.again def _rotate_camera_with_mouse(self, x, z, task): """Rotate the main camera according to the mouse movement. Args: x (float): The original mouse X position. z (float): The original mouse Y position. """ if not base.mouseWatcherNode.hasMouse(): # noqa: F821 return new_x = base.mouseWatcherNode.getMouseX() # noqa: F821 new_z = base.mouseWatcherNode.getMouseY() # noqa: F821 if new_x - x <= -0.125: self._np.setH(self._np.getH() - 1.5) elif new_x - x >= 0.125: self._np.setH(self._np.getH() + 1.5) elif new_z - z <= -0.125: r = self._np.getR() if r < 20: self._np.setR(r + 1.5) elif new_z - z >= 0.125: r = self._np.getR() if r > -30: self._np.setR(r - 1.5) return task.again def _set_move_keys(self): """Set camera move and rotate keys.""" # key pressed - start movement base.accept("arrow_up", self._move, MAX_UP_MOVE) # noqa: F821 base.accept("arrow_down", self._move, MAX_DOWN_MOVE) # noqa: F821 base.accept("arrow_left", self._move, MAX_LEFT_MOVE) # noqa: F821 base.accept("arrow_right", self._move, MAX_RIGHT_MOVE) # noqa: F821 # key released - stop base.accept("arrow_up-up", self._stop, [True]) # noqa: F821 base.accept("arrow_down-up", self._stop, [True]) # noqa: F821 base.accept("arrow_left-up", self._stop, [False]) # noqa: F821 base.accept("arrow_right-up", self._stop, [False]) # noqa: F821 # key pressed - start turning base.accept("alt-arrow_left", self._turn, [-360, 0]) # noqa: F821 base.accept("alt-arrow_right", self._turn, [360, 0]) # noqa: F821 base.accept("alt-arrow_up", self._turn, [0, -60]) # noqa: F821 base.accept("alt-arrow_down", self._turn, [0, 25]) # noqa: F821 # camera zooming controls base.accept("+", self._zoom, [0.7, 1.2, 1.75]) # noqa: F821 base.accept("-", self._zoom, [2, 3, 1.75]) # noqa: F821 base.accept("+-up", self._stop, [False, True, True]) # noqa: F821 base.accept("--up", self._stop, [False, True, True]) # noqa: F821 base.accept("wheel_up", self._zoom_with_mouse, [-0.25]) # noqa: F821 base.accept("wheel_down", self._zoom_with_mouse, [0.25]) # noqa: F821 base.accept("mouse2", self._turn_camera_with_mouse) # noqa: F821 base.accept( # noqa: F821 "mouse2-up", taskMgr.remove, # noqa: F821 extraArgs=["rotate_camera_with_mouse"], ) def _stop(self, stop_x, stop_zoom=False, is_hard=False): """Stop moving and rotating camera (on key release). Args: stop_x (bool): True - movement along x axis should be stopped. False - movement along y axis should be stopped. stop_zoom (bool): True if camera stopped zoom movement. is_hard (bool): If False, camera will be stopped with an deceleration interval. If True, stopping will be immediate. """ if self._move_int is not None: self._move_int.pause() if self._turn_int is not None: self._turn_int.pause() if stop_zoom: self._target = base.cam.getPos() # noqa: F821 elif stop_x: self._target.setX(base.cam.getX()) # noqa: F821 else: self._target.setY(base.cam.getY()) # noqa: F821 if not is_hard: self._move_int = LerpPosInterval( base.cam, 0.75, self._target, other=self._np # noqa: F821 ) self._move_int.start() def _toggle_centered_view(self): """Set camera onto default position. Centered position is optimal for characters manipulations. Press repeating returns camera to the previous position. """ if not self._is_centered: self._stop(False, is_hard=True) self._last_pos = base.cam.getPos() # noqa: F821 self._last_hpr = base.cam.getHpr() # noqa: F821 self._last_np_hpr = self._np.getHpr() base.cam.wrtReparentTo(base.train.model) # noqa: F821 base.cam.setPosHpr(0, 0, 1.8, 90, -90, 0) # noqa: F821 self._np.setHpr(0) self._disable_ctrl_keys() taskMgr.remove("move_camera_with_mouse") # noqa: F821 else: base.cam.wrtReparentTo(self._np) # noqa: F821 self._set_move_keys() base.cam.setPosHpr(*self._last_pos, *self._last_hpr) # noqa: F821 self._np.setHpr(*self._last_np_hpr) taskMgr.doMethodLater( # noqa: F821 0.1, self._move_with_mouse, "move_camera_with_mouse", appendTask=True) self._is_centered = not self._is_centered def _turn(self, h, r): """Turn camera with a single interval (on key press). Args: h (int): Translation for camera heading, angle. r (int): Translation for camera rolling, angle. """ if self._turn_int is not None and self._turn_int.isPlaying(): return self._turn_int = LerpHprInterval(self._np, 4, (self._np.getH() + h, 0, r)) self._turn_int.start() def _turn_camera_with_mouse(self): """Start the main camera movement by mouse.""" if not base.mouseWatcherNode.hasMouse(): # noqa: F821 return taskMgr.doMethodLater( # noqa: F821 0.01, self._rotate_camera_with_mouse, "rotate_camera_with_mouse", extraArgs=[ base.mouseWatcherNode.getMouseX(), # noqa: F821 base.mouseWatcherNode.getMouseY(), # noqa: F821 ], appendTask=True, ) def _zoom(self, x, z, zoom_time): """Zoom camera. Args: x (float): Translation along x axis. z (float): Translation along z axis. zoom_time (float): Time to zoom. """ if self._move_int is not None: self._move_int.pause() self._target.setX(x) self._target.setZ(z) self._move_int = LerpPosInterval( base.cam, zoom_time, self._target, other=self._np # noqa: F821 ) self._move_int.start() def _zoom_with_mouse(self, shift): """Zoom camera with mouse. Args: shift (float): Direction and coefficient of move. """ x, _, z = base.cam.getPos() # noqa: F821 new_x = x + shift new_z = z + shift if 2 >= new_x >= 0.7: x = new_x if 3 >= new_z >= 1.2: z = new_z self._zoom(x, z, 0.2) def enable_ctrl_keys(self): """Enable all the camera control keys.""" self._set_move_keys() base.accept("c", self._toggle_centered_view) # noqa: F821 taskMgr.doMethodLater( # noqa: F821 0.1, self._move_with_mouse, "move_camera_with_mouse", appendTask=True) def push(self): """Camera push caused by a kick/explosion.""" if self._move_int is not None: self._move_int.pause() if self._turn_int is not None: self._turn_int.pause() self._move_int = Sequence() self._target.setZ(base.cam.getZ() - random.uniform(-0.02, 0.02)) # noqa: F821 self._target.setX(base.cam.getX() + random.uniform(-0.03, 0.03)) # noqa: F821 self._target.setY(base.cam.getY() + random.uniform(-0.03, 0.03)) # noqa: F821 self._move_int.append( Parallel( LerpPosInterval( base.cam, 0.15, self._target, bakeInStart=False, # noqa: F821 ), LerpHprInterval( self._np, 0.15, ( self._np.getH() + random.uniform(-1, 1), self._np.getP() + random.uniform(-2, 2), self._np.getR() + random.uniform(-3, 3), ), ), )) self._target.setX(base.cam.getX()) # noqa: F821 self._target.setY(base.cam.getY()) # noqa: F821 self._target.setZ(base.cam.getZ()) # noqa: F821 self._move_int.append( Parallel( LerpPosInterval( base.cam, # noqa: F821 0.5, self._target, bakeInStart=False, blendType="easeOut", ), LerpHprInterval( self._np, 0.5, (self._np.getH(), 0, self._np.getR()), blendType="easeOut", ), )) self._move_int.start() def set_controls(self, train): """Configure the main camera and its controls. Args: train (train.Train): The Train object. """ base.disableMouse() # noqa: F821 self._np = train.node.attachNewNode("camera_node") base.cam.reparentTo(self._np) # noqa: F821 base.cam.setPos(self._target) # noqa: F821 base.cam.lookAt(train.model) # noqa: F821 self._set_move_keys() base.accept("c", self._toggle_centered_view) # noqa: F821 taskMgr.doMethodLater( # noqa: F821 0.1, self._move_with_mouse, "move_camera_with_mouse", appendTask=True) def set_hangar_pos(self, hangar): """Set camera to hangar position. Args: hangar (panda3d.core.NodePath): Hangar model. """ self._disable_ctrl_keys() self._stop(True, is_hard=True) base.ignore("c") # noqa: F821 self._last_pos = base.cam.getPos() # noqa: F821 self._last_hpr = base.cam.getHpr() # noqa: F821 self._last_np_pos = self._np.getPos() self._last_np_hpr = self._np.getHpr() base.cam.setPos(0) # noqa: F821 base.cam.setHpr(0) # noqa: F821 self._np.reparentTo(hangar) self._np.setPosHpr(-0.35, 1.36, 0.12, -163, 5, 0) def unset_hangar_pos(self): """Return camera back to normal position.""" base.cam.setPosHpr(*self._last_pos, *self._last_hpr) # noqa: F821 self._np.reparentTo(base.train.node) # noqa: F821 self._np.setPosHpr(*self._last_np_pos, *self._last_np_hpr)
def init_menu(self): interval = LerpHprInterval(self.cameraPivot, 1, (0,0,0), (0,10,0)) interval2 = LerpPosInterval(self.cameraPivot, 1, (0,0,0), (0,0,-0.05)) interval.start() interval2.start() taskMgr.doMethodLater(1, spawnButton, "spawnButton")