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/scripts/objects
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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): 
Note: See TracChangeset for help on using the changeset viewer.