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

Revision 150, 5.2 KB checked in by maximinus_parpg, 10 years ago (diff)

Added tZee's patch.
Some other minor editing.

  • Property svn:eol-style set to native
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
20import sys
21from gamedata import *
22
23class LocalXMLParser(ContentHandler):
24    """Class inherits from ContantHandler, and is used to parse the
25       local objects data"""
26    def __init__(self):
27        """Initialise the instance.
28           @return: None"""
29        self.search = "objects"
30        self.pc = None
31        self.objects = []
32        self.npcs = []
33        self.doors = []
34   
35    def getParser(self):
36        """Simple one liner to remove XML dependencies in engine.py.
37           @rtype: parser
38           @return: A parser to work with"""
39        return make_parser()
40   
41    def getObject(self, attrs):
42        """Grab the object details from the XML data.
43           @type attrs: ???
44           @param attrs: XML attributes
45           @return: None"""
46        try:
47            display = attrs.getValue("display")
48            xpos, ypos = 0, 0
49            owner = None
50            if(display == "True"):
51                xpos = attrs.getValue("xpos")
52                ypos = attrs.getValue("ypos")
53            else:
54                owner = attrs.getValue("owner")
55            gfx = attrs.getValue("gfx")
56            ident = attrs.getValue("id")
57            text = attrs.getValue("text")
58            contain = attrs.getValue("contain")
59            carry = attrs.getValue("carry")
60        except(KeyError):
61            sys.stderr.write("Error: Data missing in object definition\n")
62            sys.exit(False)
63        # now we have the data, save it for later
64        self.objects.append(NonLivingObjectData(str(ident), float(xpos),
65                            float(ypos), str(text), None, str(gfx),
66                            str(display) == "True", str(owner),
67                            str(contain) == "1", str(carry) == "1"))
68
69    def getDoor(self, attrs):
70        """Grab the door data.
71           @type attrs: ???
72           @param attrs: XML attributes
73           @return: None"""
74        try:
75            display = attrs.getValue("display")
76            xpos, ypos, xdest, ydest = 0, 0, 0, 0
77            owner = None
78            if(display == "True"):
79                xpos = attrs.getValue("xpos")
80                ypos = attrs.getValue("ypos")
81            else:
82                owner = attrs.getValue("owner")
83            xdest = attrs.getValue("txpos")
84            ydest = attrs.getValue("typos")
85            gfx = attrs.getValue("gfx")
86            ident = attrs.getValue("id")
87            text = attrs.getValue("text")
88            map = attrs.getValue("map")
89        except(KeyError):
90            sys.stderr.write("Error: Data missing in door definition\n")
91            sys.exit(False)
92        # now we have the data, save it for later
93        door = DoorData(ident, xpos, ypos, text, None, gfx, display == "True",
94                        owner, xdest, ydest, map)
95        self.objects.append(door)
96        self.doors.append(door)
97
98    def startElement(self, name, attrs):
99        """Called every time we meet a new element in the XML file
100           @type name: string
101           @param name: XML element?
102           @type attrs: ???
103           @param attrs: XML attributes
104           @return: None"""
105        # we are only looking for the 'layer' elements, the rest we ignore
106        if(name == "PC"):
107            # already have a PC?
108            if(self.pc != None):
109                sys.stderr.write("Error: 2 PC characters defined")
110                sys.exit(False)
111            # grab the data and store that as well
112            try:
113                xpos = attrs.getValue("xpos")
114                ypos = attrs.getValue("ypos")
115            except(KeyError):
116                sys.stderr.write("Error: Data missing in PC definition")
117                sys.exit(False)
118            # store for later
119            self.pc=[xpos,ypos]
120        elif(name == "NPC"):
121            # let's parse and add the data
122            try:
123                xpos=attrs.getValue("xpos")
124                ypos = attrs.getValue("ypos")
125                gfx = attrs.getValue("gfx")
126                ident = attrs.getValue("id")
127                text = attrs.getValue("text")
128            except(KeyError):
129                sys.stderr.write("Error: Data missing in NPC definition\n")
130                sys.exit(False)
131            # now we have the data, save it for later
132            self.npcs.append(NpcData(ident, xpos, ypos, text, None, gfx, True))
133        elif(name == "object"):
134            # same old same old
135            self.getObject(attrs)
136        elif(name == "door"):
137            # firstly, add the object
138            self.getDoor(attrs)
139
Note: See TracBrowser for help on using the repository browser.