def move_selection(vector): selection = self.arena.vec_to_tile_coord( p3d.LVector2(self.selected_tile) + p3d.LVector2(vector)) self.menu_helper.sfx_select.play() if self.arena.tile_coord_in_bounds(selection): self.selected_tile = selection
def update(self, dt): # Update player position movvec = self.target - self.player.get_pos() newpos = self.player.get_pos() if movvec.length_squared() < 0.4: newpos.set_x(self.target.x) newpos.set_y(self.target.y) else: movvec.normalize() movvec *= self.PLAYER_SPEED * dt newpos.set_x(self.player.get_x() + movvec.x) newpos.set_y(self.player.get_y() + movvec.y) self.player.look_at(newpos) if self.dungeon.is_walkable(*newpos.xy): self.player.set_pos(newpos) if self.dungeon.is_exit(*newpos.xy): next_didx = self.dungeon_idx + 1 if next_didx >= len(self.dungeons): # Create a new dungeon self.dungeons.append( Dungeon(self.mapgen, self.DUNGEON_SX, self.DUNGEON_SY)) self.switch_to_dungeon(next_didx) if self.last_tele_loc is not None: if (self.last_tele_loc - self.player.get_pos()).length_squared() > 3: self.last_tele_loc = None if self.last_tele_loc is None: teleloc = self.dungeon.get_tele_loc(*newpos.xy) if teleloc is not None: newpos.x, newpos.y = teleloc self.last_tele_loc = p3d.LVector3(newpos) self.target = p3d.LVector3(newpos) self.player.set_pos(newpos) self.reset_camera() if not self.debug_cam and base.mouseWatcherNode.has_mouse(): mousex, mousey = base.mouseWatcherNode.get_mouse() border = self.CAM_MOVE_BORDER camdelta = p3d.LVector2(0, 0) if mousex < -border: camdelta.x -= 1 elif mousex > border: camdelta.x += 1 if mousey < -border: camdelta.y -= 1 elif mousey > border: camdelta.y += 1 camdelta.normalize() camdelta *= self.CAM_MOVE_SPEED * dt campos = base.cam.get_pos() campos.x += camdelta.x campos.y += camdelta.y base.cam.set_pos(campos)
def tile_get_facing_to(self, from_tile, to_tile): posa = p3d.LVector2(from_tile) posb = p3d.LVector2(to_tile) direction = posb - posa direction.normalize() direction.x = round(direction.x) direction.y = round(direction.y) facing = [int(i) for i in direction] # Special cases if abs(facing[0]) == 1 and abs(facing[1]) == 1: facing[1] = 0 return tuple(facing)
def find_tile_at_range(self, combatant, other, target_range): distance = self.arena.tile_distance(combatant.tile_position, other.tile_position) inc = 1 if target_range < distance else -1 new_pos = combatant.tile_position for target in range(target_range, distance, inc): direction = p3d.LVector2( self.arena.tile_get_facing_to(combatant.tile_position, other.tile_position)) other_pos = p3d.LVector2(other.tile_position) pos = self.arena.vec_to_tile_coord(-direction * target + other_pos) pos_legal = (self.arena.tile_coord_in_bounds(pos) and not self.combatant_in_tile(pos)) if pos_legal: new_pos = pos break return new_pos
def handle_window_event(self, window): self.window_w, self.window_h = window.getSize() min_dim = min(self.window_w, self.window_h) self.pad_radius = min_dim * 0.1 if self.window_w < 1300: self.pad_center = core.LVector2(min_dim * 0.15, min_dim * 0.15) else: self.pad_center = core.LVector2(self.window_w * 0.75, self.window_h * 0.5) self.pad_outline.setPos(self.pixels_to_im_coords(self.pad_center)) self.pad_outline.setScale(2 * self.pad_radius / min_dim) self.pad_outline.show() self.pad_response_circle.setScale(0.2 * self.pad_radius / min_dim) # Propagate event self.windowEvent(window)
def __init__(self): super().__init__() # Unnecessary if using vsync with 60 FPS monitor, otherwise uncomment # globalClock.setMode(core.ClockObject.M_limited) # globalClock.setFrameRate(60) self.motion_controller = load_motion_controller(sys.argv[1], self.heightmap, *sys.argv[2:]) self.target_vel = core.LVector2() self.target_dir = 90 self.strafe_amount = 0 self.crouch_amount = 0 self.gaits = np.zeros(6) self.strafe_pressed, self.crouch_pressed, self.run_pressed = 0, 0, 0 self.accept('lshift', self.set_strafe_pressed, [1]) self.accept('lshift-up', self.set_strafe_pressed, [0]) self.accept('c', self.set_crouch_pressed, [1]) self.accept('c-up', self.set_crouch_pressed, [0]) self.accept('r', self.set_run_pressed, [1]) self.accept('r-up', self.set_run_pressed, [0])
def update_pad(self, task): if self.controlling_pad and self.mouseWatcherNode.hasMouse(): mpos = self.screen_coords_to_pixels( self.mouseWatcherNode.getMouse()) pad_input = (mpos - self.pad_center) * PAD_SENSITIVITY if pad_input.lengthSquared() > 1: pad_input.normalize() resp_circle_pos = self.pad_center + pad_input * (self.pad_radius * 0.9) self.pad_response_circle.setPos( self.pixels_to_im_coords(resp_circle_pos)) self.pad_response_circle.show() else: pad_input = core.LVector2() self.pad_response_circle.hide() pad_input.setY(-pad_input.getY()) # flip y self.pad_input = pad_input return Task.cont
def tile_distance(self, tilea, tileb): distvec = p3d.LVector2(tilea) - p3d.LVector2(tileb) return int(abs(distvec.x) + abs(distvec.y))
def screen_coords_to_pixels(self, point): x_frac, y_frac = (point + 1.0) / 2.0 return core.LVector2(x_frac * self.window_w, (1 - y_frac) * self.window_h)