# HG changeset patch # User KarstenBock@gmx.net # Date 1316701087 -7200 # Node ID 7829eb185d6f65476ffc686eda6086353fd061ec # Parent de741e83a422616494c55b8888daf05d2d7ef51c (Re)added setting of inventory and equipment in the object files. diff -r de741e83a422 -r 7829eb185d6f src/parpg/components/equip.py --- a/src/parpg/components/equip.py Thu Sep 22 14:39:00 2011 +0200 +++ b/src/parpg/components/equip.py Thu Sep 22 16:18:07 2011 +0200 @@ -54,7 +54,7 @@ raise AlreadyEquippedError if slot in equipable.possible_slots: try: - old_item = getattr(wearer, slot) + old_item = getattr(wearer, slot) if hasattr(wearer, slot) else None setattr(wearer, slot, equipable) equipable.in_slot = slot equipable.wearer = wearer diff -r de741e83a422 -r 7829eb185d6f src/parpg/components/equipable.py --- a/src/parpg/components/equipable.py Thu Sep 22 14:39:00 2011 +0200 +++ b/src/parpg/components/equipable.py Thu Sep 22 16:18:07 2011 +0200 @@ -19,4 +19,4 @@ """ def __init__(self): - Component.__init__(self, possible_slots=list, wearer=object, in_slot=str) \ No newline at end of file + Component.__init__(self, image=str, possible_slots=list, wearer=object, in_slot=str) \ No newline at end of file diff -r de741e83a422 -r 7829eb185d6f src/parpg/gamemodel.py --- a/src/parpg/gamemodel.py Thu Sep 22 14:39:00 2011 +0200 +++ b/src/parpg/gamemodel.py Thu Sep 22 16:18:07 2011 +0200 @@ -31,7 +31,7 @@ from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError from parpg.entities import createEntity from parpg import behaviours -from parpg.components import fifeagent +from parpg.components import fifeagent, container, equip try: import xml.etree.cElementTree as ElementTree @@ -133,65 +133,6 @@ ID = ID + "_" + str(id_number) return ID - - def createContainerItems(self, container_objs): - """Create the items of a container from a dictionary - @param container_objs: Dictionary containing the items - @type container_objs: dict""" - items = [] - for container_obj in container_objs: - items.append(self.createContainerObject(container_obj)) - - return items - - def createContainerObject(self, attributes): - """Create an object that can be stored in - an container and return it - @param attributes: Dictionary of all object attributes - @type attributes: Dictionary - @return: The created object """ - # create the extra data - extra = {} - extra['controller'] = self - - info = {} - info.update(attributes) - info.update(extra) - ID = info.pop("id") if info.has_key("id") else info.pop("ID") - if not info.has_key("item_type"): - info["item_type"] = info["type"] - ID = self.createUniqueID(ID) - if info.has_key("attributes"): - attributes = info["attributes"] - if "Container" in attributes: - info["actions"]["Open"] = "" - if info.has_key("Items"): - inventory_objs = info["Items"] - info["items"] = self.createContainerItems(inventory_objs) - - new_item = CarryableContainer(ID = ID, **info) - else: - new_item = CarryableItem(ID = ID, **info) - else: - new_item = CarryableItem(ID = ID, **info) - self.game_state.addObject(None, new_item) - return new_item - - def createInventoryObject(self, container, attributes): - """Create an inventory object and place it into a container - @type container: base.Container - @param container: Container where the item is on - @type attributes: Dictionary - @param attributes: Dictionary of all object attributes - @return: None""" - index = attributes.pop("index") if attributes.has_key("index") else None - slot = attributes.pop("slot") if attributes.has_key("slot") else None - obj = self.createContainerObject(attributes) - #obj = createObject(attributes, extra) - if slot: - container.moveItemToSlot(obj, slot) - else: - container.placeItem(obj, index) def deleteObject(self, object_id): """Removes an object from the game @@ -491,10 +432,42 @@ entity_data["fifeagent"]["behaviour"] = behaviours.Base() if self.dialogues.has_key(inst_id): entity_data["dialogue"] = {} - entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id] - - self.createMapObject(self.active_map.agent_layer, entity_data, world) - + entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id] + + obj = self.createMapObject(self.active_map.agent_layer, entity_data, world) + + if agent.has_key("Inventory"): + inv = agent["Inventory"] + slots = inv["Slots"] + obj.container.children = list() + for x in xrange(slots): + obj.container.children.append(None) + items = inv["Items"] if inv.has_key("Items") else list() + for data in items: + item_data = {} + item_data = self.checkAttributes(item_data, data["type"]) + if item_data.has_key("containable"): + item = createEntity(item_data, world, None) + self.game_state.addObject(self.createUniqueID(data["ID"]), None, item) + container.put_item(obj.container, item.containable) + else: + raise Exception("Item %s is not containable." % data["type"]) + + if agent.has_key("Equipment"): + for slot, data in agent["Equipment"].iteritems(): + item_data = {} + item_data = self.checkAttributes(item_data, data["type"]) + if item_data.has_key("containable") and item_data.has_key("equipable"): + if not item_data["equipable"].has_key("image"): + item_data["equipable"]["image"]=item_data["containable"]["image"] + if not item_data["containable"].has_key("image"): + item_data["containable"]["image"]=item_data["equipable"]["image"] + item = createEntity(item_data, world, None) + self.game_state.addObject(self.createUniqueID(data["ID"]), None, item) + equip.equip(obj.equip, item.equipable, slot) + else: + raise Exception("Item %s is not containable or equipable." % data["type"]) + def placeAgents(self, world): """Places the current maps agents """ if not self.active_map: @@ -565,7 +538,7 @@ @param attributes: Dictionary of all object attributes @type instance: fife.Instance @param instance: FIFE instance corresponding to the object - @return: None""" + @return: The created object""" # create the extra data extra = {} if layer is not None: @@ -574,7 +547,8 @@ obj = createEntity(attributes, world, extra) if obj: - self.addObject(layer, obj) + self.addObject(layer, obj) + return obj def addPC(self, layer, player_char): """Add the PlayerCharacter to the map @@ -606,7 +580,7 @@ self.game_state.current_map_name) if ref is None: # no, add it to the game state - self.game_state.addObject(self.game_state.current_map_name, obj) + self.game_state.addObject(obj.fifeagent.identifier, self.game_state.current_map_name, obj) else: # yes, use the current game state data obj.fifeagent.pos.X = ref.X diff -r de741e83a422 -r 7829eb185d6f src/parpg/gamestate.py --- a/src/parpg/gamestate.py Thu Sep 22 14:39:00 2011 +0200 +++ b/src/parpg/gamestate.py Thu Sep 22 16:18:07 2011 +0200 @@ -22,6 +22,7 @@ self.quest_engine = QuestEngine(quests_dir) self.quest_engine.readQuests() self.objects = {} + self.objects[None] = {} self.object_ids = {} self.current_map_name = None self.maps = {} diff -r de741e83a422 -r 7829eb185d6f tests/test_equip.py --- a/tests/test_equip.py Thu Sep 22 14:39:00 2011 +0200 +++ b/tests/test_equip.py Thu Sep 22 16:18:07 2011 +0200 @@ -37,12 +37,6 @@ unittest.TestCase.setUp(self) self.world = self.GameWorld() self.wearer = Entity(self.world) - self.wearer.equip.head = None - self.wearer.equip.body = None - self.wearer.equip.leg = None - self.wearer.equip.feet = None - self.wearer.equip.l_arm = None - self.wearer.equip.r_arm = None self.arms_item = Entity(self.world) self.arms_item.equipable.possible_slots = ["l_arm", "r_arm"] self.l_arm_item = Entity(self.world)