source: trunk/game/scripts/objects/composed.py @ 632

Revision 632, 5.5 KB checked in by beliar, 9 years ago (diff)

Ticket #139: Patch by Beliar.

  • Initiating a dialogue will now pause the game and no other interaction with the world is possible.
  • Opening the pause menu will now pause the game
  • Opening the save or load window will now pause the game

fixes[s:trac, t:139]

  • 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
18"""Composite game object classes are kept here"""
19
20from base import GameObject, Container, Lockable, \
21                Scriptable, Trapable, Destructable, Carryable, \
22                Usable
23
24class ImmovableContainer(GameObject, Container, Lockable, Scriptable, 
25                         Trapable, Destructable):
26    """Composite class that can be used for crates, chests, etc."""
27    def __init__ (self, **kwargs):
28        GameObject   .__init__(self, **kwargs)
29        Container    .__init__(self, **kwargs)
30        Lockable     .__init__(self, **kwargs)
31        Scriptable   .__init__(self, **kwargs)
32        Trapable    .__init__(self, **kwargs)
33        Destructable .__init__(self, **kwargs)
34        self.blocking = True
35
36class SingleItemContainer (Container) :
37    """Container that can only store a single item.
38       This class can represent single-item inventory slots"""
39    def __init__ (self, **kwargs):
40        Container.__init__(self, **kwargs)
41
42    def placeItem(self,item, index=None):
43        if len(self.items) > 0 :
44            raise self.SlotBusy ('%s is already busy' % self)
45        Container.placeItem(self, item)
46   
47class CarryableItem (GameObject, Carryable, Usable):
48    """Composite class that will be used for all carryable items"""
49    def __init__(self, item_type, **kwargs):
50        GameObject.__init__(self, **kwargs)
51        Carryable.__init__(self, **kwargs)
52        Usable.__init__(self, **kwargs)
53        self.item_type = item_type
54
55    def prepareStateForSaving(self, state):
56        """Prepares state for saving
57        @type state: dictionary
58        @param state: State of the object 
59        """
60        super(CarryableItem, self).prepareStateForSaving(state)
61        if state.has_key("in_container"):
62            del state["in_container"]
63        if state.has_key("on_map"):
64            del state["on_map"]
65        if state.has_key("agent"):
66            del state["agent"]
67
68    def getStateForSaving(self):
69        """Returns state for saving
70        @type state: dictionary
71        @param state: State of the object 
72        """
73        ret_dict = self.__dict__.copy()
74        self.prepareStateForSaving(ret_dict)
75        return ret_dict
76
77class CarryableContainer(Container, CarryableItem):
78    """Composite class that will be used for backpack, pouches, etc."""
79    def __init__ (self, item_type, **kwargs):
80        Container.__init__(self, **kwargs)
81        CarryableItem.__init__(self, item_type, **kwargs)
82        self.own_bulk = 0
83        self.own_weight = 0
84
85    def getWeight(self):
86        """Resulting weight of a container"""
87        return sum((item.weight for item in self.items.values()), 
88                   self.own_weight)
89
90    def setWeight(self, weight):
91        """Set container's own weight.
92        For compatibility with inherited methods"""
93        self.own_weight = weight
94
95    weight = property(getWeight, setWeight, "Total weight of container")
96
97    def getBulk(self):
98        """Resulting bulk of container"""
99        return self.getContentsBulk()+self.own_bulk
100
101    def setBulk(self, bulk):
102        """Set container's own bulk. For compatibility with inherited methods"""
103        self.own_bulk = bulk
104
105    bulk = property(getBulk, setBulk, "Total bulk of container")
106   
107    def __repr__(self):
108        return "[%s" % self.name + str(reduce((lambda a, b: a + ', ' + \
109                                    str(self.items[b])), self.items, "")) + " ]"
110
111    def getStateForSaving(self):
112        """Returns state for saving
113        @type state: dictionary
114        @param state: State of the object 
115        """
116        state = Container.getStateForSaving(self)
117        if not state.has_key("attributes"):
118            state["attributes"] = []
119        state["attributes"].append("Container")
120        state.update(CarryableItem.getStateForSaving(self))
121        return state
122
123class CarryableSingleItemContainer (SingleItemContainer, CarryableContainer) :
124    """Container that can only store a single item.
125       This class can represent single-item inventory slots"""
126    def __init__ (self, item_type, **kwargs):
127        SingleItemContainer.__init__(self, **kwargs)
128        CarryableContainer.__init__(self, item_type, **kwargs)
129       
130class Door(GameObject, Lockable, Scriptable, Trapable):
131    """Composite class that can be used to create doors on a map."""
132    def __init__ (self, target_map_name = 'my-map',
133                  target_x = 0.0, target_y = 0.0, **kwargs):
134        GameObject.__init__(self, **kwargs)
135        Lockable.__init__(self, **kwargs)
136        Scriptable.__init__(self, **kwargs)
137        Trapable.__init__(self, **kwargs)
138        self.attributes.append("door")
139        self.target_map_name = target_map_name
140        self.target_pos = (target_x, target_y)
141        self.blocking = True
142
143    def getStateForSaving(self):
144        """Returns state for saving
145        """
146        ret_dict = super(Door, self).getStateForSaving()
147        return ret_dict
Note: See TracBrowser for help on using the repository browser.