source: trunk/game/scripts/inventory.py @ 504

Revision 480, 4.8 KB checked in by maximinus_parpg, 10 years ago (diff)

Added inventory images to inventory.
Inventory now displays multiple images, not only the dagger.
Image data is stored in the object, not the inventory.

  • Property svn:eol-style set to native
Line 
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
16from scripts.objects.base import GameObject, Container, Carryable
17from scripts.objects.composed import CarryableContainer, SingleItemContainer as Slot
18import copy
19
20# TODO: many missing function definitions in this code
21
22class Inventory(CarryableContainer):
23    """The class to represent inventory 'model': allow operations with
24       inventory contents, perform weight/bulk calculations, etc"""
25    def __init__(self, **kwargs):
26        """Initialise instance"""
27        CarryableContainer.__init__(self, **kwargs)
28        self.items = {"head": Slot(name="head"), "neck": Slot(name="neck"),
29                      "shoulders": Slot(name="shoulders"),
30                      "chest": Slot(), "abdomen": Slot(), "left_arm": Slot(),
31                      "right_arm": Slot(),"groin": Slot(), "hips": Slot(),
32                      "left_leg": Slot(), "right_leg": Slot(),
33                      "left_hand": Slot(), "right_hand": Slot(),
34                      "ready": CarryableContainer(),
35                      "backpack": CarryableContainer()}
36        for key,item in self.items.iteritems():
37            item.name = key
38        self.item_lookup = {}
39
40    def placeItem(self,item, index=None):
41        self.items["backpack"].placeItem(item, index)
42        self.item_lookup[item.ID] = "backpack"
43       
44    def takeItem(self,item):
45        if not item.ID in self.item_lookup:
46            raise ValueError ('I do not contain this item: %s' % item)
47        self.items[self.item_lookup[item.ID]].takeItem(item)
48        self.item_lookup[item.ID] = None
49
50    def getWeight(self):
51        """Total weight of all items in container + container's own weight"""
52        return sum((item.weight for item in self.items.values()))
53
54    def setWeightDummy(self, weight):
55        pass
56
57    weight = property(getWeight, setWeightDummy, "Total weight of container")
58
59
60    def count(self):
61        return sum(item.count() for item in self.items.values())
62   
63    def takeOff(self, item):
64        return self.moveItemToSlot(item, "backpack")
65
66    def moveItemToSlot(self,item,slot,index=None):
67        if not slot in self.items:
68            raise(ValueError,"%s: No such slot" % slot)
69
70        if item.ID in self.item_lookup:
71            self.items[self.item_lookup[item.ID]].takeItem(item)
72        try:
73            self.items[slot].placeItem(item, index)
74        except Container.SlotBusy:
75            if index == None :
76                offending_item = self.items[slot].items[0]
77            else :
78                offending_item = self.items[slot].items[index]
79            self.items[slot].takeItem(offending_item)
80            self.items[slot].placeItem(item, index)
81            self.placeItem(offending_item)
82        self.item_lookup[item.ID] = slot
83     
84    def getItemsInSlot(self, slot, index=None):
85        if not slot in self.items:
86            raise(ValueError,"%s: No such slot" % slot)
87        if index != None:
88            return self.items[slot].items.get(index)
89        else:
90            return copy.copy(self.items[slot].items)
91
92    def isSlotEmpty(self, slot):
93        if not slot in self.items:
94            raise(ValueError,"%s: No such slot" % slot)
95        return self.items[slot].count() == 0
96
97    def has(self, item):
98        return item.ID in self.item_lookup;
99
100    def findItemByID(self, ID):
101        if ID not in self.item_lookup:
102            return None
103        return self.items[self.item_lookup[ID]].findItemByID(ID)
104
105    def findItem(self, **kwargs):
106        """Find an item in inventory by various attributes. All parameters
107           are optional.
108           @type name: String
109           @param name: Object name. If the name is non-unique,
110                        first matching object is returned
111           @type kind: String
112           @param kind: One of the possible object kinds like "openable" or
113                        "weapon" (see base.py)
114           @return: The item matching criteria or None if none was found"""
115        for slot in self.items:
116            item_found = self.items[slot].findItem(**kwargs)
117            if item_found != None:
118                return item_found
119        return None
120
121    def __repr__(self):
122        return "[Inventory contents: " +reduce((lambda a,b: str(a) +', '+str(b)), self.items.values())+" ]"
Note: See TracBrowser for help on using the repository browser.