def test_init_weapon(self): name = 'short_sword' sprite = 'imgs/dungeon_crawl/item/weapon/short_sword_2_old.png' description = 'A basic little sword, but one that can already prove very useful' price = 500 equipped_sprite = [ 'imgs/dungeon_crawl/player/hand_right/short_sword.png' ] durability = 40 reach = [1] power = 4 kind = 'PHYSICAL' weight = 2 restrictions = [] possible_effects = [] strong_against = [Keyword.LARGE] sword = Weapon(name, sprite, description, price, equipped_sprite, power, kind, weight, durability, reach, restrictions, possible_effects, strong_against) self.assertEqual(name, sword.name) self.assertEqual(description, sword.desc) self.assertEqual('Short Sword', str(sword)) self.assertEqual(price, sword.price) self.assertEqual(price // 2, sword.resell_price) self.assertEqual(durability, sword.durability_max) self.assertEqual(durability, sword.durability) self.assertEqual(reach, sword.reach) self.assertEqual(power, sword.atk) self.assertEqual(DamageKind[kind], sword.attack_kind) self.assertEqual(weight, sword.weight) self.assertEqual(restrictions, sword.restrictions) self.assertEqual(possible_effects, sword.effects) self.assertEqual(strong_against, sword.strong_against)
def test_init_spiritual_weapon(self): name = 'wooden_staff' sprite = 'imgs/dungeon_crawl/item/staff/staff_2.png' description = 'A frail staff, usable to make a range attack with weak spells' price = 400 equipped_sprite = ['imgs/dungeon_crawl/player/hand_right/staff_mage_2.png'] durability = 30 reach = [2] power = 2 kind = 'SPIRITUAL' weight = 2 restrictions = [] possible_effects = [] strong_against = [Keyword.CAVALRY] staff = Weapon(name, sprite, description, price, equipped_sprite, power, kind, weight, durability, reach, restrictions, possible_effects, strong_against) self.assertEqual(name, staff.name) self.assertEqual(description, staff.desc) self.assertEqual('Wooden Staff', str(staff)) self.assertEqual(price, staff.price) self.assertEqual(price // 2, staff.resell_price) self.assertEqual(durability, staff.durability_max) self.assertEqual(durability, staff.durability) self.assertEqual(reach, staff.reach) self.assertEqual(power, staff.atk) self.assertEqual(DamageKind[kind], staff.attack_kind) self.assertEqual(weight, staff.weight) self.assertEqual(restrictions, staff.restrictions) self.assertEqual(possible_effects, staff.effects) self.assertEqual(strong_against, staff.strong_against)
def random_weapon(price=None, durability=None, atk=None, strong_against=None, charge=False): attrs = random_weapon_attributes(price, durability, atk, charge, strong_against) return Weapon(attrs['name'], attrs['sample_img'], attrs['desc'], attrs['cost'], [attrs['sample_img']], attrs['atk'], attrs['attack_kind'], attrs['weight'], attrs['durability'], attrs['reach'], attrs['restrictions'], attrs['effects'], attrs['strong_against'], attrs['charge'])
def parse_item_file(name): # Retrieve data root for item it_tree_root = etree.parse('data/items.xml').getroot().find('.//' + name) sprite = 'imgs/dungeon_crawl/item/' + it_tree_root.find( 'sprite').text.strip() info = it_tree_root.find('info').text.strip() price = it_tree_root.find('price') if price is not None: price = int(price.text.strip()) else: price = 0 category = it_tree_root.find('category').text.strip() if category == 'potion' or category == 'consumable': effects = [] for eff in it_tree_root.findall('.//effect'): effect_name = eff.find('type').text.strip() pow_el = eff.find('power') power = int(pow_el.text.strip()) if pow_el is not None else 0 duration_el = eff.find('duration') duration = int( duration_el.text.strip()) if duration_el is not None else 0 effects.append(Effect(effect_name, power, duration)) item = Potion(name, sprite, info, price, effects) if category == 'potion' else \ Consumable(name, sprite, info, price, effects) elif category == 'armor': body_part = it_tree_root.find('bodypart').text.strip() defense_el = it_tree_root.find('def') defense = int(defense_el.text.strip()) if defense_el is not None else 0 weight = int(it_tree_root.find('weight').text.strip()) eq_sprites = it_tree_root.find('equipped_sprites') if eq_sprites is not None: equipped_sprites = [] for eq_sprite in eq_sprites.findall('sprite'): equipped_sprites.append('imgs/dungeon_crawl/player/' + eq_sprite.text.strip()) else: equipped_sprites = [ 'imgs/dungeon_crawl/player/' + it_tree_root.find('equipped_sprite').text.strip() ] restrictions = load_restrictions(it_tree_root.find('restrictions')) item = Equipment(name, sprite, info, price, equipped_sprites, body_part, defense, 0, 0, weight, restrictions) elif category == 'shield': parry = int(float(it_tree_root.find('parry_rate').text.strip()) * 100) defense_el = it_tree_root.find('def') defense = int(defense_el.text.strip()) if defense_el is not None else 0 fragility = int(it_tree_root.find('fragility').text.strip()) weight = int(it_tree_root.find('weight').text.strip()) equipped_sprite = [ 'imgs/dungeon_crawl/player/hand_left/' + it_tree_root.find('equipped_sprite').text.strip() ] restrictions = load_restrictions(it_tree_root.find('restrictions')) item = Shield(name, sprite, info, price, equipped_sprite, defense, weight, parry, fragility, restrictions) elif category == 'weapon': power = int(it_tree_root.find('power').text.strip()) attack_kind = it_tree_root.find('kind').text.strip() weight = int(it_tree_root.find('weight').text.strip()) fragility = int(it_tree_root.find('fragility').text.strip()) w_range = [ int(reach) for reach in it_tree_root.find('range').text.strip().split(',') ] equipped_sprite = [ 'imgs/dungeon_crawl/player/hand_right/' + it_tree_root.find('equipped_sprite').text.strip() ] restrictions = load_restrictions(it_tree_root.find('restrictions')) effects = it_tree_root.find('effects') possible_effects = [] if effects is not None: possible_effects = [ load_weapon_effect(eff) for eff in effects.findall('effect') ] keywords_el = it_tree_root.find('strong_against/keywords') strong_against = [ Keyword[keyword.upper()] for keyword in keywords_el.text.strip().split(',') ] if keywords_el is not None else [] item = Weapon(name, sprite, info, price, equipped_sprite, power, attack_kind, weight, fragility, w_range, restrictions, possible_effects, strong_against) elif category == 'key': for_chest = it_tree_root.find('open_chest') is not None for_door = it_tree_root.find('open_door') is not None item = Key(name, sprite, info, price, for_chest, for_door) elif category == 'spellbook': spell = it_tree_root.find('effect').text.strip() item = Spellbook(name, sprite, info, price, spell) else: # No special category item = Item(name, sprite, info, price) return item