source: branches/active/character_customization/game/parpg/gamestate.py @ 758

Revision 736, 4.6 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites:

  • renamed scripts package to parpg
  • renamed parpg module to application
  • removed packaging and other related files (kept locally for reference, will reintroduce similar scripts to resolve bug #275
  • updated all import statements to respect changes above
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env 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 parpg.quest_engine import QuestEngine
19
20class GameState(object):
21    """This class holds the current state of the game."""
22    def __init__(self, quests_dir = None):
23        self.player_character = None
24        self.quest_engine = QuestEngine(quests_dir)
25        self.quest_engine.readQuests()
26        self.objects = {}
27        self.object_ids = {}
28        self.current_map_name = None
29        self.maps = {}
30       
31       
32    def addObject(self, map_id, game_object):
33        """Adds an object to the objects and object_ids
34        dictionaries.
35        @param map_id: ID of the map the object is on.
36        If the object is in a container this has to be None
37        @param object: object to be added
38        @type object: GameObject
39        @type map_id: str or None
40        """
41        object_id = game_object.ID
42        if not self.object_ids.has_key(object_id):
43            if map_id:
44                self.objects[map_id][object_id] = game_object
45            self.object_ids[object_id] = map_id
46   
47    def deleteObject(self, object_id):
48        """Removes an object from the dictionaries
49        @param object_id: ID of the object
50        @type object_id: str
51        """
52        if self.hasObject(object_id):
53            map_id = self.getMapOfObject(object_id)
54            if map_id:
55                inst = self.maps[map_id].agent_layer.getInstance(object_id)
56                self.maps[map_id].agent_layer.deleteInstance(inst)
57                del self.objects[map_id][object_id]
58            del self.object_ids[object_id]
59           
60           
61    def getObjectsFromMap(self, map_id):
62        """Gets all objects that are currently on the given map.
63           @type map: String
64           @param map: The map name.
65           @returns: The list of objects on this map. Or an empty list"""
66        if map_id in self.objects:
67            return [i for i in self.objects[map_id].values() \
68                                        if map_id in self.objects]
69       
70        return {}
71   
72    def hasObject(self, object_id):
73        """Check if an object with the given id is present
74        @param object_id: ID of the object
75        @type object_id: str
76        @return: True if there is an object False if not
77        """
78        return self.object_ids.has_key(object_id)
79   
80    def getMapOfObject(self, object_id):
81        """Returns the map the object is on.
82        @param object_id: ID of the object
83        @type object_id: str
84        @return: Name of the map the object is on.
85        If there is no such object or the object is in a container None is returned
86        """
87        if self.object_ids.has_key(object_id):
88            return self.object_ids[object_id]
89        return None
90   
91    def getObjectById(self, obj_id, map_id = None):
92        """Gets an object by its object id and map id
93           @type obj_id: String
94           @param obj_id: The id of the object.
95           @type map_id: String
96           @param map_id: It id of the map containing the object.
97           @returns: The object or None."""
98        if not map_id:
99            map_id = self.getMapOfObject(obj_id)
100        if not map_id in self.objects:
101            self.objects[map_id] = {}
102        if obj_id in self.objects[map_id]:
103            return self.objects[map_id][obj_id]
104   
105    def clearObjects(self):
106        """Delete all objects from the state
107        """
108        self.objects = {}
109        self.object_ids = {}
110       
111    def getStateForSaving(self):
112        """Prepares state for saving
113        @type state: dictionary
114        @param state: State of the object 
115        """
116        ret_dict = {}
117        ret_dict["CurrentMap"] = self.current_map_name
118        ret_dict["Quests"] = self.quest_engine.getStateForSaving()
119        return ret_dict
120
121    def restoreFromState(self, state):
122        """Restores the state"""
123        self.current_map_name = state["CurrentMap"]
124        self.quest_engine.readQuests()
125        self.quest_engine.restoreFromState(state["Quests"])
126       
Note: See TracBrowser for help on using the repository browser.