Mercurial > parpg-source
comparison gamemodel.py @ 75:9e03f7816061
(Re)added setting of inventory and equipment in the object files.
author | KarstenBock@gmx.net |
---|---|
date | Thu, 22 Sep 2011 16:18:07 +0200 |
parents | e856b604b650 |
children | 4cf150131139 |
comparison
equal
deleted
inserted
replaced
74:c9818290bbe7 | 75:9e03f7816061 |
---|---|
29 from common.utils import locateFiles | 29 from common.utils import locateFiles |
30 from common.utils import parseBool | 30 from common.utils import parseBool |
31 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError | 31 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError |
32 from parpg.entities import createEntity | 32 from parpg.entities import createEntity |
33 from parpg import behaviours | 33 from parpg import behaviours |
34 from parpg.components import fifeagent | 34 from parpg.components import fifeagent, container, equip |
35 | 35 |
36 try: | 36 try: |
37 import xml.etree.cElementTree as ElementTree | 37 import xml.etree.cElementTree as ElementTree |
38 except ImportError: | 38 except ImportError: |
39 import xml.etree.ElementTree as ElementTree | 39 import xml.etree.ElementTree as ElementTree |
131 raise ValueError( | 131 raise ValueError( |
132 "Number exceeds MAX_ID_NUMBER:" + str(self.MAX_ID_NUMBER)) | 132 "Number exceeds MAX_ID_NUMBER:" + str(self.MAX_ID_NUMBER)) |
133 | 133 |
134 ID = ID + "_" + str(id_number) | 134 ID = ID + "_" + str(id_number) |
135 return ID | 135 return ID |
136 | |
137 def createContainerItems(self, container_objs): | |
138 """Create the items of a container from a dictionary | |
139 @param container_objs: Dictionary containing the items | |
140 @type container_objs: dict""" | |
141 items = [] | |
142 for container_obj in container_objs: | |
143 items.append(self.createContainerObject(container_obj)) | |
144 | |
145 return items | |
146 | |
147 def createContainerObject(self, attributes): | |
148 """Create an object that can be stored in | |
149 an container and return it | |
150 @param attributes: Dictionary of all object attributes | |
151 @type attributes: Dictionary | |
152 @return: The created object """ | |
153 # create the extra data | |
154 extra = {} | |
155 extra['controller'] = self | |
156 | |
157 info = {} | |
158 info.update(attributes) | |
159 info.update(extra) | |
160 ID = info.pop("id") if info.has_key("id") else info.pop("ID") | |
161 if not info.has_key("item_type"): | |
162 info["item_type"] = info["type"] | |
163 ID = self.createUniqueID(ID) | |
164 if info.has_key("attributes"): | |
165 attributes = info["attributes"] | |
166 if "Container" in attributes: | |
167 info["actions"]["Open"] = "" | |
168 if info.has_key("Items"): | |
169 inventory_objs = info["Items"] | |
170 info["items"] = self.createContainerItems(inventory_objs) | |
171 | |
172 new_item = CarryableContainer(ID = ID, **info) | |
173 else: | |
174 new_item = CarryableItem(ID = ID, **info) | |
175 else: | |
176 new_item = CarryableItem(ID = ID, **info) | |
177 self.game_state.addObject(None, new_item) | |
178 return new_item | |
179 | |
180 def createInventoryObject(self, container, attributes): | |
181 """Create an inventory object and place it into a container | |
182 @type container: base.Container | |
183 @param container: Container where the item is on | |
184 @type attributes: Dictionary | |
185 @param attributes: Dictionary of all object attributes | |
186 @return: None""" | |
187 index = attributes.pop("index") if attributes.has_key("index") else None | |
188 slot = attributes.pop("slot") if attributes.has_key("slot") else None | |
189 obj = self.createContainerObject(attributes) | |
190 #obj = createObject(attributes, extra) | |
191 if slot: | |
192 container.moveItemToSlot(obj, slot) | |
193 else: | |
194 container.placeItem(obj, index) | |
195 | 136 |
196 def deleteObject(self, object_id): | 137 def deleteObject(self, object_id): |
197 """Removes an object from the game | 138 """Removes an object from the game |
198 @param object_id: ID of the object | 139 @param object_id: ID of the object |
199 @type object_id: str """ | 140 @type object_id: str """ |
489 entity_data["fifeagent"]["behaviour"] = getattr(behaviours, entity_data["fifeagent"]["behaviour"])() | 430 entity_data["fifeagent"]["behaviour"] = getattr(behaviours, entity_data["fifeagent"]["behaviour"])() |
490 else: | 431 else: |
491 entity_data["fifeagent"]["behaviour"] = behaviours.Base() | 432 entity_data["fifeagent"]["behaviour"] = behaviours.Base() |
492 if self.dialogues.has_key(inst_id): | 433 if self.dialogues.has_key(inst_id): |
493 entity_data["dialogue"] = {} | 434 entity_data["dialogue"] = {} |
494 entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id] | 435 entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id] |
495 | 436 |
496 self.createMapObject(self.active_map.agent_layer, entity_data, world) | 437 obj = self.createMapObject(self.active_map.agent_layer, entity_data, world) |
497 | 438 |
439 if agent.has_key("Inventory"): | |
440 inv = agent["Inventory"] | |
441 slots = inv["Slots"] | |
442 obj.container.children = list() | |
443 for x in xrange(slots): | |
444 obj.container.children.append(None) | |
445 items = inv["Items"] if inv.has_key("Items") else list() | |
446 for data in items: | |
447 item_data = {} | |
448 item_data = self.checkAttributes(item_data, data["type"]) | |
449 if item_data.has_key("containable"): | |
450 item = createEntity(item_data, world, None) | |
451 self.game_state.addObject(self.createUniqueID(data["ID"]), None, item) | |
452 container.put_item(obj.container, item.containable) | |
453 else: | |
454 raise Exception("Item %s is not containable." % data["type"]) | |
455 | |
456 if agent.has_key("Equipment"): | |
457 for slot, data in agent["Equipment"].iteritems(): | |
458 item_data = {} | |
459 item_data = self.checkAttributes(item_data, data["type"]) | |
460 if item_data.has_key("containable") and item_data.has_key("equipable"): | |
461 if not item_data["equipable"].has_key("image"): | |
462 item_data["equipable"]["image"]=item_data["containable"]["image"] | |
463 if not item_data["containable"].has_key("image"): | |
464 item_data["containable"]["image"]=item_data["equipable"]["image"] | |
465 item = createEntity(item_data, world, None) | |
466 self.game_state.addObject(self.createUniqueID(data["ID"]), None, item) | |
467 equip.equip(obj.equip, item.equipable, slot) | |
468 else: | |
469 raise Exception("Item %s is not containable or equipable." % data["type"]) | |
470 | |
498 def placeAgents(self, world): | 471 def placeAgents(self, world): |
499 """Places the current maps agents """ | 472 """Places the current maps agents """ |
500 if not self.active_map: | 473 if not self.active_map: |
501 return | 474 return |
502 agents = self.getAgentsOfMap(self.game_state.current_map_name) | 475 agents = self.getAgentsOfMap(self.game_state.current_map_name) |
563 @param layer: FIFE layer object exists in | 536 @param layer: FIFE layer object exists in |
564 @type attributes: Dictionary | 537 @type attributes: Dictionary |
565 @param attributes: Dictionary of all object attributes | 538 @param attributes: Dictionary of all object attributes |
566 @type instance: fife.Instance | 539 @type instance: fife.Instance |
567 @param instance: FIFE instance corresponding to the object | 540 @param instance: FIFE instance corresponding to the object |
568 @return: None""" | 541 @return: The created object""" |
569 # create the extra data | 542 # create the extra data |
570 extra = {} | 543 extra = {} |
571 if layer is not None: | 544 if layer is not None: |
572 extra['fifeagent'] = {} | 545 extra['fifeagent'] = {} |
573 extra['fifeagent']['layer'] = layer | 546 extra['fifeagent']['layer'] = layer |
574 | 547 |
575 obj = createEntity(attributes, world, extra) | 548 obj = createEntity(attributes, world, extra) |
576 if obj: | 549 if obj: |
577 self.addObject(layer, obj) | 550 self.addObject(layer, obj) |
551 return obj | |
578 | 552 |
579 def addPC(self, layer, player_char): | 553 def addPC(self, layer, player_char): |
580 """Add the PlayerCharacter to the map | 554 """Add the PlayerCharacter to the map |
581 @type layer: fife.Layer | 555 @type layer: fife.Layer |
582 @param layer: FIFE layer object exists in | 556 @param layer: FIFE layer object exists in |
604 @return: None""" | 578 @return: None""" |
605 ref = self.game_state.getObjectById(obj.fifeagent.identifier, | 579 ref = self.game_state.getObjectById(obj.fifeagent.identifier, |
606 self.game_state.current_map_name) | 580 self.game_state.current_map_name) |
607 if ref is None: | 581 if ref is None: |
608 # no, add it to the game state | 582 # no, add it to the game state |
609 self.game_state.addObject(self.game_state.current_map_name, obj) | 583 self.game_state.addObject(obj.fifeagent.identifier, self.game_state.current_map_name, obj) |
610 else: | 584 else: |
611 # yes, use the current game state data | 585 # yes, use the current game state data |
612 obj.fifeagent.pos.X = ref.X | 586 obj.fifeagent.pos.X = ref.X |
613 obj.fifeagent.pos.Y = ref.Y | 587 obj.fifeagent.pos.Y = ref.Y |
614 obj.fifeagent.gfx = ref.gfx | 588 obj.fifeagent.gfx = ref.gfx |