Mercurial > parpg-source
comparison gamestate.py @ 0:7a89ea5404b1
Initial commit of parpg-core.
author | M. George Hansen <technopolitica@gmail.com> |
---|---|
date | Sat, 14 May 2011 01:12:35 -0700 |
parents | |
children | 98f26f7636d8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7a89ea5404b1 |
---|---|
1 # This file is part of PARPG. | |
2 | |
3 # PARPG is free software: you can redistribute it and/or modify | |
4 # it under the terms of the GNU General Public License as published by | |
5 # the Free Software Foundation, either version 3 of the License, or | |
6 # (at your option) any later version. | |
7 | |
8 # PARPG is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 | |
13 # You should have received a copy of the GNU General Public License | |
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. | |
15 | |
16 from parpg.quest_engine import QuestEngine | |
17 | |
18 class GameState(object): | |
19 """This class holds the current state of the game.""" | |
20 def __init__(self, quests_dir = None): | |
21 self.player_character = None | |
22 self.quest_engine = QuestEngine(quests_dir) | |
23 self.quest_engine.readQuests() | |
24 self.objects = {} | |
25 self.object_ids = {} | |
26 self.current_map_name = None | |
27 self.maps = {} | |
28 | |
29 | |
30 def addObject(self, map_id, game_object): | |
31 """Adds an object to the objects and object_ids | |
32 dictionaries. | |
33 @param map_id: ID of the map the object is on. | |
34 If the object is in a container this has to be None | |
35 @param object: object to be added | |
36 @type object: GameObject | |
37 @type map_id: str or None | |
38 """ | |
39 object_id = game_object.ID | |
40 if not self.object_ids.has_key(object_id): | |
41 if map_id: | |
42 self.objects[map_id][object_id] = game_object | |
43 self.object_ids[object_id] = map_id | |
44 | |
45 def deleteObject(self, object_id): | |
46 """Removes an object from the dictionaries | |
47 @param object_id: ID of the object | |
48 @type object_id: str | |
49 """ | |
50 if self.hasObject(object_id): | |
51 map_id = self.getMapOfObject(object_id) | |
52 if map_id: | |
53 inst = self.maps[map_id].agent_layer.getInstance(object_id) | |
54 self.maps[map_id].agent_layer.deleteInstance(inst) | |
55 del self.objects[map_id][object_id] | |
56 del self.object_ids[object_id] | |
57 | |
58 | |
59 def getObjectsFromMap(self, map_id): | |
60 """Gets all objects that are currently on the given map. | |
61 @type map: String | |
62 @param map: The map name. | |
63 @returns: The list of objects on this map. Or an empty list""" | |
64 if map_id in self.objects: | |
65 return [i for i in self.objects[map_id].values() \ | |
66 if map_id in self.objects] | |
67 | |
68 return {} | |
69 | |
70 def hasObject(self, object_id): | |
71 """Check if an object with the given id is present | |
72 @param object_id: ID of the object | |
73 @type object_id: str | |
74 @return: True if there is an object False if not | |
75 """ | |
76 return self.object_ids.has_key(object_id) | |
77 | |
78 def getMapOfObject(self, object_id): | |
79 """Returns the map the object is on. | |
80 @param object_id: ID of the object | |
81 @type object_id: str | |
82 @return: Name of the map the object is on. | |
83 If there is no such object or the object is in a container None is returned | |
84 """ | |
85 if self.object_ids.has_key(object_id): | |
86 return self.object_ids[object_id] | |
87 return None | |
88 | |
89 def getObjectById(self, obj_id, map_id = None): | |
90 """Gets an object by its object id and map id | |
91 @type obj_id: String | |
92 @param obj_id: The id of the object. | |
93 @type map_id: String | |
94 @param map_id: It id of the map containing the object. | |
95 @returns: The object or None.""" | |
96 if not map_id: | |
97 map_id = self.getMapOfObject(obj_id) | |
98 if not map_id in self.objects: | |
99 self.objects[map_id] = {} | |
100 if obj_id in self.objects[map_id]: | |
101 return self.objects[map_id][obj_id] | |
102 | |
103 def clearObjects(self): | |
104 """Delete all objects from the state | |
105 """ | |
106 self.objects = {} | |
107 self.object_ids = {} | |
108 | |
109 def getStateForSaving(self): | |
110 """Prepares state for saving | |
111 @type state: dictionary | |
112 @param state: State of the object | |
113 """ | |
114 ret_dict = {} | |
115 ret_dict["CurrentMap"] = self.current_map_name | |
116 ret_dict["Quests"] = self.quest_engine.getStateForSaving() | |
117 return ret_dict | |
118 | |
119 def restoreFromState(self, state): | |
120 """Restores the state""" | |
121 self.current_map_name = state["CurrentMap"] | |
122 self.quest_engine.readQuests() | |
123 self.quest_engine.restoreFromState(state["Quests"]) | |
124 |