Changeset 610


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
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/game/maps/all_agents.yaml

    r596 r610  
    2626    - {ID: "Beer", type: "Beer"} 
    2727    - {ID: "Pamphlet", type: "Pamphlet"} 
     28    - {ID: "Pot", type: "Pot"} 
    2829---             
    2930Janie: 
  • trunk/game/objects/object_database.yaml

    r596 r610  
    8686  actions: 
    8787    Look: 
    88       text: Wild potato. Looks apetizing! 
     88      text: Wild potato. Looks appetizing! 
    8989--- 
    9090Rotten yam: 
     
    9999  name: Old pot 
    100100  image: /gui/inv_images/inv_pot.png 
     101  attributes: [Container] 
    101102  actions: 
    102103    Look: 
    103       text: Old pot. Rusty, sooty, hewyweight and durable. 
     104      text: Old pot. Rusty, sooty, heavyweight and durable. 
    104105--- 
    105106Butcher's hook: 
  • trunk/game/scripts/gamemodel.py

    r609 r610  
    1919import sys 
    2020import os.path 
     21from copy import deepcopy 
     22 
     23from fife import fife 
     24 
    2125from gamestate import GameState 
    2226from objects import createObject 
    23 from objects.composed import CarryableItem 
     27from objects.composed import CarryableItem, CarryableContainer 
    2428from gamemap import GameMap 
    25 from fife import fife 
    2629from common.utils import locateFiles 
    2730from common.utils import parseBool 
    2831from inventory import Inventory 
    29 from copy import deepcopy 
    3032 
    3133try: 
     
    119121        return ID 
    120122 
     123    def createContainerItems(self, container_objs): 
     124        """Create the items of a container from a dictionary 
     125        @param container_objs: Dictionary containing the items 
     126        @type container_objs: dict""" 
     127        items = [] 
     128        for container_obj in container_objs: 
     129            items.append(self.createContainerObject(container_obj)) 
     130         
     131        return items 
     132 
    121133    def createContainerObject(self, attributes): 
    122134        """Create an object that can be stored in  
     
    137149            info["item_type"] = info["type"] 
    138150        ID = self.createUniqueID(ID) 
    139         new_item = CarryableItem(ID = ID, **info)  
     151        if info.has_key("attributes"): 
     152            attributes = info["attributes"] 
     153            if "Container" in attributes: 
     154                info["actions"]["Open"] = "" 
     155                if info.has_key("Items"): 
     156                    inventory_objs = info["Items"] 
     157                    info["items"] = self.createContainerItems(inventory_objs) 
     158                 
     159                new_item = CarryableContainer(ID = ID, **info)  
     160        else: 
     161            new_item = CarryableItem(ID = ID, **info)  
    140162        self.game_state.addObject(None, new_item) 
    141163        return new_item 
     
    399421            new_map = GameMap(self.engine, self) 
    400422            self.game_state.maps[map_name] = new_map 
    401             new_map.load(map_file) 
    402      
     423            new_map.load(map_file)     
     424 
    403425    def createAgent(self, agent, inst_id): 
    404426        object_type = agent["ObjectType"] 
     
    489511 
    490512        if agent.has_key("Items"): 
    491             items = [] 
    492             inventory_objs = agent["Items"] 
    493             for inventory_obj in inventory_objs: 
    494                 items.append(self.createContainerObject(inventory_obj)) 
     513            container_objs = agent["Items"] 
     514            items = self.createContainerItems(container_objs) 
    495515            inst_dict["items"] = items 
    496516             
  • trunk/game/scripts/gui/containergui.py

    r606 r610  
    1717from scripts.gui.containergui_base import ContainerGUIBase 
    1818from scripts.gui import drag_drop_data as data_drag 
     19from scripts.objects.base import Container 
    1920 
    2021class ContainerGUI(ContainerGUIBase): 
     
    8889                       the dictionary 'self.buttons'  
    8990           @return: None""" 
    90         drop_widget = self.gui.findChild(name = obj) 
    91         drop_index = drop_widget.index 
    92         replace_item = drop_widget.item 
    93  
    94         if data_drag.dragging: 
    95             container = self.container 
    96             drag_item = data_drag.dragged_item 
    97             #this will get the replacement item and the data for drag_drop if 
    98             ## there is an item all ready occupying the slot 
    99             if replace_item != None: 
    100                 self.dragObject(obj) 
     91        try: 
     92            drop_widget = self.gui.findChild(name = obj) 
     93            drop_index = drop_widget.index 
     94            replace_item = drop_widget.item 
     95     
     96            if data_drag.dragging: 
     97                container = self.container 
     98                drag_item = data_drag.dragged_item 
     99                #this will get the replacement item and the data for drag_drop if 
     100                ## there is an item all ready occupying the slot 
     101                if replace_item != None: 
     102                    self.dragObject(obj) 
     103                container.placeItem(drag_item, drop_index) 
    101104                 
    102             container.placeItem(drag_item, drop_index) 
    103              
    104         drop_widget.item = drag_item 
    105         self.updateImage(drop_widget) 
    106         #if there was no item the stop dragging and reset cursor 
    107         if replace_item == None: 
    108             data_drag.dragging = False 
    109             #reset the mouse cursor to the normal cursor 
    110             self.controller.resetMouseCursor() 
     105            drop_widget.item = drag_item 
     106            self.updateImage(drop_widget) 
     107            #if there was no item the stop dragging and reset cursor 
     108            if replace_item == None: 
     109                data_drag.dragging = False 
     110                #reset the mouse cursor to the normal cursor 
     111                self.controller.resetMouseCursor() 
     112        except (Container.SlotBusy, Container.TooBig, Container.ItemSelf): 
     113            #Do we want to notify the player why the item can't be dropped? 
     114            pass 
    111115         
    112116    def showContainer(self): 
  • trunk/game/scripts/gui/containergui_base.py

    r601 r610  
    7878                    param_dict["item"] = item 
    7979                    display_name = actions[action_name].pop("text") 
    80                 param_dict.update(actions[action_name]) 
     80                if action_name == "Open": 
     81                    param_dict["container"] = item 
     82                if actions[action_name]: 
     83                    param_dict.update(actions[action_name]) 
    8184                menu_actions.append([action_name,  
    8285                                     display_name,  
  • trunk/game/scripts/gui/hud.py

    r600 r610  
    408408            # after createBoxGUI becomes a blocking call or it's otherwise 
    409409            # possible to wait till the box GUI is closed. 
    410             self.box_container.container.close() 
     410            if self.box_container.container.trueAttr("openable"): 
     411                self.box_container.container.close() 
    411412            self.box_container = None 
    412413 
  • trunk/game/scripts/gui/inventorygui.py

    r606 r610  
    184184           @return: None""" 
    185185        drop_widget = self.gui.findChild(name = obj) 
    186         print dir(drop_widget) 
    187186        drop_slot, drop_index = drop_widget.slot, drop_widget.index 
    188187        replace_item = None 
     
    193192                #this will get the replacement item and data for drag_drop if 
    194193                ## there is an item All ready occupying the slot 
    195                 print drag_item 
    196194                if not inventory.isSlotEmpty(drop_slot, drop_index): 
    197195                    #get the item and then remove it from the inventory 
     
    199197                                                (drop_slot, drop_index) 
    200198                    self.dragObject(obj) 
    201                 print drag_item     
    202199                self.inventory_storage.moveItemToSlot(drag_item, 
    203200                                                      drop_slot, 
     
    210207                self.controller.resetMouseCursor() 
    211208                data_drag.dragging = False 
    212             print data_drag.dragged_item, data_drag.dragging     
    213209        except Container.TooBig : 
    214210            print("%s too big to fit into %s" % (data_drag.dragged_item, 
    215211                                                 drop_widget.slot)) 
    216             data_drag.dragged_container.placeItem(data_drag.dragged_item) 
     212        except (Container.SlotBusy, Container.ItemSelf): 
     213            pass 
    217214        self.updateInventoryButtons() 
    218215               
  • trunk/game/scripts/inventory.py

    r609 r610  
    1414# along with PARPG.  If not, see <http://www.gnu.org/licenses/>. 
    1515 
    16 from scripts.objects.base import Container 
    17 from scripts.objects.composed import CarryableContainer, \ 
    18                                         SingleItemContainer as Slot 
     16from scripts.objects.base import Container, SingleItemContainer as Slot 
     17 
    1918import copy 
    2019 
    2120# TODO: many missing function definitions in this code 
    2221 
    23 class Inventory(CarryableContainer): 
     22class Inventory(Container): 
    2423    """The class to represent inventory 'model': allow operations with 
    2524       inventory contents, perform weight/bulk calculations, etc""" 
    2625    def __init__(self, **kwargs): 
    2726        """Initialise instance""" 
    28         CarryableContainer.__init__(self, **kwargs) 
     27        Container.__init__(self, **kwargs) 
    2928        self.items = {"head": Slot(), "neck": Slot(), 
    3029                      "shoulders": Slot(), "chest": Slot(), 
     
    3332                      "hips": Slot(), "left_leg": Slot(), 
    3433                      "right_leg": Slot(), "left_hand": Slot(), 
    35                       "right_hand": Slot(), "ready": CarryableContainer(), 
    36                       "backpack": CarryableContainer()} 
     34                      "right_hand": Slot(), "ready": Container(), 
     35                      "backpack": Container()} 
    3736        for key, item in self.items.iteritems(): 
    3837            item.name = key 
  • 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.