Changeset 186


Ignore:
Timestamp:
07/07/09 20:01:18 (10 years ago)
Author:
meggie_parpg
Message:

Added a reference of the current map's ObjectLayer? to the objectLoader when it is used so that classes such as PC and NPC can be constructed.

Location:
trunk/PARPG/scripts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/PARPG/scripts/engine.py

    r181 r186  
    126126        parser.parse(objects_file) 
    127127        objects_file.seek(0) 
    128         other_handler.getObjects(objects_file) 
     128        other_handler.getObjects(objects_file, self.view.agent_layer) 
    129129        objects_file.close() 
    130130        # must have at least 1 PC 
     
    146146           @return: None""" 
    147147        for obj in objList: 
    148             print obj.X, obj.Y, obj.gfx['map'], obj.ID 
    149148            self.view.addObject(obj.X, obj.Y, obj.gfx['map'], obj.ID) 
    150149 
  • trunk/PARPG/scripts/objectLoader.py

    r178 r186  
    2626       provided in _objects.xml files.""" 
    2727    def __init__(self): 
     28        """Initializes the ObjectXMLParser.""" 
     29        # local_info is a list of dictionaries. When startElement is called  
     30        # (through code in the scripts.Engine), this list is populated. 
    2831        self.local_info = [] 
    29  
    30     def getParser(self): 
    31         """Simple one liner to remove XML dependencies in engine.py. 
    32            @rtype: parser 
    33            @return: A parser to work with""" 
    34         return make_parser() 
     32        # parser is created when getObjects is called. 
     33        self.parser = None 
     34        # an agent layer, which is set to something in getObjects and is set 
     35        # to None at the end of getObjects to ensure that it is always current. 
     36        self.agent_layer = None 
    3537     
    36     def getObjects(self, file_desc): 
    37         """Gets the objects from the file. Populates the dictionary. 
     38    def getObjects(self, file_desc, a_layer): 
     39        """Gets the objects from the file. Populates local_info. This function 
     40           is how the scripts.Engine object interacts with the objectLoader. 
     41           So, this function takes the current agent_layer from the engine and 
     42           sets self.agent_layer so that it can be used in startElement. 
    3843           @type file_desc: File 
    3944           @param file_desc: an open file from which we read 
    4045           @return: None""" 
    41         parser = self.getParser() 
     46        parser = make_parser() 
    4247        parser.setContentHandler(self) 
     48        self.agent_layer = a_layer 
    4349        parser.parse(file_desc) 
     50        self.agent_layer = None 
    4451 
    4552    def startElement(self, name, attrs): 
    46         """Called every time we meet a new element in the XML file 
     53        """Called every time we meet a new element in the XML file. This 
     54           function is specified in ContentHandler, and is called by the parser. 
    4755           @type name: string 
    4856           @param name: XML element? 
     
    6068  
    6169    def createObject(self, info): 
    62         """Called when we need to get an actual object. Nasty if statement. 
    63            (Don't see why this should have its own class though...) 
     70        """Called when we need to get an actual object.  
    6471           @type info: dict 
    6572           @param info: stores information about the object we want to create 
     
    7279            sys.stderr.write("Error: Game object missing type or id.") 
    7380            sys.exit(False) 
     81         
     82        # add the agent_layer to the object dictionary in case it is needed by 
     83        # the object we are constructing. If it is not needed, it will be  
     84        # ignored 
     85        info['agent_layer'] = str(self.agent_layer) 
    7486 
    7587        all_types = getAllObjects() 
Note: See TracChangeset for help on using the changeset viewer.