comparison src/parpg/gamemodel.py @ 88:d89e88a90c9e

Implemented creation of dynamic entities.
author KarstenBock@gmx.net
date Sun, 18 Sep 2011 16:26:12 +0200
parents 1e7465a785c4
children 0f659c7675f6
comparison
equal deleted inserted replaced
87:b764229a0fad 88:d89e88a90c9e
96 self.engine.getAnimationPool(), 96 self.engine.getAnimationPool(),
97 self.engine.getModel(), 97 self.engine.getModel(),
98 self.engine.getVFS() 98 self.engine.getVFS()
99 ) 99 )
100 100
101 def checkAttributes(self, attributes): 101 def checkAttributes(self, attributes, template):
102 """Checks for attributes that where not given in the map file 102 """Checks for attributes that where not given in the map file
103 and fills them with values from the object database 103 and fills them with values from the object database
104 @param attributes: attributes to check 104 @param attributes: attributes to check
105 @type attributes: Dictionary 105 @type attributes: Dictionary
106 @param template: Template from which the values will be used
106 @return: The modified attributes""" 107 @return: The modified attributes"""
107 if attributes.has_key("object_type"): 108 if self.object_db.has_key(template):
108 class_name = attributes.pop("object_type") 109 db_attributes = deepcopy(self.object_db[template])
109 else:
110 class_name = attributes["type"]
111 if not attributes.has_key("type"):
112 attributes["type"] = class_name
113 if self.object_db.has_key(class_name):
114 db_attributes = deepcopy(self.object_db[class_name])
115 for key in db_attributes.keys(): 110 for key in db_attributes.keys():
116 if attributes.has_key(key): 111 if attributes.has_key(key):
117 attributes[key] = attributes[key] or db_attributes[key] 112 attributes[key] = attributes[key] or db_attributes[key]
118 else: 113 else:
119 attributes[key] = db_attributes[key] 114 attributes[key] = db_attributes[key]
156 @type attributes: Dictionary 151 @type attributes: Dictionary
157 @return: The created object """ 152 @return: The created object """
158 # create the extra data 153 # create the extra data
159 extra = {} 154 extra = {}
160 extra['controller'] = self 155 extra['controller'] = self
161 attributes = self.checkAttributes(attributes)
162 156
163 info = {} 157 info = {}
164 info.update(attributes) 158 info.update(attributes)
165 info.update(extra) 159 info.update(extra)
166 ID = info.pop("id") if info.has_key("id") else info.pop("ID") 160 ID = info.pop("id") if info.has_key("id") else info.pop("ID")
384 unique_agent_id = self.createUniqueID(agent.keys()[0]) 378 unique_agent_id = self.createUniqueID(agent.keys()[0])
385 del agent[agent.keys()[0]] 379 del agent[agent.keys()[0]]
386 agent[unique_agent_id] = agent_values 380 agent[unique_agent_id] = agent_values
387 self.agents[namespace].update(agent) 381 self.agents[namespace].update(agent)
388 object_model = "" 382 object_model = ""
389 if agent_values.has_key("ObjectModel"): 383 if agent_values["Entity"].has_key("fifeagent") \
390 object_model = agent_values["ObjectModel"] 384 and agent_values["Entity"]["fifeagent"].has_key("gfx"):
391 elif agent_values["ObjectType"] == "MapItem": 385 object_model = agent_values["Entity"]["fifeagent"]["gfx"]
392 object_data = self.object_db[agent_values["ItemType"]]
393 object_model = object_data["gfx"] if object_data.has_key("gfx") \
394 else "generic_item"
395 else: 386 else:
396 object_model = self.object_db[agent_values["ObjectType"]]["gfx"] 387 object_model = self.object_db[agent_values["Template"]]["fifeagent"]["gfx"]
397 import_file = self.agent_import_files[object_model] 388 import_file = self.agent_import_files[object_model]
398 loadImportFile(self.obj_loader, import_file, self.engine) 389 loadImportFile(self.obj_loader, import_file, self.engine)
399 390
400 def readAgentsOfMap(self, map_name): 391 def readAgentsOfMap(self, map_name):
401 """Read the agents of the map 392 """Read the agents of the map
452 new_map = GameMap(self.engine, self) 443 new_map = GameMap(self.engine, self)
453 self.game_state.maps[map_name] = new_map 444 self.game_state.maps[map_name] = new_map
454 new_map.load(map_file) 445 new_map.load(map_file)
455 446
456 def createAgent(self, agent, inst_id, world): 447 def createAgent(self, agent, inst_id, world):
457 object_type = agent["ObjectType"] 448 entity_data = agent["Entity"]
458 object_id = agent["ObjectModel"] \ 449 if agent.has_key("Template"):
459 if agent.has_key("ObjectModel") \ 450 entity_data = self.checkAttributes(entity_data, agent["Template"])
460 else None 451 object_id = agent["Entity"]["fifeagent"]["gfx"] \
461 if object_id == None: 452 if agent["Entity"]["fifeagent"].has_key("gfx") \
462 if object_type == "MapItem": 453 else "generic_item"
463 object_data = self.object_db[agent["ItemType"]]
464 object_id = object_data["gfx"] if object_data.has_key("gfx") \
465 else "generic_item"
466 else:
467 object_id = self.object_db[object_type]["gfx"]
468 map_obj = self.fife_model.getObject(str(object_id), "PARPG") 454 map_obj = self.fife_model.getObject(str(object_id), "PARPG")
469 if not map_obj: 455 if not map_obj:
470 logging.warning("Object with inst_id={0}, ns=PARPG, " 456 logging.warning("Object with inst_id={0}, ns=PARPG, "
471 "could not be found. " 457 "could not be found. "
472 "Omitting...".format(str(obj_id))) 458 "Omitting...".format(str(obj_id)))
473 459
474 x_pos = agent["Position"][0] 460 x_pos = agent["Position"][0]
475 y_pos = agent["Position"][1] 461 y_pos = agent["Position"][1]
476 z_pos = agent["Position"][2] if len(agent["Position"]) == 3 \ 462 z_pos = agent["Position"][2] if len(agent["Position"]) == 3 \
477 else -0.1 if object_type == "MapItem" \
478 else 0.0 463 else 0.0
479 stack_pos = agent["Stackposition"] if \ 464 stack_pos = agent["Stackposition"] if \
480 agent.has_key("StackPosition") \ 465 agent.has_key("StackPosition") \
481 else None 466 else None
482 inst = self.active_map.agent_layer.\ 467 inst = self.active_map.agent_layer.\
494 if (stack_pos): 479 if (stack_pos):
495 inst.get2dGfxVisual().setStackPosition(int(stack_pos)) 480 inst.get2dGfxVisual().setStackPosition(int(stack_pos))
496 481
497 if (map_obj.getAction('default')): 482 if (map_obj.getAction('default')):
498 target = fife.Location(self.active_map.agent_layer) 483 target = fife.Location(self.active_map.agent_layer)
499 inst.act('default', target, True) 484 inst.act('default', target, True)
485
500 486
501 inst_dict = {} 487 entity_data["fifeagent"]["identifier"] = inst_id
502 inst_dict["identifier"] = inst_id 488 if entity_data["fifeagent"].has_key("behaviour"):
503 inst_dict["world"]= world 489 entity_data["fifeagent"]["behaviour"] = getattr(behaviours, entity_data["fifeagent"]["behaviour"])()
504 inst_dict["type"] = object_type 490 else:
505 inst_dict["gfx"] = object_id 491 entity_data["fifeagent"]["behaviour"] = behaviours.Base()
506 if agent.has_key("Open"):
507 inst_dict["is_open"] = parseBool(agent["Open"])
508 if agent.has_key("Locked"):
509 inst_dict["locked"] = parseBool(agent["Locked"])
510 if agent.has_key("ViewName"):
511 inst_dict["view_name"] = agent["ViewName"]
512 inst_dict["real_name"] = agent["RealName"] \
513 if agent.has_key("RealName") else agent["ViewName"]
514 if agent.has_key("Text"):
515 inst_dict["desc"] = agent["Text"]
516 if self.dialogues.has_key(inst_id): 492 if self.dialogues.has_key(inst_id):
517 inst_dict["dialogue"] = self.dialogues[inst_id] 493 entity_data["dialogue"] = {}
518 if agent.has_key("TargetMap"): 494 entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]
519 inst_dict["target_map_name"] = agent["TargetMap"] 495
520 if agent.has_key("TargetPosition"): 496 self.createMapObject(self.active_map.agent_layer, entity_data, world)
521 inst_dict["target_x"] = agent["TargetPosition"][0]
522 inst_dict["target_y"] = agent["TargetPosition"][1]
523 if agent.has_key("Inventory"):
524 #TODO: Fix setting of inventory
525 #inventory = Inventory()
526 #inventory_objs = agent["Inventory"]
527 #for inventory_obj in inventory_objs:
528 # self.createInventoryObject(inventory,
529 # inventory_obj
530 # )
531 #inst_dict["inventory"] = inventory
532 pass
533
534 if agent.has_key("Items"):
535 container_objs = agent["Items"]
536 #TODO: Create inventory items
537 items = []#self.createContainerItems(container_objs)
538 inst_dict["items"] = items
539
540 if agent.has_key("ItemType"):
541 if not agent.has_key("item"):
542 item_data = {}
543 item_data["type"] = agent["ItemType"]
544 item_data["ID"] = inst_id
545 #TODO item_data = self.createContainerObject(item_data)
546 else:
547 item_data = agent["item"]
548 inst_dict["item"] = item_data
549 inst_dict["item_type"] = agent["ItemType"]
550 if agent.has_key("Behaviour"):
551 inst_dict["behaviour"] = getattr(behaviours, agent["Behaviour"])()
552 #TODO: Add real statistics and bulk
553 if object_type == "Character":
554 inst_dict["statistics"] = {}
555 inst_dict["max_bulk"] = 100
556
557 self.createMapObject(self.active_map.agent_layer, inst_dict)
558 497
559 def placeAgents(self, world): 498 def placeAgents(self, world):
560 """Places the current maps agents """ 499 """Places the current maps agents """
561 if not self.active_map: 500 if not self.active_map:
562 return 501 return
616 # Make the new map active. 555 # Make the new map active.
617 self.active_map = self.game_state.maps[map_name] 556 self.active_map = self.game_state.maps[map_name]
618 self.active_map.makeActive() 557 self.active_map.makeActive()
619 self.game_state.current_map_name = map_name 558 self.game_state.current_map_name = map_name
620 559
621 def createMapObject (self, layer, attributes): 560 def createMapObject (self, layer, attributes, world):
622 """Create an object and add it to the current map. 561 """Create an object and add it to the current map.
623 @type layer: fife.Layer 562 @type layer: fife.Layer
624 @param layer: FIFE layer object exists in 563 @param layer: FIFE layer object exists in
625 @type attributes: Dictionary 564 @type attributes: Dictionary
626 @param attributes: Dictionary of all object attributes 565 @param attributes: Dictionary of all object attributes
628 @param instance: FIFE instance corresponding to the object 567 @param instance: FIFE instance corresponding to the object
629 @return: None""" 568 @return: None"""
630 # create the extra data 569 # create the extra data
631 extra = {} 570 extra = {}
632 if layer is not None: 571 if layer is not None:
633 extra['layer'] = layer 572 extra['fifeagent'] = {}
634 attributes = self.checkAttributes(attributes) 573 extra['fifeagent']['layer'] = layer
635 574
636 obj_type = attributes["type"] 575 obj = createEntity(attributes, world, extra)
637 obj = createEntity(attributes, extra)
638 if obj: 576 if obj:
639 self.addObject(layer, obj, obj_type) 577 self.addObject(layer, obj)
640 578
641 def addPC(self, layer, player_char): 579 def addPC(self, layer, player_char):
642 """Add the PlayerCharacter to the map 580 """Add the PlayerCharacter to the map
643 @type layer: fife.Layer 581 @type layer: fife.Layer
644 @param layer: FIFE layer object exists in 582 @param layer: FIFE layer object exists in
653 self.game_state.getObjectById("PlayerCharacter").fifeagent = player_char 591 self.game_state.getObjectById("PlayerCharacter").fifeagent = player_char
654 self.game_state.getObjectById("PlayerCharacter").fifeagent.setup() 592 self.game_state.getObjectById("PlayerCharacter").fifeagent.setup()
655 self.game_state.getObjectById("PlayerCharacter").fifeagent.behaviour.speed = self.settings.parpg.PCSpeed 593 self.game_state.getObjectById("PlayerCharacter").fifeagent.behaviour.speed = self.settings.parpg.PCSpeed
656 594
657 595
658 def addObject(self, layer, obj, obj_type): 596 def addObject(self, layer, obj):
659 """Adds an object to the map. 597 """Adds an object to the map.
660 @type layer: fife.Layer 598 @type layer: fife.Layer
661 @param layer: FIFE layer object exists in 599 @param layer: FIFE layer object exists in
662 @type obj: GameObject 600 @type obj: GameObject
663 @param obj: corresponding object class 601 @param obj: corresponding object class
673 # yes, use the current game state data 611 # yes, use the current game state data
674 obj.fifeagent.pos.X = ref.X 612 obj.fifeagent.pos.X = ref.X
675 obj.fifeagent.pos.Y = ref.Y 613 obj.fifeagent.pos.Y = ref.Y
676 obj.fifeagent.gfx = ref.gfx 614 obj.fifeagent.gfx = ref.gfx
677 615
678 if obj_type == "Character": 616 if obj.fifeagent.behaviour:
679 obj.fifeagent.behaviour.parent = obj 617 obj.fifeagent.behaviour.parent = obj
680 fifeagent.setup_behaviour(obj.fifeagent) 618 fifeagent.setup_behaviour(obj.fifeagent)
681 obj.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed 619 obj.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed
682 #Start the behaviour 620 #Start the behaviour
683 obj.fifeagent.behaviour.idle() 621 obj.fifeagent.behaviour.idle()