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

Revision 151, 5.9 KB checked in by maximinus_parpg, 10 years ago (diff)

Merged gamestate and engine.
Fixed a few typos.

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 agents.npc import NPC
19
20class SavedData(object):
21    """ SavedData holds data that can be passed to Engine in the addPC, addNPCs,
22        etc. functions."""
23
24    def __init__(self, PC, npcs, objects, doors):
25        """ Creates a new SavedData object.
26            @type PC: list
27            @param PC: a list of the x y coordinates of the PC
28            @type npcs: dict
29            @param npcs: {npc identifier : npc list info as LocalXMLParser}
30            @type objects: dict
31            @param objects: {object id : object list info as LocalXMLParser}
32            @type doors: dict
33            @param doors: {door id : door list info as LocalXMLParser}
34            @return: None"""
35        # PC = [xposition, yposition]
36        self.PC = PC
37        # one NPC = [xpos, ypos, gfx, ident, text]
38        self.npcs = npcs
39        # one object (if visible) = [True, xpos, ypos, gfx, ident, text,
40        #   contain, carry]
41        # one object (if not visible) = [False, gfx, ident, text, owner,
42        #   contain, carry]
43        self.objects = objects
44        # one door = [id, target map, (xpos, ypos)]
45        self.doors = doors
46
47    def getNPC(self, ident):
48        """ Attempts to find a specific npc in the npc dictionary
49            @type ident: string
50            @param ident: the identifier of the npc we're looking for
51            @return: the list of npc information if found, False otherwise"""
52        return self.npcs.get(ident, False)
53
54    def getPC(self):
55        """ There's only one PC. Return it and throw an exception if it doesn't
56            exist.
57            @rtype: Hero
58            @return: an instance of the Hero class"""
59        if self.PC:
60            return self.PC
61        sys.stderr.write("Error: No PC defined.\n")
62        sys.exit(False)
63 
64    def getList(self, target):
65        """ Turn the a storage dict into a list readable by Engine.addX
66            @type target: string
67            @param target: can be "npcs", "objects", "doors"
68            @return: a list of readable information"""
69        if target == "npcs":
70            return self.npcs.values()
71        elif target == "objects":
72            return self.objects.values()
73        elif target == "doors":
74            return self.doors.values()
75        else:
76            #TODO raise appropriate exception
77            print "not acceptable target"
78
79class Saver(object):
80    """ Saver holds data that will be used for saving and loading maps and
81        objects throughout the game. World has a copy of this class."""
82
83    def __init__(self):
84        """ Constructor for the saver.
85            @return: None"""
86        # { map_id - string : SavedData }
87        self.maps = {}
88        # cur_map is a string of the current filename
89        self.curMap = None
90
91    def addData(self, pc, npcs, objects, doors):
92        """ Adds a new maps worth of data to the maps object.
93            @type pc: list
94            @param pc: a list of the x y coordinates of the PC
95            @type npcs: list
96            @param npcs: a list of various npc information as from .xml files
97            @type objects: list
98            @param objects: a list of various object info as from .xml files
99            @type doors: list
100            @param doors: a list of various door info as from .xml files
101            @return: None"""
102        if self.curMap != None:
103            npcDict = {}
104            for npc in npcs:
105                npcDict[npc[3]] = npc
106            objDict = {}
107            for obj in objects:
108                if obj[0]:
109                    objDict[obj[4]] = obj
110                else:
111                    objDict[obj[2]] = obj
112            doorDict = {}
113            for door in doors:
114                doorDict[door[0]] = door
115            self.maps[self.curMap] = SavedData(pc, npcDict, objDict, doorDict)
116
117    def setCurMap(self, map_name):
118        """ Sets the name of the current map. The name is maps/x.xml.
119            @type map_name: string
120            @param map_name: The name of the map the PC is on
121            @return: None"""
122        self.curMap = map_name
123
124    def getData(self, map_name):
125        """ Checks to see if the saver has a requested map and returns it.
126            Returns false if the map doesn't exist in the saver.
127            @type map_name: string
128            @param map_name: the name of the map to check for
129            @return: the engine object or False"""
130        if self.maps.has_key(map_name):
131            return self.maps[map_name]
132        return False
133   
134    def updatePC(self, PC):
135        """ Updates the PC's information. We care about:
136                - the PC's position
137            TODO: care about other things! (hp, etc)
138            @type PC: Hero
139            @param PC: the PC
140            @return: None"""
141        pcInfo = self.maps[self.curMap].getPC()
142        pcInfo[0] = PC.getX()
143        pcInfo[1] = PC.getY()
144
145    def updateNPCs(self, npcList):
146        """ Updates all NPCs who have changed since the map was loaded.
147            Right now, we care about:
148                - the NPC position
149            @type npcList: list
150            @param npcList: list of members of NPC class
151            @return: None"""
152        for npc in npcList:
153            npcInfo = self.maps[self.curMap].getNPC(npc.id)
154            if npcInfo:
155                npcInfo[0] = npc.getX()
156                npcInfo[1] = npc.getY()
Note: See TracBrowser for help on using the repository browser.