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

Revision 346, 3.3 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #107: patch by b0rland

  • Wrote initial version of the inventory storage class
  • Added some useful utility functions into container class and its derivatives
  • Created several combined classes for carryable containers
  • Extended tests and of course made new ones
  • Renamed old Inventory class to be InventoryGUI

Note: InventoryGUI still works the old way and doesn't use 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
20class Inventory(CarryableContainer):
21    """The class to represent inventory 'model': allow operations with
22    inventory contents, perform weight/bulk calculations, etc"""
23    def __init__(self, ID, **kwargs):
24        """Initialise instance"""
25        CarryableContainer.__init__(self, ID=ID, **kwargs)
26        self.items = {"head": Slot(ID), "neck": Slot(ID), "shoulders": Slot(ID),
27                      "chest": Slot(ID), "abdomen": Slot(ID), "left_arm": Slot(ID),
28                      "right_arm": Slot(ID),"groin": Slot(ID), "hips": Slot(ID),
29                      "left_leg": Slot(ID), "right_leg": Slot(ID),
30                      "backpack": CarryableContainer(ID)}
31        for key,item in self.items.iteritems():
32            item.name = key
33        self.item_lookup = {}
34
35    def placeItem(self,item):
36        self.items["backpack"].placeItem(item)
37        self.item_lookup[item.ID] = "backpack"
38       
39    def takeItem(self,item):
40        if not item.ID in self.item_lookup:
41            raise ValueError ('I do not contain this item: %s' % item)
42        self.items[self.item_lookup[item.ID]].takeItem(item)
43        self.item_lookup[item.ID] = None
44
45    def getWeight(self):
46        """Total weight of all items in container + container's own weight"""
47        return sum((item.weight for item in self.items.values()), self.own_weight)
48
49    def setWeightDummy(self, weight):
50        pass
51
52    weight = property(getWeight, setWeightDummy, "Total weight of container")
53
54
55    def count(self):
56        return sum(item.count() for item in self.items.values())
57   
58    def takeOff(self, item):
59        return self.moveItemToSlot(item, "backpack")
60
61    def moveItemToSlot(self,item,slot):
62        if not slot in self.items:
63            raise(ValueError,"%s: No such slot" % slot)
64
65        if item.ID in self.item_lookup:
66            self.items[self.item_lookup[item.ID]].takeItem(item)
67        try:
68            self.items[slot].placeItem(item)
69        except ValueError:
70            self.takeOff(self.items[slot].items[0])
71            self.items[slot].placeItem(item)
72        self.item_lookup[item.ID] = slot
73     
74    def getItemsInSlot(self, slot):
75        if not slot in self.items:
76            raise(ValueError,"%s: No such slot" % slot)
77        return copy.copy(self.items[slot].items)
78
79    def isSlotEmpty(self, slot):
80        if not slot in self.items:
81            raise(ValueError,"%s: No such slot" % slot)
82        return self.items[slot].count() == 0
83
84    def __repr__(self):
85        return "[%s:%s "%(self.name, self.ID)+reduce((lambda a,b: str(a) +', '+str(b)), self.items.values())+" ]"
Note: See TracBrowser for help on using the repository browser.