Mercurial > parpg-source
comparison gamestate.py @ 101:824f3068ef2a
Fixed Saving and Loading.
author | KarstenBock@gmx.net |
---|---|
date | Thu, 29 Sep 2011 18:09:56 +0200 |
parents | 0411a4bcceee |
children | 75c0b728ccf3 |
comparison
equal
deleted
inserted
replaced
100:485d866a847f | 101:824f3068ef2a |
---|---|
20 def __init__(self, quests_dir = None): | 20 def __init__(self, quests_dir = None): |
21 self.player_character = None | 21 self.player_character = None |
22 self.quest_engine = QuestEngine(quests_dir) | 22 self.quest_engine = QuestEngine(quests_dir) |
23 self.quest_engine.readQuests() | 23 self.quest_engine.readQuests() |
24 self.objects = {} | 24 self.objects = {} |
25 self.objects[None] = {} | |
26 self.object_ids = {} | 25 self.object_ids = {} |
27 self.current_map_name = None | 26 self.current_map_name = None |
28 self.maps = {} | 27 self.maps = {} |
29 self.npcs_met = set() | 28 self.npcs_met = set() |
30 | 29 |
39 @type map_id: str or None | 38 @type map_id: str or None |
40 @param object: object to be added | 39 @param object: object to be added |
41 @type object: GameObject | 40 @type object: GameObject |
42 """ | 41 """ |
43 if not self.object_ids.has_key(object_id): | 42 if not self.object_ids.has_key(object_id): |
43 if not self.objects.has_key(map_id): | |
44 self.objects[map_id] = {} | |
44 self.objects[map_id][object_id] = game_object | 45 self.objects[map_id][object_id] = game_object |
45 self.object_ids[object_id] = map_id | 46 self.object_ids[object_id] = map_id |
46 | 47 |
47 def deleteObject(self, object_id): | 48 def deleteObject(self, object_id): |
48 """Removes an object from the dictionaries | 49 """Removes an object from the dictionaries |
49 @param object_id: ID of the object | 50 @param object_id: ID of the object |
50 @type object_id: str | 51 @type object_id: str |
52 @returns The deleted object | |
51 """ | 53 """ |
52 if self.hasObject(object_id): | 54 if self.hasObject(object_id): |
53 map_id = self.getMapOfObject(object_id) | 55 map_id = self.getMapOfObject(object_id) |
54 if map_id: | 56 if map_id: |
55 inst = self.maps[map_id].agent_layer.getInstance(object_id) | 57 inst = self.maps[map_id].agent_layer.getInstance(object_id) |
56 self.maps[map_id].agent_layer.deleteInstance(inst) | 58 self.maps[map_id].agent_layer.deleteInstance(inst) |
57 self.objects[map_id][object_id].delete() | 59 obj = self.objects[map_id][object_id] |
58 del self.objects[map_id][object_id] | 60 del self.objects[map_id][object_id] |
59 del self.object_ids[object_id] | 61 del self.object_ids[object_id] |
62 return obj | |
63 return None | |
60 | 64 |
61 | 65 |
62 def getObjectsFromMap(self, map_id): | 66 def getObjectsFromMap(self, map_id): |
63 """Gets all objects that are currently on the given map. | 67 """Gets all objects that are currently on the given map. |
64 @type map: String | 68 @type map: String |
73 def deleteObjectsFromMap(self, map_id): | 77 def deleteObjectsFromMap(self, map_id): |
74 """Deletes all objects of the given map. | 78 """Deletes all objects of the given map. |
75 @type map: String | 79 @type map: String |
76 @param map: The map name. | 80 @param map: The map name. |
77 @returns: None""" | 81 @returns: None""" |
82 deleted_objs = [] | |
78 if map_id in self.objects: | 83 if map_id in self.objects: |
79 for obj in self.objects[map_id].copy(): | 84 for obj in self.objects[map_id].copy(): |
80 self.deleteObject(obj) | 85 deleted_objs.append(self.deleteObject(obj)) |
86 return deleted_objs | |
81 | 87 |
82 def hasObject(self, object_id): | 88 def hasObject(self, object_id): |
83 """Check if an object with the given id is present | 89 """Check if an object with the given id is present |
84 @param object_id: ID of the object | 90 @param object_id: ID of the object |
85 @type object_id: str | 91 @type object_id: str |
124 @param state: State of the object | 130 @param state: State of the object |
125 """ | 131 """ |
126 ret_dict = {} | 132 ret_dict = {} |
127 ret_dict["CurrentMap"] = self.current_map_name | 133 ret_dict["CurrentMap"] = self.current_map_name |
128 ret_dict["Quests"] = self.quest_engine.getStateForSaving() | 134 ret_dict["Quests"] = self.quest_engine.getStateForSaving() |
135 ret_dict["NPCsMet"] = self.npcs_met | |
129 return ret_dict | 136 return ret_dict |
130 | 137 |
131 def restoreFromState(self, state): | 138 def restoreFromState(self, state): |
132 """Restores the state""" | 139 """Restores the state""" |
133 self.current_map_name = state["CurrentMap"] | 140 self.current_map_name = state["CurrentMap"] |
141 self.npcs_met = state["NPCsMet"] | |
134 self.quest_engine.readQuests() | 142 self.quest_engine.readQuests() |
135 self.quest_engine.restoreFromState(state["Quests"]) | 143 self.quest_engine.restoreFromState(state["Quests"]) |
136 | 144 |
137 def meet(self, npc): | 145 def meet(self, npc): |
138 """Record that the PC has met a certain NPC | 146 """Record that the PC has met a certain NPC |