source: trunk/PARPG/scripts/objectLoader.py @ 181

Revision 178, 2.9 KB checked in by meggie_parpg, 10 years ago (diff)

Added new objectLoader. Changed GameObject? constructor to take an xpos and a ypos instead of a tuple, which was necessary because of the way the dictionary unpacking works. Changed the default gfx in WoodenCrate? to make it so that the view can understand it. Added something to engine so that it will make the crate. Ugly hard-coding to get rid of later.

Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
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.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18from xml.sax import make_parser
19from xml.sax.handler import ContentHandler
20from objects import getAllObjects
21from objects.containers import *
22import sys
23
24class ObjectXMLParser(ContentHandler):
25    """ObjectXMLParser call constructors to make GameObjects using information
26       provided in _objects.xml files."""
27    def __init__(self):
28        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()
35   
36    def getObjects(self, file_desc):
37        """Gets the objects from the file. Populates the dictionary.
38           @type file_desc: File
39           @param file_desc: an open file from which we read
40           @return: None"""
41        parser = self.getParser()
42        parser.setContentHandler(self)
43        parser.parse(file_desc)
44
45    def startElement(self, name, attrs):
46        """Called every time we meet a new element in the XML file
47           @type name: string
48           @param name: XML element?
49           @type attrs: ???
50           @param attrs: XML attributes
51           @return: None"""
52        # For now, only looking for game_obj things
53        if name == "game_obj":
54            obj_info = dict(attrs.items())
55            # we need to convert all the unicode strings to ascii strings
56            for key, val in attrs.items():
57                obj_info.pop(key)
58                obj_info[str(key)] = str(val)
59            self.local_info.append(self.createObject(obj_info))
60 
61    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...)
64           @type info: dict
65           @param info: stores information about the object we want to create
66           @return: the object"""
67        # First, we try to get the type and ID, which every game_obj needs.
68        try:
69            obj_type = info.pop('type')
70            ID = info.pop('ID')
71        except KeyError:
72            sys.stderr.write("Error: Game object missing type or id.")
73            sys.exit(False)
74
75        all_types = getAllObjects()
76        return all_types[obj_type](ID, **info)
Note: See TracBrowser for help on using the repository browser.