comparison gamemodel.py @ 132:e2ccd64f3a86

Split code off from createInventoryItems to createItemByID and createItemByType. Entities with a containable component will now get the "item_type" field set to the Template, if that field is not present.
author KarstenBock@gmx.net
date Sat, 08 Oct 2011 14:18:16 +0200
parents d1c2d316cc25
children 53faff63037f
comparison
equal deleted inserted replaced
131:d1c2d316cc25 132:e2ccd64f3a86
369 def createAgent(self, agent, inst_id, world): 369 def createAgent(self, agent, inst_id, world):
370 if self.game_state.hasObject(inst_id): 370 if self.game_state.hasObject(inst_id):
371 return None 371 return None
372 entity_data = deepcopy(agent["Entity"]) 372 entity_data = deepcopy(agent["Entity"])
373 entity_data["fifeagent"] = {} 373 entity_data["fifeagent"] = {}
374 template = None
374 if agent.has_key("Template"): 375 if agent.has_key("Template"):
375 entity_data = self.checkAttributes(entity_data, agent["Template"]) 376 template = agent["Template"]
377 entity_data = self.checkAttributes(entity_data, template)
376 object_id = (entity_data["graphics"]["gfx"] 378 object_id = (entity_data["graphics"]["gfx"]
377 if entity_data.has_key("graphics") and 379 if entity_data.has_key("graphics") and
378 entity_data["graphics"].has_key("gfx") 380 entity_data["graphics"].has_key("gfx")
379 else self.GENERIC_ITEM_GFX 381 else self.GENERIC_ITEM_GFX
380 ) 382 )
416 entity_data["behaviour"]["behaviour_type"])() 418 entity_data["behaviour"]["behaviour_type"])()
417 else: 419 else:
418 entity_data["fifeagent"]["behaviour"] = behaviours.Base() 420 entity_data["fifeagent"]["behaviour"] = behaviours.Base()
419 if self.dialogues.has_key(inst_id): 421 if self.dialogues.has_key(inst_id):
420 entity_data["dialogue"] = {} 422 entity_data["dialogue"] = {}
421 entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id] 423 entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]
424 if (entity_data.has_key("containable") and not
425 entity_data["containable"].has_key("item_type")
426 ):
427 entity_data["containable"]["item_type"] = template
428
422 429
423 obj = self.createMapObject(self.active_map.agent_layer, entity_data, inst_id, world) 430 obj = self.createMapObject(self.active_map.agent_layer, entity_data, inst_id, world)
424 431
425 if agent.has_key("Inventory"): 432 if agent.has_key("Inventory"):
426 inv = agent["Inventory"] 433 inv = agent["Inventory"]
427 self.create_inventory_items(inv, obj, world) 434 self.createInventoryItems(inv, obj, world)
428 435
429 if agent.has_key("Equipment"): 436 if agent.has_key("Equipment"):
430 for slot, data in agent["Equipment"].iteritems(): 437 for slot, data in agent["Equipment"].iteritems():
431 item = None 438 item = None
432 if data.has_key("type"): 439 if data.has_key("type"):
433 item_type = data["type"] 440 item_type = data["type"]
434 item_data = {} 441 item_data = {}
435 item_data = self.checkAttributes(item_data, item_type) 442 item_data = self.checkAttributes(item_data, item_type)
436 if item_data.has_key("containable") and item_data.has_key("equipable"): 443 if item_data.has_key("containable") and item_data.has_key("equipable"):
437 item = self.create_item( 444 item = self.createItem(
438 self.createUniqueID(data["ID"]), 445 self.createUniqueID(data["ID"]),
439 item_data, world, item_type) 446 item_data, world, item_type)
440 else: 447 else:
441 raise Exception("Item %s is not containable or equipable." % item_type) 448 raise Exception("Item %s is not containable or equipable." % item_type)
442 else: 449 else:
444 if self.game_state.hasObject(identifier): 451 if self.game_state.hasObject(identifier):
445 item = self.game_state.getObjectById(identifier) 452 item = self.game_state.getObjectById(identifier)
446 else: 453 else:
447 item_data = self.items[identifier]["Entity"] 454 item_data = self.items[identifier]["Entity"]
448 item_type = item_data["containable"]["item_type"] 455 item_type = item_data["containable"]["item_type"]
449 item = self.create_item(identifier, item_data, 456 item = self.createItem(identifier, item_data,
450 world, item_type) 457 world, item_type)
451 equip.equip(obj.equip, item.equipable, slot) 458 equip.equip(obj.equip, item.equipable, slot)
452 if (obj.fifeagent and (obj.lockable and not obj.lockable.closed)): 459 if (obj.fifeagent and (obj.lockable and not obj.lockable.closed)):
453 obj.fifeagent.behaviour.animate("opened", repeating=True) 460 obj.fifeagent.behaviour.animate("opened", repeating=True)
454 return obj 461 return obj
455 462
456 def create_inventory_items(self, inv, obj, world): 463 def createInventoryItems(self, inv, obj, world):
457 slots = inv["Slots"] 464 slots = inv["Slots"]
458 obj.container.children = list() 465 obj.container.children = list()
459 for x in xrange(slots): 466 for x in xrange(slots):
460 obj.container.children.append(None) 467 obj.container.children.append(None)
461 items = inv["Items"] if inv.has_key("Items") else list() 468 items = inv["Items"] if inv.has_key("Items") else list()
462 for data in items: 469 for data in items:
463 item = None 470 item = None
464 slot = data["Slot"] if data.has_key("Slot") else -1 471 slot = data["Slot"] if data.has_key("Slot") else -1
465 if data.has_key("type"): 472 if data.has_key("type"):
466 item_type = data["type"] 473 item_type = data["type"]
467 item_data = {} 474 item = self.createItemByType(item_type, data["ID"], world)
468 item_data = self.checkAttributes(item_data, item_type)
469 if item_data.has_key("containable"):
470 item = self.create_item(
471 self.createUniqueID(data["ID"]),
472 item_data, world, item_type)
473 else:
474 raise Exception("Item %s is not containable." % item_type)
475 else: 475 else:
476 identifier = data["ID"] 476 identifier = data["ID"]
477 if self.game_state.hasObject(identifier): 477 item = self.createItemByID(world, identifier)
478 item = self.game_state.getObjectById(identifier)
479 else:
480 agent_data = self.items[identifier]
481 item_data = agent_data["Entity"]
482 item_type = item_data["containable"]["item_type"]
483 item = self.create_item(identifier, item_data,
484 world, item_type)
485 if item.container and agent_data.has_key("Inventory"):
486 self.create_inventory_items(agent_data["Inventory"],
487 item, world)
488 478
489 container.put_item(obj.container, item.containable, slot) 479 container.put_item(obj.container, item.containable, slot)
490 480
491 def create_item(self, identifier, item_data, world, item_type): 481 def createItemByID(self, world, identifier):
482 if self.game_state.hasObject(identifier):
483 item = self.game_state.getObjectById(identifier)
484 else:
485 agent_data = self.items[identifier]
486 item_data = agent_data["Entity"]
487 item_type = item_data["containable"]["item_type"]
488 item = self.createItem(identifier, item_data,
489 world, item_type)
490 if item.container and agent_data.has_key("Inventory"):
491 self.createInventoryItems(agent_data["Inventory"],
492 item, world)
493 return item
494
495 def createItemByType(self, item_type, identifier, world):
496 item_data = {}
497 item_data = self.checkAttributes(item_data, item_type)
498 if item_data.has_key("containable"):
499 return self.createItem( self.createUniqueID(identifier),
500 item_data, world, item_type)
501 else:
502 raise Exception("Item %s is not containable." % item_type)
503
504 def createItem(self, identifier, item_data, world, item_type):
492 if not item_data["description"].has_key("view_name"): 505 if not item_data["description"].has_key("view_name"):
493 item_data["description"]["view_name"] = ( 506 item_data["description"]["view_name"] = (
494 item_data["description"]["real_name"]) 507 item_data["description"]["real_name"])
495 item = createEntity(item_data, identifier, world, None) 508 item = createEntity(item_data, identifier, world, None)
496 item.containable.item_type = item_type 509 item.containable.item_type = item_type