def save(self): layout = [self.contents[x][y].tile_type for x, y in double_range(self.get_width, self.get_height)] obstacle_layout = [] character_layout = [] for x, y in double_range(self.get_width, self.get_height): if isinstance(self.contents[x][y].rigid_body, character.Obstacle): obstacle_layout.append(self.contents[x][y].rigid_body.save_type()) else: obstacle_layout.append(None) if isinstance(self.contents[x][y].rigid_body, character.Character): character_layout.append(self.contents[x][y].rigid_body.save_type()) else: character_layout.append(None) save_byte((self.get_width, self.get_height, layout, obstacle_layout, character_layout), self.file_path)
def initialize(conn): for (x, y) in double_range(conn.contents.width, conn.contents.height): tile = conn.contents[x][y] tile.scene_pos_x, tile.scene_pos_y = x, y tile.initialize(x, y, Res.ground_image) if isinstance(tile.rigid_body, character.RigidBody): tile.rigid_body.initialize(x, y)
def refresh_character(self): for dx, dy in double_range(-SIGHT_AREA, SIGHT_AREA + 1, -SIGHT_AREA, SIGHT_AREA + 1): tile = self.scene.get_tile_absolute(self.tile.scene_pos_x + dx, self.tile.scene_pos_y + dy) if tile and isinstance(tile.rigid_body, Character) and self.check_tile(dx, dy)[1]: self.visible_characters.append(tile.rigid_body)
def initialize(conn): for i in range(9): for x, y in double_range(LOCAL_SIZE, LOCAL_SIZE): conn.contents[i][x][y].scene_pos_x = (1 + Pos.direction[i][0]) * LOCAL_SIZE + x conn.contents[i][x][y].scene_pos_y = (1 + Pos.direction[i][1]) * LOCAL_SIZE + y conn.contents[i][x][y].initialize(conn.contents[i][x][y].scene_pos_x, conn.contents[i][x][y].scene_pos_y) if conn.contents[i][x][y].rigid_body is not None: conn.contents[i][x][y].rigid_body.initialize(conn.contents[i][x][y].scene_pos_x, conn.contents[i][x][y].scene_pos_y)
def uninstall(self, scene): self.save() for x, y in double_range(self.get_width, self.get_height): temp, self.contents[x][y] = self.contents[x][y], None scene.removeItem(temp) if isinstance(temp.rigid_body, character.Obstacle): scene.removeItem(temp.rigid_body) del temp.rigid_body elif isinstance(temp.rigid_body, character.Character): scene.removeItem(temp.rigid_body) if temp.rigid_body in scene.characters: scene.characters.remove(temp.rigid_body) del temp.rigid_body del temp self.contents = None
def find_way(self, target_x, target_y): if not (-10 <= target_x < 10 and -10 < target_y < 10): return Pos.r_direction[(sgn(target_x), sgn(target_y))] map_data = [[-1 for i in range(20)] for j in range(20)] for dx, dy in double_range(0, 20, 0, 20): x, y = dx + self.tile.scene_pos_x - 10, dy + self.tile.scene_pos_y - 10 if 0 <= x < self.scene.get_width() and 0 <= y < self.scene.get_height(): if self.scene.get_tile_absolute(x, y).rigid_body: map_data[dx][dy] = -1 else: map_data[dx][dy] = 0 else: map_data[dx][dy] = -1 map_data[target_x + 10][target_y + 10] = 0 return Calculate.find_way(map_data, 10, 10, target_x + 10, target_y + 10)
def find_way(self, target_x, target_y): if not (-10 <= target_x < 10 and -10 < target_y < 10): return Pos.r_direction[(sgn(target_x), sgn(target_y))] map_data = [[-1 for i in range(20)] for j in range(20)] for dx, dy in double_range(0, 20, 0, 20): x, y = dx + self.tile.scene_pos_x - 10, dy + self.tile.scene_pos_y - 10 if 0 <= x < self.scene.get_width( ) and 0 <= y < self.scene.get_height(): if self.scene.get_tile_absolute(x, y).rigid_body: map_data[dx][dy] = -1 else: map_data[dx][dy] = 0 else: map_data[dx][dy] = -1 map_data[target_x + 10][target_y + 10] = 0 return Calculate.find_way(map_data, 10, 10, target_x + 10, target_y + 10)
def refresh(self): for tile in self.scene.get_all_tiles(): tile.visible = False tile.next_opacity = 0.01 for dx, dy in double_range( -SIGHT_AREA, SIGHT_AREA + 1, -SIGHT_AREA, SIGHT_AREA + 1): tile = self.scene.get_tile_absolute(self.tile.scene_pos_x + dx, self.tile.scene_pos_y + dy) if not tile: continue distance, visible = self.check_tile(dx, dy) tile.visible = visible if tile.visible: tile.setZValue(-1.1) else: tile.setZValue(-0.9) tile.next_opacity = min(1.0, visible + 0.01) if tile.rigid_body: tile.rigid_body.next_opacity = tile.next_opacity
def refresh(self): for tile in self.scene.get_all_tiles(): tile.visible = False tile.next_opacity = 0.01 for dx, dy in double_range(-SIGHT_AREA, SIGHT_AREA + 1, -SIGHT_AREA, SIGHT_AREA + 1): tile = self.scene.get_tile_absolute(self.tile.scene_pos_x + dx, self.tile.scene_pos_y + dy) if not tile: continue distance, visible = self.check_tile(dx, dy) tile.visible = visible if tile.visible: tile.setZValue(-1.1) else: tile.setZValue(-0.9) tile.next_opacity = min(1.0, visible + 0.01) if tile.rigid_body: tile.rigid_body.next_opacity = tile.next_opacity
def random_create(self, scene, ground_types, obstacle_types): for (x, y) in double_range(self.get_width, self.get_height): self.contents[x][y] = Tile(x, y, ground_types[randrange(len(ground_types))], self, Res.ground_image) scene.addItem(self.contents[x][y]) check1 = randrange(20) check1_1 = randrange(5) check2 = randrange(20) if not check1: if check1_1: self.contents[x][y].rigid_body = character.Obstacle.create( obstacle_types[randrange(len(obstacle_types))], "forest_small", scene, self.contents[x][y], Res.obstacle_images, centre_y=0.9) else: self.contents[x][y].rigid_body = character.Obstacle.create( randrange(4), "forest_large", scene, self.contents[x][y], Res.obstacle_images, rigid_height=4.0, rigid_size=2.0, centre_y=0.9) scene.addItem(self.contents[x][y].rigid_body) elif not check2: self.contents[x][y].rigid_body = character.NonePlayer( (("female", "male", "others")[randrange(3)], randrange(3)), scene, self.contents[x][y], [0], action_time=scene.current_time) scene.characters.append(self.contents[x][y].rigid_body) scene.addItem(self.contents[x][y].rigid_body)
def load_init(self, scene): result = load_byte(self.file_path) if result: self._width, self._height, layout, obstacle_layout, character_layout = result self.contents = [[None for x in range(self._height)] for y in range(self._width)] index = 0 for x, y in double_range(self._width, self._height): self.contents[x][y] = Tile(x, y, layout[index], self, Res.ground_image) scene.addItem(self.contents[x][y]) if obstacle_layout[index] is not None: rigid_body_save = obstacle_layout[index] self.contents[x][y].rigid_body = character.Obstacle.load( rigid_body_save, self, self.contents[x][y], Res.obstacle_images) scene.addItem(self.contents[x][y].rigid_body) if character_layout[index] is not None: self.contents[x][y].rigid_body = character.NonePlayer.load( character_layout[index], self.contents[x][y], scene, scene.current_time) scene.characters.append(self.contents[x][y].rigid_body) scene.addItem(self.contents[x][y].rigid_body) index += 1 return True else: return False
def refresh_character(self): for dx, dy in double_range( -SIGHT_AREA, SIGHT_AREA + 1, -SIGHT_AREA, SIGHT_AREA + 1): tile = self.scene.get_tile_absolute(self.tile.scene_pos_x + dx, self.tile.scene_pos_y + dy) if tile and isinstance(tile.rigid_body, Character) and self.check_tile(dx, dy)[1]: self.visible_characters.append(tile.rigid_body)
def random_create(self, *content_types): assert isinstance(self.contents, list), "content not set or incorrect!" for (x, y) in double_range(self.get_width, self.get_height): self.contents[x][y] = content_types[randrange(len(content_types))]