def hydrate(data): """Populates the level with data from the file""" contents = [] if data == None: return contents for key, value in data.items(): content_type = "item" keys = value.keys() description = None target = None damage = None if "x" not in keys or "y" not in keys: pass else: if "type" in keys: content_type = value["type"] if "description" in keys: description = value["description"] if "target" in keys: target = value["target"] if "damage" in keys: damage = value["damage"] if content_type == "creature": locatable = Cockroach(key, description) locatable.target = target # if target: # for itm in contents: # if itm.name == target: # locatable.set_target(itm) # else: # #What should go here? # pass else: locatable = Item(key, description) if content_type == "weapon": locatable = Weapon(damage) locatable.name = key locatable.description = description locatable.place((value["x"], value["y"])) if "display" in keys: locatable.set_display(value["display"]) contents.append(locatable) index = None for item in contents: if isinstance(item, Monster): index = contents.index(item) if index is not None: creature = contents.pop(index) for itm in contents: if itm.name == creature.target: creature.set_target(itm) contents.append(creature) return contents
def hydrate(data, size): random_place = [] """Populates the level with data from the file""" contents = [] if data == None: return contents for key, value in data.items(): content_type = "item" keys = value.keys() description = None target = None damage = None if "x" not in keys or "y" not in keys: pass else: if "type" in keys: content_type = value["type"] if "description" in keys: description = value["description"] if "target" in keys: target = value["target"] if "damage" in keys: damage = value["damage"] if content_type == "creature": if value["display"] == "蟑": locatable = Cockroach(key, description) elif value["display"] == "狗": locatable = Dog(key, description) elif value["display"] == "狼": locatable = Wolf(key, description) else: locatable = Dog(key, description) locatable.target = target # if target: # for itm in contents: # if itm.name == target: # locatable.set_target(itm) # else: # #What should go here? # pass else: locatable = Item(key, description) if content_type == "weapon": if value["display"] == "剑": locatable = Excalibur(damage) locatable.name = key locatable.description = description post_x = random.randint(0, size - 1) post_y = random.randint(0, size - 1) post_data = str(post_x) + '*' + str(post_y) while post_data in random_place: post_x = random.randint(0, size - 1) post_y = random.randint(0, size - 1) post_data = str(post_x) + '*' + str(post_y) random_place.append(post_data) locatable.place((post_x, post_y)) if "display" in keys: locatable.set_display(value["display"]) contents.append(locatable) index = [] for item in contents: if isinstance(item, Monster): index.append(contents.index(item)) for i in index: creature = contents.pop(i) for itm in contents: if itm.name == creature.target: creature.set_target(itm) contents.insert(i, creature) return contents