source: trunk/PARPG/scripts/ @ 187

Revision 187, 3.7 KB checked in by tZee_parpg, 11 years ago (diff)

Moved code to the new object classes and the new
(N)PC and (N)PCBehaviour coupling is still a bit clumsy: Too many dereferences between those two. Needs to be improved.
Doors are missing for now and need to be added. Except that all functionality should have been preserved.

3#   This file is part of PARPG.
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
13#   GNU General Public License for more details.
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <>.
18from xml.sax import make_parser
19from xml.sax.handler import ContentHandler
20from objects import getAllObjects
21from objects.containers import *
22import sys
24class ObjectXMLParser(ContentHandler):
25    """ObjectXMLParser call constructors to make GameObjects using information
26       provided in _objects.xml files."""
27    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.
31        self.local_info = []
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
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.
43           @type file_desc: File
44           @param file_desc: an open file from which we read
45           @return: None"""
46        parser = make_parser()
47        parser.setContentHandler(self)
48        self.agent_layer = a_layer
49        parser.parse(file_desc)
50        self.agent_layer = None
52    def startElement(self, name, attrs):
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.
55           @type name: string
56           @param name: XML element?
57           @type attrs: ???
58           @param attrs: XML attributes
59           @return: None"""
60        # For now, only looking for game_obj things
61        if str(name) == "object":
62            obj_info = dict(attrs.items())
63            # we need to convert all the unicode strings to ascii strings
64            for key, val in attrs.items():
65                obj_info.pop(key)
66                obj_info[str(key)] = str(val)
67            self.local_info.append(self.createObject(obj_info))
69    def createObject(self, info):
70        """Called when we need to get an actual object.
71           @type info: dict
72           @param info: stores information about the object we want to create
73           @return: the object"""
74        # First, we try to get the type and ID, which every game_obj needs.
75        try:
76            obj_type = info.pop('type')
77            ID = info.pop('id')
78        except KeyError:
79            sys.stderr.write("Error: Game object missing type or id.")
80            sys.exit(False)
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'] = self.agent_layer
87        all_types = getAllObjects()
88        return all_types[obj_type](ID, **info)
Note: See TracBrowser for help on using the repository browser.