comparison gamemodel.py @ 88:0411a4bcceee

Fixed moving between maps.
author KarstenBock@gmx.net
date Mon, 26 Sep 2011 15:44:42 +0200
parents a9cc5559ec2a
children 939984cff702
comparison
equal deleted inserted replaced
87:aed2e094e0c7 88:0411a4bcceee
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 import components
34 from parpg.components import fifeagent, container, equip 35 from parpg.components import fifeagent, container, equip
35 36
36 try: 37 try:
37 import xml.etree.cElementTree as ElementTree 38 import xml.etree.cElementTree as ElementTree
38 except ImportError: 39 except ImportError:
396 new_map = GameMap(self.engine, self) 397 new_map = GameMap(self.engine, self)
397 self.game_state.maps[map_name] = new_map 398 self.game_state.maps[map_name] = new_map
398 new_map.load(map_file) 399 new_map.load(map_file)
399 400
400 def createAgent(self, agent, inst_id, world): 401 def createAgent(self, agent, inst_id, world):
401 entity_data = agent["Entity"] 402 entity_data = deepcopy(agent["Entity"])
402 if agent.has_key("Template"): 403 if agent.has_key("Template"):
403 entity_data = self.checkAttributes(entity_data, agent["Template"]) 404 entity_data = self.checkAttributes(entity_data, agent["Template"])
404 object_id = agent["Entity"]["fifeagent"]["gfx"] \ 405 object_id = entity_data["fifeagent"]["gfx"] \
405 if agent["Entity"]["fifeagent"].has_key("gfx") \ 406 if entity_data["fifeagent"].has_key("gfx") \
406 else "generic_item" 407 else "generic_item"
407 map_obj = self.fife_model.getObject(str(object_id), "PARPG") 408 map_obj = self.fife_model.getObject(str(object_id), "PARPG")
408 if not map_obj: 409 if not map_obj:
409 logging.warning("Object with inst_id={0}, ns=PARPG, " 410 logging.warning("Object with inst_id={0}, ns=PARPG, "
410 "could not be found. " 411 "could not be found. "
411 "Omitting...".format(str(obj_id))) 412 "Omitting...".format(str(object_id)))
412 413
413 x_pos = agent["Position"][0] 414 x_pos = agent["Position"][0]
414 y_pos = agent["Position"][1] 415 y_pos = agent["Position"][1]
415 z_pos = agent["Position"][2] if len(agent["Position"]) == 3 \ 416 z_pos = agent["Position"][2] if len(agent["Position"]) == 3 \
416 else 0.0 417 else 0.0
597 self.game_state.addObject(obj.general.identifier, self.game_state.current_map_name, obj) 598 self.game_state.addObject(obj.general.identifier, self.game_state.current_map_name, obj)
598 else: 599 else:
599 # yes, use the current game state data 600 # yes, use the current game state data
600 obj.fifeagent.pos.X = ref.X 601 obj.fifeagent.pos.X = ref.X
601 obj.fifeagent.pos.Y = ref.Y 602 obj.fifeagent.pos.Y = ref.Y
602 obj.fifeagent.gfx = ref.gfx 603 obj.fifeagent.gfx = ref.gfx
603 604
604 if obj.fifeagent.behaviour: 605 if obj.fifeagent.behaviour:
605 obj.fifeagent.behaviour.parent = obj 606 obj.fifeagent.behaviour.parent = obj
606 fifeagent.setup_behaviour(obj.fifeagent) 607 fifeagent.setup_behaviour(obj.fifeagent)
607 obj.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed 608 obj.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed
654 database_file = vfs.VFS.open(self.object_db_file) 655 database_file = vfs.VFS.open(self.object_db_file)
655 database = yaml.load_all(database_file) 656 database = yaml.load_all(database_file)
656 for object_info in database: 657 for object_info in database:
657 self.object_db.update(object_info) 658 self.object_db.update(object_info)
658 659
660 def updateObjectDB(self, world):
661 """Updates the values in the object database with the worlds values"""
662
663 for entity in world.entities:
664 identifier = entity.general.identifier
665 agent_data = {}
666 map_id = self.game_state.getMapOfObject(identifier)
667 if not map_id:
668 continue
669 if self.agents[self.ALL_AGENTS_KEY].has_key(identifier):
670 agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
671 else:
672 agent_data = self.agents[map_id][identifier]
673
674 entity_data = {}
675 entity_data["general"] = {"identifier": identifier}
676 for name, component in components.components.iteritems():
677 if hasattr(entity, name):
678 comp_data = {}
679 if name == "fifeagent":
680 if agent_data["Entity"].has_key("fifeagent"):
681 comp_data = agent_data["Entity"]["fifeagent"]
682 if entity.fifeagent.layer:
683 layer = entity.fifeagent.layer
684 inst = layer.getInstance(identifier)
685 loc = inst.getLocation().getExactLayerCoordinates()
686 agent_data["Position"] = (loc.x, loc.y, loc.z)
687 agent_data["Map"] = map_id
688 agent_data["Rotation"] = inst.getRotation()
689 elif name == "container":
690 #TODO: Save Inventory
691 pass
692 elif name == "equip":
693 #TODO: Save Equipment
694 pass
695 else:
696 comp_vals = getattr(entity, name)
697 for field in component.fields:
698 try:
699 comp_data[field] = getattr(comp_vals, field)
700 except AttributeError:
701 #The entity doesn't have this specific value,
702 #ignore it
703 pass
704 entity_data[name] = comp_data
705 agent_data["Entity"] = entity_data
706
707
659 def getAgentImportFiles(self): 708 def getAgentImportFiles(self):
660 """Searches the agents directory for import files """ 709 """Searches the agents directory for import files """
661 filepaths = locateFiles("*.xml", self.objects_directory) 710 filepaths = locateFiles("*.xml", self.objects_directory)
662 for filepath in filepaths: 711 for filepath in filepaths:
663 try: 712 try: