def from_json(json_data: Dict) -> "Entity": name = json_data["name"] entity_type = EntityType(json_data["entity_type"]) x = json_data["x"] y = json_data["y"] glyph = json_data["glyph"] fg = json_data["fg"] bg = json_data["bg"] blocks = json_data["blocks"] fighter = Fighter.from_json(json_data["fighter"]) ai = get_ai(json_data["ai"]) item = Item.from_json(json_data["item"]) # This seems to be necessary to avoid circular imports, since inventories are full of Entities: inv_data = json_data["inventory"] if inv_data is not None: inv_capacity_data = inv_data["capacity"] inv_items_data = inv_data["items"] inventory = Inventory(capacity=inv_capacity_data) inventory.items = [ Entity.from_json(item_data) for item_data in inv_items_data ] else: inventory = None stairs = Stairs.from_json(json_data["stairs"]) level = Level.from_json(json_data["level"]) # This is also necessary to avoid circular imports: equipment_data = json_data["equipment"] if equipment_data is not None: main_hand_data = equipment_data["main_hand"] off_hand_data = equipment_data["off_hand"] main_hand = Entity.from_json( main_hand_data) if main_hand_data is not None else None off_hand = Entity.from_json( off_hand_data) if off_hand_data is not None else None equipment = Equipment(main_hand, off_hand) else: equipment = None equippable = Equippable.from_json(json_data["equippable"]) buffs = [Buff.from_json(buff_data) for buff_data in json_data["buffs"]] entity = Entity(name, entity_type, x, y, glyph, fg, bg, blocks, fighter, ai, item, inventory, stairs, level, equipment, equippable) for buff in buffs: buff.owner = entity entity.buffs = buffs return entity
def from_json(cls, json_data): x = json_data.get('x') y = json_data.get('y') char = json_data.get('char') color = json_data.get('color') name = json_data.get('name') blocks = json_data.get('blocks') render_order_value = json_data.get('render_order') ai_json = json_data.get('ai') equipment_json = json_data.get('equipment') equippable_json = json_data.get('equippable') fighter_json = json_data.get('fighter') inventory_json = json_data.get('inventory') item_json = json_data.get('item') if equipment_json: equipment = Equipment.from_json(json_data=equipment_json) else: equipment = None if equippable_json: equippable = Equippable.from_json(json_data=equippable_json) else: equippable = None if fighter_json: fighter = Fighter.from_json(json_data=fighter_json) else: fighter = None if inventory_json: inventory = Inventory.from_json(json_data=inventory_json) else: inventory = None if item_json: item = Item.from_json(json_data=item_json) else: item = None entity = cls(x=x, y=y, char=char, color=color, name=name, blocks=blocks, render_order=RenderOrder(render_order_value), ai=None, equipment=equipment, equippable=equippable, fighter=fighter, inventory=inventory, item=item) if ai_json: name = ai_json.get('name') if name == BasicMonster.__name__: ai = BasicMonster.from_json(json_data=ai_json, owner=entity) elif name == ConfusedMonster.__name__: ai = ConfusedMonster.from_json(json_data=ai_json['ai_data'], owner=entity) else: ai = None entity.ai = ai return entity
def from_json(json_data): x = json_data.get('x') y = json_data.get('y') char = json_data.get('char') color_o = json_data.get('color') color = libtcod.Color(color_o[0], color_o[1], color_o[2]) name = json_data.get('name') blocks = json_data.get('blocks', False) render_order = RenderOrder(json_data.get('render_order')) fighter_json = json_data.get('fighter') ai_json = json_data.get('ai') item_json = json_data.get('item') inventory_json = json_data.get('inventory') stairs_json = json_data.get('stairs') level_json = json_data.get('level') equipment_json = json_data.get('equipment') equippable_json = json_data.get('equippable') uID_data = json_data.get('uID') quickuse_json = json_data.get('quick_use') entity = Entity(x, y, char, color, name, blocks, render_order, uID=uID_data) if fighter_json: entity.fighter = Fighter.from_json(fighter_json) entity.fighter.owner = entity if ai_json: name = ai_json.get('name') if name == BasicMonster.__name__: ai = BasicMonster.from_json() elif name == ConfusedMonster.__name__: ai = ConfusedMonster.from_json(ai_json, entity) else: ai = None if ai: entity.ai = ai entity.ai.owner = entity if item_json: entity.item = Item.from_json(item_json) entity.item.owner = entity if inventory_json: entity.inventory = Inventory.from_json(inventory_json) entity.inventory.owner = entity if stairs_json: entity.stairs = Stairs.from_json(stairs_json) entity.stairs.owner = entity if level_json: entity.level = Level.from_json(level_json) entity.level.owner = entity if equipment_json: entity.equipment = Equipment.from_json(equipment_json) entity.equipment.owner = entity if equippable_json: entity.equippable = Equippable.from_json(equippable_json) entity.equippable.owner = entity if quickuse_json: entity.quick_use = Quickuse.from_json(quickuse_json) entity.quick_use.owner = entity return entity
def from_json(cls, json_data: dict): x = json_data.get('x') y = json_data.get('y') char = json_data.get('char') color = json_data.get('color') name = json_data.get('name') blocks = json_data.get('blocks', False) render_order = RenderOrder(json_data.get('render_order')) fighter_json = json_data.get('fighter') ai_json = json_data.get('ai') item_json = json_data.get('item') inventory_json = json_data.get('inventory') stairs_json = json_data.get('stairs') level_json = json_data.get('level') equipped_main_index_json = json_data.get('equipped_main_index') equipped_off_index_json = json_data.get('equipped_off_index') equippable_json = json_data.get('equippable') entity = cls(x, y, char, color, name, blocks, render_order) if fighter_json: entity.fighter = Fighter.from_json(fighter_json) entity.fighter.owner = entity if ai_json: name = ai_json.get('name') ai = None if name == BasicMonster.__name__: ai = BasicMonster.from_json() elif name == ConfusedMonster.__name__: ai = ConfusedMonster.from_json(ai_json, entity) if ai: entity.ai = ai entity.ai.owner = entity if item_json: entity.item = Item.from_json(item_json) entity.item.owner = entity if inventory_json: entity.inventory = Inventory.from_json(inventory_json) entity.inventory.owner = entity if stairs_json: entity.stairs = Stairs.from_json(stairs_json) entity.stairs.owner = entity if level_json: entity.level = Level.from_json(level_json) entity.level.owner = entity equipped_main = None equipped_off = None if equipped_main_index_json is not None: equipped_main = entity.inventory.items[equipped_main_index_json] if equipped_off_index_json is not None: equipped_off = entity.inventory.items[equipped_off_index_json] entity.equipment = Equipment(equipped_main, equipped_off) entity.equipment.owner = entity if equippable_json: entity.equippable = Equippable.from_json(equippable_json) entity.equippable.owner = entity return entity