Changeset 449


Ignore:
Timestamp:
12/14/09 07:48:48 (10 years ago)
Author:
b0rland_parpg
Message:

Ticket #107: patch by b0rland

  • Added bulk and capacity into Carryable and Containers
  • Slightly improved weight property
  • Replaced ValueError? with domain-specific exceptions
  • Added bulk limits processing into InventoryGUI
Location:
trunk/game
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/gui/inventorygui.py

    r444 r449  
    221221 
    222222        drop_widget = self.inventory.findChild(name = obj) 
    223         if drop_widget != data_drag.dragged_widget: 
    224             if data_drag.dragged_container != self.inventory_storage: 
    225                 data_drag.dragged_container.takeItem(data_drag.dragged_item) 
    226             self.inventory_storage.moveItemToSlot(data_drag.dragged_item, 
     223        try : 
     224            if drop_widget != data_drag.dragged_widget: 
     225                if data_drag.dragged_container != self.inventory_storage: 
     226                    data_drag.dragged_container.takeItem(data_drag.dragged_item) 
     227                self.inventory_storage.moveItemToSlot(data_drag.dragged_item, 
    227228                                                  drop_widget.slot, drop_widget.index) 
    228         if drop_widget.slot == 'ready': 
    229             self.readyCallback() 
     229            if drop_widget.slot == 'ready': 
     230                self.readyCallback() 
     231            self.resetMouseCursor() 
     232            data_drag.dragging = False 
     233        except Container.TooBig : 
     234            print("%s too big to fit into %s" % (data_drag.dragged_item, 
     235                                                 drop_widget.slot)) 
     236            data_drag.dragged_container.placeItem(data_drag.dragged_item) 
    230237        self.updateInventoryButtons() 
    231         self.resetMouseCursor() 
    232         data_drag.dragging = False 
    233238               
    234239    def getImage(self, name): 
  • trunk/game/scripts/inventory.py

    r445 r449  
    4949    def getWeight(self): 
    5050        """Total weight of all items in container + container's own weight""" 
    51         return sum((item.weight for item in self.items.values()), self.own_weight) 
     51        return sum((item.weight for item in self.items.values())) 
    5252 
    5353    def setWeightDummy(self, weight): 
     
    7171        try: 
    7272            self.items[slot].placeItem(item, index) 
    73         except ValueError: 
     73        except Container.SlotBusy: 
    7474            if index == None : 
    7575                offending_item = self.items[slot].items[0] 
  • trunk/game/scripts/objects/base.py

    r439 r449  
    184184class Carryable (object): 
    185185    """Allows objects to be stored in containers""" 
    186     def __init__ (self, **kwargs): 
     186    def __init__ (self, weight=0.0, bulk=0.0, **kwargs): 
    187187        self.is_carryable = True 
    188188        self.in_container = None 
    189         self.weight = 1.0 
     189        self.weight = weight 
     190        self.bulk=bulk 
    190191 
    191192    def getInventoryThumbnail(self): 
     
    195196class Container (object): 
    196197    """Gives objects the capability to hold other objects""" 
     198    class TooBig(Exception): 
     199        """Exception to be raised when the object is too big 
     200        to fit into container""" 
     201        pass 
     202     
     203    class SlotBusy(Exception): 
     204        """Exception to be raised when the requested slot is occupied""" 
     205        pass 
     206 
    197207    def __init__ (self, 
    198208                  events = {}, 
    199                   engine = None, **kwargs): 
     209                  engine = None, capacity = 0, **kwargs): 
    200210        self.is_container = True 
    201211        self.items = {} 
    202212        self.events = events 
    203213        self.containergui = None 
     214        self.capacity=capacity 
     215 
    204216        if not self.events: 
    205217            self.events = {'takeAllButton':self.hideContainer, 
     
    212224           Runs an 'onStoreItem' script, if present"""     
    213225        if not item.trueAttr ('carryable'): 
    214             raise ValueError ('% is not carriable!' % item) 
     226            raise TypeError ('%s is not carriable!' % item) 
     227        if self.capacity and self.getContentsBulk()+item.bulk > self.capacity: 
     228            raise self.TooBig ('%s is too big to fit into %s' % (item,self)) 
    215229        item.in_container = self 
    216230        if index == None: 
     
    218232        else: 
    219233            if index in self.items : 
    220                 raise ValueError('Slot %d is busy in %s' % (index, self.name)) 
     234                raise self.SlotBusy('Slot %d is busy in %s' % (index, self.name)) 
    221235            self.items[index]=item 
    222236 
     
    254268    def count (self): 
    255269        return len(self.items) 
     270 
     271    def getContentsBulk(self): 
     272        """Bulk of the container contents""" 
     273        return sum((item.bulk for item in self.items.values())) 
    256274 
    257275    def showContainer (self): 
  • trunk/game/scripts/objects/composed.py

    r424 r449  
    3838        Container.__init__(self,**kwargs) 
    3939        Carryable.__init__(self,**kwargs) 
    40         self.own_weight = 0 
    4140 
    4241    def getWeight(self): 
    43         """Total weight of all items in container + container's own weight""" 
     42        """Resulting weight of a container""" 
    4443        return sum((item.weight for item in self.items.values()), self.own_weight) 
    4544 
    4645 
    4746    def setWeight(self, weight): 
     47        """Set container's own weight. For compatibility with inherited methods""" 
    4848        self.own_weight = weight 
    4949 
    5050    weight = property(getWeight, setWeight, "Total weight of container") 
     51 
     52    def getBulk(self): 
     53        """Resulting bulk of container""" 
     54        return self.getContentsBulk()+self.own_bulk 
     55 
     56    def setBulk(self, bulk): 
     57        """Set container's own bulk. For compatibility with inherited methods""" 
     58        self.own_bulk = bulk 
     59 
     60    bulk = property(getBulk, setBulk, "Total bulk of container") 
    5161 
    5262    def __repr__(self): 
  • trunk/game/tests/test_container.py

    r424 r449  
    6565        self.assertEqual(self.item.in_container, container) 
    6666        self.assertEqual(container.count(), 1) 
     67        self.assertRaises(Container.SlotBusy, container.placeItem, self.item2, 0) 
    6768        self.assertRaises(ValueError, container.takeItem, self.item2) 
    6869        container.takeItem(self.item) 
    6970        self.assertEqual(container.items, {}) 
    7071        self.assertEqual(container.count(), 0) 
     72 
     73    def testBulk(self): 
     74        container = self.NonScriptableContainer(5, capacity=15) 
     75        self.item.bulk=10 
     76        self.item2.bulk=7 
     77        container.placeItem(self.item) 
     78        self.assertRaises(Container.TooBig, container.placeItem, self.item2) 
    7179 
    7280    def testScripting(self): 
  • trunk/game/tests/test_objects_base.py

    r424 r449  
    5050            locked = False, 
    5151            is_carryable = True, 
    52             weight = 1.0, 
     52            weight = 0.0, 
     53            bulk = 0.0, 
    5354            is_container = True, 
    5455            items = {}, 
Note: See TracChangeset for help on using the changeset viewer.