comparison gamemodel.py @ 39:3011bc71ab20

Added funcionality to load Entities from file
author KarstenBock@gmx.net
date Mon, 05 Sep 2011 14:47:21 +0200
parents 06145a6ee387
children ff3e395abf91
comparison
equal deleted inserted replaced
38:b30a72c41f90 39:3011bc71ab20
19 import logging 19 import logging
20 from copy import deepcopy 20 from copy import deepcopy
21 21
22 from fife import fife 22 from fife import fife
23 from fife.extensions.serializers.xmlobject import XMLObjectLoader 23 from fife.extensions.serializers.xmlobject import XMLObjectLoader
24 from parpg.grease.geometry import Vec2d
24 25
25 from parpg import vfs 26 from parpg import vfs
26 from gamestate import GameState 27 from gamestate import GameState
27 from objects import createObject
28 from objects.composed import CarryableItem, CarryableContainer
29 from gamemap import GameMap 28 from gamemap import GameMap
30 from common.utils import locateFiles 29 from common.utils import locateFiles
31 from common.utils import parseBool 30 from common.utils import parseBool
32 from inventory import Inventory
33 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError 31 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError
32 from parpg.entities import createEntity
33 from parpg import behaviours
34 34
35 try: 35 try:
36 import xml.etree.cElementTree as ElementTree 36 import xml.etree.cElementTree as ElementTree
37 except ImportError: 37 except ImportError:
38 import xml.etree.ElementTree as ElementTree 38 import xml.etree.ElementTree as ElementTree
450 map_file = self.map_files[map_name] 450 map_file = self.map_files[map_name]
451 new_map = GameMap(self.engine, self) 451 new_map = GameMap(self.engine, self)
452 self.game_state.maps[map_name] = new_map 452 self.game_state.maps[map_name] = new_map
453 new_map.load(map_file) 453 new_map.load(map_file)
454 454
455 def createAgent(self, agent, inst_id): 455 def createAgent(self, agent, inst_id, world):
456 object_type = agent["ObjectType"] 456 object_type = agent["ObjectType"]
457 object_id = agent["ObjectModel"] \ 457 object_id = agent["ObjectModel"] \
458 if agent.has_key("ObjectModel") \ 458 if agent.has_key("ObjectModel") \
459 else None 459 else None
460 if object_id == None: 460 if object_id == None:
496 if (map_obj.getAction('default')): 496 if (map_obj.getAction('default')):
497 target = fife.Location(self.active_map.agent_layer) 497 target = fife.Location(self.active_map.agent_layer)
498 inst.act('default', target, True) 498 inst.act('default', target, True)
499 499
500 inst_dict = {} 500 inst_dict = {}
501 inst_dict["id"] = inst_id 501 inst_dict["identifier"] = inst_id
502 inst_dict["world"]= world
502 inst_dict["type"] = object_type 503 inst_dict["type"] = object_type
503 inst_dict["xpos"] = x_pos 504 inst_dict["pos"] = Vec2d(x_pos, y_pos)
504 inst_dict["ypos"] = y_pos
505 inst_dict["gfx"] = object_id 505 inst_dict["gfx"] = object_id
506 inst_dict["is_open"] = parseBool(agent["Open"]) \ 506 if agent.has_key("Open"):
507 if agent.has_key("Open") \ 507 inst_dict["is_open"] = parseBool(agent["Open"])
508 else False 508 if agent.has_key("Locked"):
509 inst_dict["locked"] = parseBool(agent["Locked"]) \ 509 inst_dict["locked"] = parseBool(agent["Locked"])
510 if agent.has_key("Locked") \ 510 if agent.has_key("ViewName"):
511 else False 511 inst_dict["view_name"] = agent["ViewName"]
512 inst_dict["name"] = agent["ViewName"] 512 inst_dict["real_name"] = agent["RealName"] \
513 inst_dict["real_name"] = agent["RealName"] \ 513 if agent.has_key("RealName") else agent["ViewName"]
514 if agent.has_key("RealName") \ 514 if agent.has_key("Text"):
515 else agent["ViewName"] 515 inst_dict["desc"] = agent["Text"]
516 inst_dict["text"] = agent["Text"] \
517 if agent.has_key("Text") \
518 else None
519 if self.dialogues.has_key(inst_id): 516 if self.dialogues.has_key(inst_id):
520 inst_dict["dialogue"] = self.dialogues[inst_id] 517 inst_dict["dialogue"] = self.dialogues[inst_id]
521 inst_dict["target_map_name"] = agent["TargetMap"] \ 518 if agent.has_key("TargetMap"):
522 if agent.\ 519 inst_dict["target_map_name"] = agent["TargetMap"]
523 has_key("TargetMap") \ 520 if agent.has_key("TargetPosition"):
524 else None 521 inst_dict["target_x"] = agent["TargetPosition"][0]
525 inst_dict["target_x"] = agent["TargetPosition"][0] \ 522 inst_dict["target_y"] = agent["TargetPosition"][1]
526 if agent.\
527 has_key("TargetPosition") \
528 else None
529 inst_dict["target_y"] = agent["TargetPosition"][1] \
530 if agent.\
531 has_key("TargetPosition") \
532 else None
533 if agent.has_key("Inventory"): 523 if agent.has_key("Inventory"):
534 inventory = Inventory() 524 inventory = Inventory()
535 inventory_objs = agent["Inventory"] 525 inventory_objs = agent["Inventory"]
536 for inventory_obj in inventory_objs: 526 for inventory_obj in inventory_objs:
537 self.createInventoryObject(inventory, 527 self.createInventoryObject(inventory,
552 item_data = self.createContainerObject(item_data) 542 item_data = self.createContainerObject(item_data)
553 else: 543 else:
554 item_data = agent["item"] 544 item_data = agent["item"]
555 inst_dict["item"] = item_data 545 inst_dict["item"] = item_data
556 inst_dict["item_type"] = agent["ItemType"] 546 inst_dict["item_type"] = agent["ItemType"]
547 if agent.has_key("Behaviour"):
548 inst_dict["behaviour"] = getattr(behaviours, agent["Behaviour"])()
549 #TODO: Add real statistics and bulk
550 if object_type == "Character":
551 inst_dict["statistics"] = {}
552 inst_dict["max_bulk"] = 100
557 553
558 self.createMapObject(self.active_map.agent_layer, inst_dict) 554 self.createMapObject(self.active_map.agent_layer, inst_dict)
559 555
560 def placeAgents(self): 556 def placeAgents(self, world):
561 """Places the current maps agents """ 557 """Places the current maps agents """
562 if not self.active_map: 558 if not self.active_map:
563 return 559 return
564 agents = self.getAgentsOfMap(self.game_state.current_map_name) 560 agents = self.getAgentsOfMap(self.game_state.current_map_name)
565 for agent in agents: 561 for agent in agents:
566 if agent == "PlayerCharacter": 562 if agent == "PlayerCharacter":
567 continue 563 continue
568 if self.active_map.agent_layer.getInstances(agent): 564 if self.active_map.agent_layer.getInstances(agent):
569 continue 565 continue
570 self.createAgent(agents[agent], agent) 566 self.createAgent(agents[agent], agent, world)
571 567
572 def placePC(self): 568 def placePC(self):
573 """Places the PlayerCharacter on the map""" 569 """Places the PlayerCharacter on the map"""
574 agent = self.agents[self.ALL_AGENTS_KEY]["PlayerCharacter"] 570 agent = self.agents[self.ALL_AGENTS_KEY]["PlayerCharacter"]
575 inst_id = "PlayerCharacter" 571 inst_id = "PlayerCharacter"
629 @param instance: FIFE instance corresponding to the object 625 @param instance: FIFE instance corresponding to the object
630 @return: None""" 626 @return: None"""
631 # create the extra data 627 # create the extra data
632 extra = {} 628 extra = {}
633 if layer is not None: 629 if layer is not None:
634 extra['agent_layer'] = layer 630 extra['layer'] = layer
635 attributes = self.checkAttributes(attributes) 631 attributes = self.checkAttributes(attributes)
636 632
637 obj = createObject(attributes, extra) 633 obj = createEntity(attributes, extra)
638 634
639 if obj.trueAttr("PC"): 635 if obj.trueAttr("PC"):
640 self.addPC(layer, obj) 636 self.addPC(layer, obj)
641 else: 637 else:
642 self.addObject(layer, obj) 638 self.addObject(layer, obj)