def load(yaml_file=None, hard_values=None, coord=Vector2.zero(), collision_handler=None, game_state=None, inventory=None): if yaml_file: with open(yaml_file) as stream: values = yaml.safe_load(stream) if not values: raise RuntimeError("File could not be read") else: values = hard_values fighter = None if values["fighter"]: fighter = Fighter.load(hard_values=values["fighter"]) if values["color"] in [a for a in dir(Colors) if "__" not in a]: color = eval("Colors." + values["color"]) else: color = Colors.white ai = None if "ai" in values.keys(): ai = BasicMonsterAI(interest_tag=values["ai"]["interest_tag"]) torch = 0 if "torch" in values.keys(): torch = values["torch"] if "blocks" not in values.keys(): values["blocks"] = True if inventory: inventory = list() return Character(coord=coord, char=values["char"], color=color, name=values["name"], fighter=fighter, ai=ai, blocks=values["blocks"], collision_handler=collision_handler, torch=torch, tags=values["tags"], game_state=game_state, inventory=inventory)
def give_item_for_player(self, item_tag): try: prototype = self._get_random_object_template(item_tag) prototype.coord = Vector2.zero() prototype.z_index = 1 prototype.collision_handler = self.collision_handler self.object_pool.append(prototype) player = self.object_pool.player prototype.pick_up(player) except Exception as e: logger.warning("Could not find any item with tag " + item_tag) return MapObjectsConstructor( object_templates=self.object_templates, max_items_per_room=self.max_items_per_room, max_monster_per_room=self.max_monsters_per_room, game_instance=self.game_instance)
def get_closest_point_of_interest(self): points_of_interest = self.owner.object_pool.find_by_tag( self.interest_tag) closest_point_of_interest = { 'obj': None, 'dist': 1000000, 'coord': Vector2.zero() } for poi in points_of_interest: dist = Vector2.distance(self.owner.coord, poi.coord) if dist < closest_point_of_interest['dist']: closest_point_of_interest['dist'] = dist closest_point_of_interest['obj'] = poi closest_point_of_interest['coord'] = poi.coord return closest_point_of_interest
def load(yaml_file=None, hard_values=None, coord=Vector2.zero(), collision_handler=None): if yaml_file: with open(yaml_file) as stream: values = yaml.safe_load(stream) if not values: raise RuntimeError("File could not be read") else: values = hard_values color = Colors.white if values["color"] in [a for a in dir(Colors) if "__" not in a]: color = eval("Colors." + values["color"]) if values["use_function"] in [ a for a in dir(UseFunctions) if "__" not in a ]: use_function = eval("UseFunctions." + values["use_function"]) else: logger.error("Could not find use_function {}".format( values["use_function"])) use_function = UseFunctions.do_nothing if "blocks" not in values.keys(): values["blocks"] = False if "extra_params" in values.keys( ) and "status_effect" in values["extra_params"].keys(): if values["extra_params"]["status_effect"] in [ a for a in dir(StatusEffects) if "__" not in a ]: values["extra_params"]["status_effect"] = eval( "StatusEffects." + values["extra_params"]["status_effect"]) return Item(coord=coord, char=values["char"], color=color, name=values["name"], blocks=values["blocks"], tags=values["tags"], use_function=use_function, extra_params=values["extra_params"])
def load(yaml_file=None, hard_values=None): if yaml_file: with open(yaml_file) as stream: values = yaml.safe_load(stream) if not values: raise RuntimeError("File could not be read") else: values = hard_values if values["color"] in [a for a in dir(Colors) if "__" not in a]: color = eval("Colors." + values["color"]) else: color = Colors.dark_crimson if "blocks" not in values.keys(): values["blocks"] = False return GameObject(coord=Vector2.zero(), char=values["char"], color=color, name=values["name"], blocks=values["blocks"], tags=values["tags"])
def load(): global game_context with shelve.open('savegame', 'r') as savefile: map = savefile['map'] objects = savefile['object_pool'] player_id = savefile['player-id'] game_msgs = savefile['game_msgs'] game_state = savefile['game_state'] ais = savefile['ais'] fighters = savefile['fighters'] extras = savefile['extras'] game_context = GameContext(next_level=next_level, game_state=ObjectManager.GameState(game_state), real_time=REALTIME, menu=menu) game_context.extras = extras game_context.set_object_pool(ObjectPool.ObjectPool()) game_context.set_map(map) for k, v in objects.items(): v.collision_handler = game_context.collision_handler if k in fighters.keys(): v.fighter = fighters[k] if v.fighter: v.fighter.owner = v if k in ais.keys(): v.ai = ais[k] if v.ai: v.ai.owner = v v.ai.visible_tiles_ref = map.visible_tiles if k == player_id: v.game_state = game_context.game_state game_context.set_player(v, inventory_width=INVENTORY_WIDTH) else: game_context.object_pool.append(v) inventory = game_context.player.get_inventory() for i in inventory: i.context = game_context viewport = ObjectManager.ConsoleBuffer( root_view, object_pool=game_context.object_pool, map=game_context.map, width=SCREEN_WIDTH, height=SCREEN_HEIGHT - PANEL_HEIGHT, origin=Vector2.zero(), target=Vector2.zero(), camera_height=SCREEN_HEIGHT - PANEL_HEIGHT, camera_width=SCREEN_WIDTH, map_height=MAP_SIZE[0], map_width=MAP_SIZE[1], mouse_controller=game_context.mouse_controller) lower_gui_renderer = ObjectManager.ConsoleBuffer( root_view, origin=Vector2(0, SCREEN_HEIGHT - PANEL_HEIGHT), target=Vector2(0, 0), width=SCREEN_WIDTH, height=PANEL_HEIGHT, mouse_controller=game_context.mouse_controller) lower_gui_renderer.add_message_console(MSG_WIDTH, MSG_HEIGHT, MSG_X, MSG_Y) lower_gui_renderer.game_msg = game_msgs lower_gui_renderer.add_bar(1, 1, BAR_WIDTH, 'HP', 'hp', 'max_hp', game_context.player.fighter, Colors.light_red, Colors.darker_red) lower_gui_renderer.add_bar(1, 3, BAR_WIDTH, 'XP', 'xp', 'level_up_xp', game_context.player.fighter, Colors.light_blue, Colors.darker_blue) game_context.game_state.set_state(EGameState.PLAYING) game_context.set_camera(viewport) game_context.lower_gui_renderer = lower_gui_renderer Messenger.broadcast_message("game-controller", {EMessage.MONSTERS_LEVEL_UP: None})
def new_game(): global game_context # Start to setup the object which will handle most of the generally accessed stuff # GameContext will keep setup of the object managers # ObjectPool keeps track of all the objects on the scene # Starting up the collision handler, which manages all the objects collision events # Adding the object pool and the map to the collision handler so they interact game_context = GameContext(next_level=next_level, game_state=ObjectManager.GameState( EGameState.LOADING), real_time=REALTIME, menu=menu) game_context.set_object_pool(ObjectPool.ObjectPool()) # A map constructor, that randomly create rooms with (not yet implemented) many different strategies # Legacy mode makes the map be drawn using chars instead of colored blocks game_context.set_map(make_map(max_rooms=1)) # Before we start the map objects constructor we load the level data that is being hold on a file # With all the information necessary to build the monsters from the template # Map objects constructor is a special factory that randomly populates the map with object templates # and does deal with weighted distributions, It makes everything in place, by reference # Creation of the player player = Character.load(yaml_file=PLAYER_DATA, coord=game_context.map.get_rooms()[0].center(), collision_handler=game_context.collision_handler, inventory=list(), game_state=game_context.game_state) game_context.set_player(player, inventory_width=INVENTORY_WIDTH) game_context.add_extra("dungeon_level", 1) MapObjectsConstructor(game_instance=game_context).load_object_templates( LEVEL_DATA).give_item_for_player("ICFBS01").populate_map() viewport = ObjectManager.ConsoleBuffer( root_view, object_pool=game_context.object_pool, map=game_context.map, width=SCREEN_WIDTH, height=SCREEN_HEIGHT - PANEL_HEIGHT, origin=Vector2.zero(), target=Vector2.zero(), camera_height=SCREEN_HEIGHT - PANEL_HEIGHT, camera_width=SCREEN_WIDTH, map_height=MAP_SIZE[0], map_width=MAP_SIZE[1], mouse_controller=game_context.mouse_controller) lower_gui_renderer = ObjectManager.ConsoleBuffer( root_view, origin=Vector2(0, SCREEN_HEIGHT - PANEL_HEIGHT), target=Vector2(0, 0), width=SCREEN_WIDTH, height=PANEL_HEIGHT, mouse_controller=game_context.mouse_controller) lower_gui_renderer.add_message_console(MSG_WIDTH, MSG_HEIGHT, MSG_X, MSG_Y) lower_gui_renderer.add_bar(1, 1, BAR_WIDTH, 'HP', 'hp', 'max_hp', player.fighter, Colors.light_red, Colors.darker_red) lower_gui_renderer.add_bar(1, 3, BAR_WIDTH, 'XP', 'xp', 'level_up_xp', game_context.player.fighter, Colors.light_blue, Colors.darker_blue) lower_gui_renderer.add_extra(1, 6, 'DUNGEON LEVEL', game_context.get_extra("dungeon_level"), Colors.yellow, None) # a warm welcoming message! Messenger.send_message( 'Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', Colors.red) game_context.game_state.set_state(EGameState.PLAYING) game_context.set_camera(viewport) game_context.lower_gui_renderer = lower_gui_renderer Messenger.broadcast_message("game-controller", {EMessage.MONSTERS_LEVEL_UP: None}) return game_context
def next_level(): global game_context # advance to the next level Messenger.send_message( 'You take a moment to rest, and recover your strength.', Colors.light_violet) player = game_context.player player.fighter.heal_percent(0.5) # heal the player by 50% game_context.object_pool.clear_object_pool(keep_player=True) game_context.set_map( make_map(max_rooms=game_context.get_extra("dungeon_level", default=1))) MapObjectsConstructor(game_instance=game_context).load_object_templates( LEVEL_DATA).populate_map() player.coord = game_context.map.get_rooms()[0].center() Messenger.send_message( 'After a rare moment of peace, you descend deeper into the heart of the dungeon...', Colors.red) viewport = ObjectManager.ConsoleBuffer( root_view, object_pool=game_context.object_pool, map=game_context.map, width=SCREEN_WIDTH, height=SCREEN_HEIGHT - PANEL_HEIGHT, origin=Vector2.zero(), target=Vector2.zero(), camera_height=SCREEN_HEIGHT - PANEL_HEIGHT, camera_width=SCREEN_WIDTH, map_height=MAP_SIZE[0], map_width=MAP_SIZE[1], mouse_controller=game_context.mouse_controller) lower_gui_renderer = ObjectManager.ConsoleBuffer( root_view, origin=Vector2(0, SCREEN_HEIGHT - PANEL_HEIGHT), target=Vector2(0, 0), width=SCREEN_WIDTH, height=PANEL_HEIGHT, mouse_controller=game_context.mouse_controller) lower_gui_renderer.add_message_console(MSG_WIDTH, MSG_HEIGHT, MSG_X, MSG_Y) lower_gui_renderer.add_bar(1, 1, BAR_WIDTH, 'HP', 'hp', 'max_hp', player.fighter, Colors.light_red, Colors.darker_red) lower_gui_renderer.add_bar(1, 3, BAR_WIDTH, 'XP', 'xp', 'level_up_xp', game_context.player.fighter, Colors.light_blue, Colors.darker_blue) lower_gui_renderer.add_extra(1, 6, 'DUNGEON LEVEL', game_context.get_extra("dungeon_level"), Colors.yellow, None) game_context.set_camera(viewport) game_context.lower_gui_renderer = lower_gui_renderer Messenger.broadcast_message("game-controller", {EMessage.MONSTERS_LEVEL_UP: None})
def load(yaml_file=None, hard_values=None, coord=Vector2.zero(), collision_handler=None): if yaml_file: with open(yaml_file) as stream: values = yaml.safe_load(stream) if not values: raise RuntimeError("File could not be read") else: values = hard_values color = Colors.white if "color" in values.keys() and values["color"] in [ a for a in dir(Colors) if "__" not in a ]: color = eval("Colors." + values["color"]) if "use_function" not in values.keys(): use_function = None else: if values["use_function"] in [ a for a in dir(UseFunctions) if "__" not in a ]: use_function = eval("UseFunctions." + values["use_function"]) else: logger.error("Could not find use_function {}".format( values["use_function"])) use_function = UseFunctions.do_nothing blocks = values["blocks"] if "blocks" in values.keys() else False charges = values["charges"] if "charges" in values.keys() else 0 slot = [ slot for slot in EEquipmentSlot if slot.value == values["slot"] ] power = values["power"] if "power" in values.keys() else 0 defense = values["defense"] if "defense" in values.keys() else 0 max_hp = values["max_hp"] if "max_hp" in values.keys() else 0 if "extra_params" in values.keys(): if "status_effect" in values["extra_params"].keys(): if values["extra_params"]["status_effect"] in [ a for a in dir(StatusEffects) if "__" not in a ]: values["extra_params"]["status_effect"] = eval( "StatusEffects." + values["extra_params"]["status_effect"]) else: values["extra_params"] = None return Equipment(coord=coord, char=values["char"], color=color, name=values["name"], blocks=blocks, tags=values["tags"], use_function=use_function, slot=slot[0], charges=charges, extra_params=values["extra_params"], power_bonus=power, defense_bonus=defense, max_hp_bonus=max_hp)