Ignore:
Timestamp:
07/31/10 20:00:28 (9 years ago)
Author:
beliar
Message:

Patch by Beliar.

  • Added "Open" action to InventoryItems?. This will open a ContainerGui? for now. Should make a gui that can reflect the actual possible size of the container.
  • Split OpenBoxAction? into a generic OpenAction?, for Containers that are not "openable" and a OpenBoxAction? for all Containers that are "openable" (The latter ones have an "open" method)
  • Moved SingleItemContainer? from composed to base and made it derive from the normal Container class.
  • Added a SingleItemCarryableContainer? class
  • MapItems? in the agent files can now have an attributes item.
  • Added container attribute to possible MapItem? attributes
  • MapItems? that have the "Container" attribute will have the "Open" action automatically added on creation.
  • Inventory now uses Container from base instead of CarryableContainer? from composed
  • Gave "Pot" the "Container" attribute
  • Added ItemSelf? exception to Container which will be raised when trying to place a container in itself.
  • ContainerGui? and InventoryGui? now catch exceptions from the Container
  • Removed debug print statements left over in inventory_gui
Location:
trunk/game/scripts/objects
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/scripts/objects/action.py

    r608 r610  
    8585                              self.target_pos) 
    8686        super(ChangeMapAction, self).execute() 
     87 
     88class OpenAction(Action): 
     89    """Open a container""" 
     90    def __init__(self, controller, container, commands = None): 
     91        """ 
     92        @param controller: A reference to the GameSceneController. 
     93        @type controller: scripts.GameSceneController 
     94        @param commands: Special commands that are executed 
     95        @type commands: Dictionary  
     96        @type view: class derived from scripts.ViewBase 
     97        @param view: The view 
     98        @param container: A reference to the container 
     99        """ 
     100        super(OpenAction, self).__init__(controller, commands) 
     101        self.view = controller.view 
     102        self.container = container 
     103    def execute(self): 
     104        """Open the box.""" 
     105        self.view.hud.createBoxGUI(self.container.name, \ 
     106                                              self.container) 
     107        super(OpenAction, self).execute() 
    87108        
    88 class OpenBoxAction(Action): 
     109        
     110class OpenBoxAction(OpenAction): 
    89111    """Open a box. Needs to be more generic, but will do for now.""" 
    90112    def __init__(self, controller, container, commands = None): 
     
    106128        try: 
    107129            self.container.open() 
    108             self.view.hud.createBoxGUI(self.container.name, \ 
    109                                               self.container) 
    110130            super(OpenBoxAction, self).execute() 
    111131 
     
    382402 
    383403ACTIONS = {"ChangeMap":ChangeMapAction,  
     404           "Open":OpenAction, 
    384405           "OpenBox":OpenBoxAction,  
    385406           "Unlock":UnlockBoxAction, 
  • trunk/game/scripts/objects/base.py

    r609 r610  
    276276        """Exception to be raised when the requested slot is occupied""" 
    277277        pass 
    278  
     278     
     279    class ItemSelf(Exception): 
     280        """Exception to be raised when trying to add the container as an item""" 
     281        pass 
     282   
    279283    def __init__ (self, capacity = 0, items = None, **kwargs): 
    280284        DynamicObject.__init__(self, **kwargs) 
     
    288292         
    289293    def placeItem (self, item, index=None): 
    290         """Adds the provided carriable item to the inventory.  
    291            Runs an 'onStoreItem' script, if present"""     
     294        """Adds the provided carryable item to the inventory.  
     295           Runs an 'onStoreItem' script, if present"""  
     296        if item is self: 
     297            raise self.ItemSelf("Paradox: Can't contain myself")     
    292298        if not item.trueAttr ('carryable'): 
    293             raise TypeError ('%s is not carriable!' % item) 
     299            raise TypeError ('%s is not carryable!' % item) 
    294300        if self.capacity and self.getContentsBulk()+item.bulk > self.capacity: 
    295301            raise self.TooBig ('%s is too big to fit into %s' % (item, self)) 
     
    432438        """Returns state for saving 
    433439        """ 
    434         ret_state = {} 
     440        ret_state = DynamicObject.getStateForSaving(self) 
    435441        ret_state["Items"] = self.serializeItems() 
    436442        return ret_state 
     443 
     444class SingleItemContainer (Container) : 
     445    """Container that can only store a single item. 
     446       This class can represent single-item inventory slots""" 
     447    def __init__ (self, **kwargs): 
     448        Container.__init__(self, **kwargs) 
     449 
     450    def placeItem(self,item, index=None): 
     451        if len(self.items) > 0 : 
     452            raise self.SlotBusy ('%s is already busy' % self) 
     453        Container.placeItem(self, item) 
    437454         
    438455class Living (object): 
  • trunk/game/scripts/objects/composed.py

    r609 r610  
    2020from base import GameObject, Container, Lockable, \ 
    2121                Scriptable, Trappable, Destructable, Carryable, \ 
    22                 Usable 
     22                Usable, SingleItemContainer 
    2323 
    2424class ImmovableContainer(GameObject, Container, Lockable, Scriptable,  
     
    3333        Destructable .__init__(self, **kwargs) 
    3434        self.blocking = True 
     35     
     36class CarryableItem (GameObject, Carryable, Usable): 
     37    """Composite class that will be used for all carryable items""" 
     38    def __init__(self, item_type, **kwargs): 
     39        GameObject.__init__(self, **kwargs) 
     40        Carryable.__init__(self, **kwargs) 
     41        Usable.__init__(self, **kwargs) 
     42        self.item_type = item_type 
    3543 
    36 class CarryableContainer(Container, Carryable): 
     44    def prepareStateForSaving(self, state): 
     45        """Prepares state for saving 
     46        @type state: dictionary 
     47        @param state: State of the object   
     48        """ 
     49        super(CarryableItem, self).prepareStateForSaving(state) 
     50        if state.has_key("in_container"): 
     51            del state["in_container"] 
     52        if state.has_key("on_map"): 
     53            del state["on_map"] 
     54        del state["agent"] 
     55 
     56    def getStateForSaving(self): 
     57        """Returns state for saving 
     58        @type state: dictionary 
     59        @param state: State of the object   
     60        """ 
     61        ret_dict = self.__dict__.copy() 
     62        self.prepareStateForSaving(ret_dict) 
     63        return ret_dict 
     64 
     65class CarryableContainer(Container, CarryableItem): 
    3766    """Composite class that will be used for backpack, pouches, etc.""" 
    38     def __init__ (self, **kwargs): 
     67    def __init__ (self, item_type, **kwargs): 
    3968        Container.__init__(self, **kwargs) 
    40         Carryable.__init__(self, **kwargs) 
     69        CarryableItem.__init__(self, item_type, **kwargs) 
    4170        self.own_bulk = 0 
    4271        self.own_weight = 0 
     
    6796        return "[%s" % self.name + str(reduce((lambda a, b: a + ', ' + \ 
    6897                                    str(self.items[b])), self.items, "")) + " ]" 
    69      
    70 class SingleItemContainer (CarryableContainer) : 
    71     """Container that can only store a single item. 
    72        This class can represent single-item inventory slots""" 
    73     def __init__ (self, **kwargs): 
    74         CarryableContainer.__init__(self, **kwargs) 
    75  
    76     def placeItem(self,item, index=None): 
    77         if len(self.items) > 0 : 
    78             raise self.SlotBusy ('%s is already busy' % self) 
    79         CarryableContainer.placeItem(self, item) 
    80  
    81 class CarryableItem (GameObject, Carryable, Usable): 
    82     """Composite class that will be used for all carryable items""" 
    83     def __init__(self, item_type, **kwargs): 
    84         GameObject.__init__(self, **kwargs) 
    85         Carryable.__init__(self, **kwargs) 
    86         Usable.__init__(self, **kwargs) 
    87         self.item_type = item_type 
    88  
    89     def prepareStateForSaving(self, state): 
    90         """Prepares state for saving 
    91         @type state: dictionary 
    92         @param state: State of the object   
    93         """ 
    94         super(CarryableItem, self).prepareStateForSaving(state) 
    95         if state.has_key("in_container"): 
    96             del state["in_container"] 
    97         del state["on_map"] 
    98         del state["agent"] 
    9998 
    10099    def getStateForSaving(self): 
     
    103102        @param state: State of the object   
    104103        """ 
    105         ret_dict = self.__dict__.copy() 
    106         self.prepareStateForSaving(ret_dict) 
    107         return ret_dict 
     104        state = Container.getStateForSaving(self) 
     105        state.update(CarryableItem.getStateForSaving(self)) 
     106        return state 
     107 
     108class CarryableSingleItemContainer (SingleItemContainer, CarryableContainer) : 
     109    """Container that can only store a single item. 
     110       This class can represent single-item inventory slots""" 
     111    def __init__ (self, item_type, **kwargs): 
     112        SingleItemContainer.__init__(self, **kwargs) 
     113        CarryableContainer.__init__(self, item_type, **kwargs) 
    108114         
    109115class Door(GameObject, Lockable, Scriptable, Trappable): 
Note: See TracChangeset for help on using the changeset viewer.