Ignore:
Timestamp:
12/03/09 10:02:10 (10 years ago)
Author:
b0rland_parpg
Message:

Ticket #107: patch by b0rland

  • Reworked inventory gui to use new inventory model
  • Made various additions and fixes to inventory model and base classes
  • Improved drag-and-drop support
  • Possibly broke dialog gui :(
Location:
trunk/game
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/game

    • Property svn:ignore
      •  

        old new  
        11*.dll 
        22Thumbs.db 
         3.project 
        34update_dlls_mingw.bat 
        45update_dlls_msvc2005.bat 
  • trunk/game/scripts/objects/actors.py

    r362 r424  
    1818from scripts import world 
    1919from base import * 
     20from composed import * 
     21from scripts.inventory import Inventory 
    2022 
    2123"""All actors go here. Concrete classes only.""" 
     
    110112    PC class 
    111113    """ 
    112     def __init__ (self, ID, agent_layer = None, **kwargs): 
     114    def __init__ (self, ID, agent_layer = None, inventory = None, **kwargs): 
    113115        GameObject.__init__( self, ID, **kwargs ) 
    114116        Living.__init__( self, **kwargs ) 
     
    116118 
    117119        self.is_PC = True 
    118         self.inventory = set(('beer',)) 
     120         
     121        # PC _has_ an inventory, he _is not_ one 
     122        if inventory == None: 
     123            self.inventory = Inventory() 
     124            self.inventory.placeItem(CarryableItem(ID=456, name="Dagger123")) 
     125            self.inventory.placeItem(CarryableItem(ID=555, name="Beer")) 
     126        else: 
     127            self.inventory = inventory 
    119128        self.peopleIknow = set() 
     129 
    120130        self.state = _AGENT_STATE_NONE 
    121131        self.layer_id = agent_layer.getId() 
     
    260270    """ 
    261271    def __init__(self, ID, agent_layer=None, name='NPC', \ 
    262                  text = 'A nonplayer character', **kwargs): 
     272                 text = 'A nonplayer character', inventory = None, **kwargs): 
    263273        # init game object 
    264274        GameObject.__init__(self, ID, name=name, **kwargs) 
     
    268278 
    269279        self.is_NPC = True 
    270         self.inventory = None 
     280        if inventory == None: 
     281            self.inventory = Inventory() 
     282        else: 
     283            self.inventory = inventory 
    271284        self.layer_id = agent_layer.getId() 
    272285        self.createBehaviour(agent_layer)         
  • trunk/game/scripts/objects/base.py

    r395 r424  
    5656from scripts.gui.popups import ExaminePopup, ContainerGUI 
    5757 
    58 class GameObject (object): 
     58class DynamicObject (object): 
     59    """A base class that only supports dynamic attributes functionality""" 
     60    def __init__ (self, name="Dynamic object", **kwargs): 
     61        """Initialise minimalistic set of data 
     62        @type name: String 
     63        @param name: Object display name 
     64        """ 
     65        self.name = name 
     66 
     67    def trueAttr(self, attr): 
     68        """Shortcut function to check if the current object has a member named 
     69           is_%attr and if that attribute evaluates to True""" 
     70        return hasattr(self,'is_%s' % attr) and getattr(self, 'is_%s' % attr) 
     71 
     72 
     73class GameObject (DynamicObject): 
    5974    """A base class to be inherited by all game objects. This must be the 
    6075       first class (left to right) inherited by any game object.""" 
     
    8095           @param desc: A long description of the item that is displayed when it is examined 
    8196           """ 
    82          
     97        DynamicObject.__init__(self, name, **kwargs) 
    8398        self.ID = ID 
    8499        self.gfx = gfx 
     
    87102        self.map_id = map_id 
    88103        self.blocking = True 
    89         self.name = name 
    90104        self.text = text 
    91105        self.desc = desc 
    92106         
    93     def trueAttr(self, attr): 
    94         """Shortcut function to check if the current object has a member named 
    95            is_%attr and if that attribute evaluates to True""" 
    96         return hasattr(self,'is_%s' % attr) and getattr(self, 'is_%s' % attr) 
    97  
    98107    def _getCoords(self): 
    99108        """Get-er property function""" 
     
    179188        self.in_container = None 
    180189        self.weight = 1.0 
     190 
     191    def getInventoryThumbnail(self): 
     192        #TODO: Implement properly after the objects database is in place 
     193        return "gui/inv_images/inv_litem.png" 
    181194     
    182195class Container (object): 
     
    184197    def __init__ (self, 
    185198                  events = {}, 
    186                   items = ["empty", "empty", "empty", "empty", "empty","empty", "empty", "empty", "empty"], 
    187199                  engine = None, **kwargs): 
    188200        self.is_container = True 
    189         self.items = items 
     201        self.items = {} 
    190202        self.events = events 
     203        self.containergui = None 
    191204        if not self.events: 
    192205            self.events = {'takeAllButton':self.hideContainer, 
    193206                      'closeButton':self.hideContainer} 
    194         self.containergui = engine.view.hud.createBoxGUI(self.name, self.items, self.events) 
    195          
    196     def placeItem (self, item): 
     207        if engine : 
     208            self.containergui = engine.view.hud.createBoxGUI(self.name, self, self.events) 
     209         
     210    def placeItem (self, item, index=None): 
    197211        """Adds the provided carriable item to the inventory.  
    198212           Runs an 'onStoreItem' script, if present"""     
     
    200214            raise ValueError ('% is not carriable!' % item) 
    201215        item.in_container = self 
    202         self.items.append (item) 
     216        if index == None: 
     217            self.placeAtVacant(item) 
     218        else: 
     219            if index in self.items : 
     220                raise ValueError('Slot %d is busy in %s' % (index, self.name)) 
     221            self.items[index]=item 
     222 
    203223        # Run any scripts associated with storing an item in the container 
    204224        try: 
     
    208228            pass 
    209229 
     230    def placeAtVacant(self, item): 
     231        vacant = None 
     232        for i in range(len(self.items)): 
     233            if i not in self.items : 
     234                vacant = i 
     235        if vacant == None : 
     236            vacant = len(self.items) 
     237        self.items[vacant] = item 
     238 
     239 
    210240    def takeItem (self, item): 
    211241        """Takes the listed item out of the inventory.  
    212242           Runs an 'ontakeItem' script"""         
    213         if not item in self.items: 
     243        if not item in self.items.values(): 
    214244            raise ValueError ('I do not contain this item: %s' % item) 
    215         self.items.remove (item) 
     245        del self.items[self.items.keys()[self.items.values().index(item)]] 
     246 
    216247        # Run any scripts associated with popping an item out of the container 
    217248        try: 
     
    224255        return len(self.items) 
    225256 
    226     def showContainer (self):         
    227         self.containergui.showContainer() 
     257    def showContainer (self): 
     258        if self.containergui: 
     259            self.containergui.showContainer() 
    228260 
    229261    def hideContainer(self): 
    230         self.containergui.hideContainer() 
     262        if self.containergui: 
     263            self.containergui.hideContainer() 
    231264         
    232265class Living (object): 
  • trunk/game/scripts/objects/composed.py

    r373 r424  
    3232        self.blocking = True 
    3333 
    34 class CarryableContainer(GameObject, Container, Carryable): 
     34class CarryableContainer(DynamicObject, Container, Carryable): 
    3535    """Composite class that will be used for backpack, pouches, etc.""" 
    36     def __init__ (self, ID, **kwargs): 
    37         GameObject.__init__(self, ID, **kwargs) 
    38         Container.__init__(self, **kwargs) 
     36    def __init__ (self, **kwargs): 
     37        DynamicObject.__init__(self, **kwargs) 
     38        Container.__init__(self,**kwargs) 
    3939        Carryable.__init__(self,**kwargs) 
    4040        self.own_weight = 0 
     
    4242    def getWeight(self): 
    4343        """Total weight of all items in container + container's own weight""" 
    44         return sum((item.weight for item in self.items), self.own_weight) 
     44        return sum((item.weight for item in self.items.values()), self.own_weight) 
    4545 
    4646 
     
    5151 
    5252    def __repr__(self): 
    53         return "[%s:%s "%(self.name, self.ID) +str(reduce((lambda a,b: a +', '+str(b)), self.items,""))+" ]" 
     53        return "[%s"%self.name +str(reduce((lambda a,b: a +', '+str(b)), self.items,""))+" ]" 
    5454 
    5555 
    5656class SingleItemContainer (CarryableContainer) : 
    5757    """Container that can only store a single item. This class can represent single-item inventory slots""" 
    58     def __init__ (self, ID, **kwargs): 
    59         CarryableContainer.__init__(self, ID,**kwargs) 
     58    def __init__ (self, **kwargs): 
     59        CarryableContainer.__init__(self, **kwargs) 
    6060 
    61     def placeItem(self,item): 
     61    def placeItem(self,item, index=None): 
    6262        if len(self.items) > 0 : 
    6363            raise ValueError ('%s is already busy' % self) 
    6464        CarryableContainer.placeItem(self, item) 
     65 
     66class CarryableItem (GameObject, Carryable): 
     67    """Composite class that will be used for all carryable items, weapons, etc. 
     68    """ 
     69    def __init__(self, **kwargs): 
     70        GameObject.__init__(self,**kwargs) 
     71        Carryable.__init__(self,**kwargs) 
    6572 
    6673class Door(GameObject, Lockable, Scriptable, Trappable): 
  • trunk/game/scripts/objects/containers.py

    r395 r424  
    2424 
    2525from composed import ImmovableContainer 
     26from composed import CarryableItem 
    2627import fife 
    2728 
     
    3233          'closeButton':self.close} 
    3334        ImmovableContainer.__init__(self, ID = ID, name = name, gfx = gfx, \ 
    34                 text = text, events = events, items = ["dagger01", "empty", "empty", "empty", "empty", 
    35                     "empty", "empty", "empty", "empty"], **kwargs) 
     35                text = text, events = events, **kwargs) 
     36        self.placeItem(CarryableItem(ID=987,name="Dagger456")) 
    3637 
    3738    def close(self, *args, **kwargs): 
     
    4950                  'closeButton':self.close} 
    5051        ImmovableContainer.__init__(self, ID = ID, name = name, gfx = gfx, \ 
    51                 text = text, events = events, items = ["dagger01", "empty", "empty", "empty", "empty", 
    52                                                        "empty", "empty", "empty", "empty"], **kwargs) 
     52                text = text, events = events, **kwargs) 
     53        self.placeItem(CarryableItem(ID=987,name="Dagger456")) 
     54 
    5355        fife.InstanceActionListener.__init__(self) 
    5456        self.layer = agent_layer 
Note: See TracChangeset for help on using the changeset viewer.