def add_move_animation(self, obj: Union[Army], destination: (int, int), time_ms): start = HexMap.offset_to_pixel_coords(obj.tile.offset_coordinates) dest = HexMap.offset_to_pixel_coords(destination) move = Animator.MoveAnimation(start, dest, time_ms, obj, self.camera_pos) self.move_animations.append(move)
def add_building(self, building: Building, player: Player): # hint("adding a building") player.buildings.append(building) position = HexMap.offset_to_pixel_coords( building.tile.offset_coordinates) building.set_sprite_pos(position, self.__camera_pos) self.__set_sprite(building, building.tex_code) building.set_state_active() if building.construction_time > 0: building.add_tex_construction(self.texture_store.get_texture("cs")) building.set_state_construction() building.add_tex_destruction(self.texture_store.get_texture("ds")) self.z_levels[Z_GAME_OBJ].append(building.sprite) # add the flag: # flag = Flag((position[0] + building.flag_offset[0], position[1] + building.flag_offset[1]), # player.colour) #self.add_flag(flag, player.colour_code) pos = (position[0] + building.flag_offset[0] + self.__camera_pos[0], position[1] + building.flag_offset[1] + self.__camera_pos[1]) flag = self.add_animated_flag(player.colour_code, pos) building.flag = flag if building.building_type == BuildingType.FARM: for a in building.associated_tiles: self.extend_building(building, a, "cf") if building.building_type == BuildingType.VILLAGE_1 or \ building.building_type == BuildingType.VILLAGE_2 or \ building.building_type == BuildingType.VILLAGE_3: for n in self.hex_map.get_neighbours(building.tile): building.associated_tiles.append(n) building.tile.ground.walkable = False building.tile.ground.buildable = False self.toggle_fog_of_war_lw(player.discovered_tiles) self.__reorder_spritelist(self.z_levels[Z_GAME_OBJ])
def add_resource(self, resource: Resource): self.scenario.resource_list.append(resource) resource.set_sprite_pos( HexMap.offset_to_pixel_coords(resource.tile.offset_coordinates), self.__camera_pos) self.__set_sprite(resource, resource.tex_code) self.z_levels[Z_GAME_OBJ].append(resource.sprite)
def add_army(self, army: Army, player: Player): player.armies.append(army) army.set_sprite_pos( HexMap.offset_to_pixel_coords(army.tile.offset_coordinates), self.__camera_pos) army.is_barbaric = player.is_barbaric self.__set_sprite(army, "f1_" + player.colour_code) self.toggle_fog_of_war_lw(player.discovered_tiles) self.z_levels[Z_GAME_OBJ].append(army.sprite)
def __add_aux_sprite(self, hex: Hexagon, tex_code: str): aux = Drawable() self.scenario.aux_sprites.append((hex, aux)) aux.set_sprite_pos( HexMap.offset_to_pixel_coords(hex.offset_coordinates), self.__camera_pos) self.__set_sprite(aux, tex_code) self.z_levels[Z_AUX].append(aux.sprite) self.__reorder_spritelist(self.z_levels[Z_AUX])
def extend_building(self, building: Building, tile: Hexagon, tex_code: str): # building.associated_tiles.append(tile) drawable = Drawable() drawable.set_sprite_pos( HexMap.offset_to_pixel_coords(tile.offset_coordinates), self.__camera_pos) building.associated_drawables.append(drawable) drawable.sprite.alpha = 100 self.__set_sprite(drawable, tex_code) self.z_levels[Z_GAME_OBJ].append(drawable.sprite)
def updata_map(self): """this function makes sure that the map remains well defined""" for hex in self.hex_map.map: if hex.ground.ground_type == GroundType.OTHER: error("Unknown ground type is a problem! {}".format( hex.offset_coordinates)) if hex.ground.ground_type == GroundType.GRASS or\ hex.ground.ground_type == GroundType.STONE or\ hex.ground.ground_type == GroundType.MIXED: hex.ground.walkable = True hex.ground.buildable = True elif hex.ground.ground_type == GroundType.WATER_DEEP: hex.ground.walkable = False hex.ground.buildable = False else: hint("GameLogic cannot update map! Unknown ground type") for player in self.player_list: for building in player.buildings: for ass in building.associated_tiles: ass.ground.buildable = False building.tile.ground.buildable = False if building.building_state == BuildingState.UNDER_CONSTRUCTION or \ building.building_state == BuildingState.ACTIVE: building.tile.ground.walkable = False # self.update_fog_of_war(player) for res in self.scenario.resource_list: if res.remaining_amount <= 0: self.del_resource(res) continue res.tile.ground.walkable = False res.tile.ground.buildable = False for p in self.player_list: for a in p.armies: pix_loc = HexMap.offset_to_pixel_coords( a.tile.offset_coordinates) a.set_sprite_pos(pix_loc, self.__camera_pos) # This is a bit of brute force approach and not really necessary. However, animations made it hard # to track when updates are necessary, however the method is still very fast self.__reorder_spritelist(self.z_levels[Z_GAME_OBJ])
def setup(self): """ load the game """ map_data: [[str]] = [] self.game_file_reader.read_map(map_data) map_obj_data: [(str, int, int)] = [] self.game_file_reader.read_map_obj(map_obj_data) # setup hex map self.hex_map = HexMap((len(map_data[0]), len(map_data)), MapStyle.S_V_C) self.income_calc.hex_map = self.hex_map # TODO make sure to set the hex_map everywhere. Ugly! #TODO do this somewhere else # background: List[Drawable] = [] # x_off = 213 # y_off = 165 # for x in range(6): # for y in range(5): # d = Drawable() # d.set_sprite_pos((x_off * x + 100, y_off * y), self.__camera_pos) # self.__set_sprite(d, "ocean") # self.z_levels[0].append(d.sprite) #map for y in range(len(map_data) - 1, -1, -1): for x in range(len(map_data[y])): hex: Hexagon = self.hex_map.get_hex_by_offset((x, y)) ground: Ground = Ground(map_data[y][x]) hex.ground = ground ground.set_sprite_pos(HexMap.offset_to_pixel_coords((x, y)), self.__camera_pos) ground.add_texture(self.texture_store.get_texture("fw")) ground.tex_code = map_data[y][x] self.z_levels[Z_MAP].append(ground.sprite) from src.misc.smooth_map import SmoothMap SmoothMap.smooth_map(self.hex_map) #SmoothMap.adjust_elevation(self.hex_map) # assign textures for hexagon in self.hex_map.map: self.__set_sprite(hexagon.ground, hexagon.ground.tex_code) for map_obj in map_obj_data: hex: Hexagon = self.hex_map.get_hex_by_offset( (map_obj[1], map_obj[2])) if map_obj[0] == "f2": # get str_code with variance import random var = random.randint(0, 6) r: Resource = Resource(hex, ResourceType.FOREST) r.tex_code = "forest_3_var{}".format(var) self.add_resource(r) elif map_obj[0] == "r1" or map_obj[0] == "g1" or map_obj[ 0] == "f1": # TODO if resource r: Resource = Resource( hex, ResourceType.get_type_from_strcode(map_obj[0])) r.tex_code = map_obj[0] self.add_resource(r) player_ids: List[Tuple[int, str]] = [] for player in self.player_list: other_players_ids: List[int] = [] for p in self.player_list: if p != player: other_players_ids.append(p.id) self.ai_interface.launch_AI(player.id, player.ai_str, "AI_" + player.name, other_players_ids) base_hex: Hexagon = self.hex_map.get_hex_by_offset(player.spaw_loc) InitialCondition.set_init_values(player, base_hex, self) # b_type = player.get_initial_building_type() # b: Building = Building(base_hex, b_type, player.id) # self.add_building(b, player) # b.construction_time = 0 # b.set_state_active() # tmp = self.hex_map.get_neighbours_dist(base_hex, b.sight_range) # player.discovered_tiles.update(tmp) if player.player_type is PlayerType.HUMAN: self.has_human_player = True # if not player.is_barbaric: # unit = Unit(player.get_initial_unit_type()) # army = Army(self.hex_map.get_hex_by_offset(player.init_army_loc), player.id) # army.add_unit(unit) # self.add_army(army, player) player_ids.append((player.id, player.colour_code)) self.__reorder_spritelist(self.z_levels[Z_GAME_OBJ]) self.toggle_fog_of_war_lw(self.hex_map.map) from src.ai.performance import PerformanceLogger PerformanceLogger.setup(player_ids) self.logic_state = GameLogicState.READY_FOR_TURN if self.player_list[0].player_type is PlayerType.HUMAN: # in case the first player is the human, we already gather his/her resources self.playNextTurn = True self.nextPlayerButtonPressed = True