Changeset 165


Ignore:
Timestamp:
06/28/09 20:43:07 (10 years ago)
Author:
tie_parpg
Message:

Added docstrings to base object classes, and fixed some bugs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PARPG/scripts/objects/base.py

    r164 r165  
    1616#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>. 
    1717 
     18"""Containes classes defining the base properties of all interactable in-game  
     19objects (such as Carryable, Openable, etc. These are generally independent  
     20classes, which can be combined in almost any way and order.  
     21 
     22Some rules that should be followed when CREATING base property classes: 
     231. If you want to support some custom initialization arguments, always define  
     24them as keyword ones. Only GameObject would use positional arguments. 
     252. In __init__() **ALWAYS** call the parent's __init__(**kwargs), preferably *at the 
     26end* of your __init__() (makes is easier to follow) 
     273. There should always be an is_x class member set to True on __init__ (where X is  
     28the name of the class) 
     29 
     30EXAMPLE: 
     31class Openable(object): 
     32    def __init__ (self, is_open = True, **kwargs): 
     33        self.is_openable = True 
     34        self.is_open = is_open 
     35        super(Openable,self).__init__ (**kwargs) 
     36         
     37==== 
     38 
     39Some rules are to be followed when USING the base classes to make composed ones: 
     401. The first parent (left to right) should always be the base GameObject class 
     412. Base classes other than GameObject can be inherited in any order 
     423. The __init__ functoin of the composed class should always invoke the parent's 
     43__init__() *before* it starts customizing any variables. 
     44 
     45EXAMPLE: 
     46class TinCan (GameObject, Container, Scriptable, Destructable, Carryable): 
     47    def __init__ (self, *args, **kwargs): 
     48        super(TinCan,self).__init__ (*args, **kwargs) 
     49        self.name = 'Tin Can' 
     50""" 
    1851 
    1952class GameObject (object): 
    20     """A base class that should be inherited by all interactable game objects""" 
     53    """A base class that should be inherited by all interactable game objects. 
     54    This must be the first class (left to right) inherited by any game object. 
     55    """ 
    2156    def __init__ (self, ID, gfx = {}, coords = (0.0,0.0), mapref = None,  
    22                   name="Generic object", text="Item description", *args, **kwargs): 
     57                  name="Generic object", text="Item description", **kwargs): 
    2358        """Set the basic values that are shared by all game objects. 
    2459        @type ID: String 
     
    6499 
    65100class Openable(object): 
     101    """Adds open() and .close() capabilities to game objects 
     102    The current state is tracked by the .is_open variable""" 
    66103    def __init__ (self, is_open = True, **kwargs): 
     104        """Init operation for openable objects 
     105        @type is_open: Boolean 
     106        @param is_open: Keyword boolen argument to set the initial open state. 
     107        """ 
    67108        self.is_openable = True 
    68109        self.is_open = is_open 
     
    70111     
    71112    def open(self): 
     113        """Opens the object, and runs an 'onOpen' script, if present""" 
    72114        self.is_open = True 
    73      
     115        if self.trueAttr ('scriptable'): 
     116            self.runScript('onOpen') 
     117             
    74118    def close(self): 
    75         self.is_closed = True 
     119        """Opens the object, and runs an 'onClose' script, if present""" 
     120        self.is_open = False 
     121        if self.trueAttr ('scriptable'): 
     122            self.runScript('onClose')              
    76123         
    77124class Lockable (Openable): 
     125    """Allows objects to be locked""" 
    78126    def __init__ (self, locked = True, **kwargs): 
     127        """Init operation for lockable objects 
     128        @type locked: Boolean 
     129        @param locked: Keyword boolen argument to set the initial locked state. 
     130        """ 
    79131        self.is_lockable = True 
    80132        self.locked = locked 
     
    82134         
    83135    def unlock (self): 
     136        """Handles unlocking functionality""" 
    84137        self.locked = False 
    85         self.close()         
     138        self.open()         
    86139         
    87140    def lock (self): 
     141        """Handles  locking functionality""" 
     142        self.close() 
    88143        self.locked = True 
    89         self.is_open = False 
    90144         
    91145class Carryable (object): 
     146    """Allows objects to be stored in containers""" 
    92147    def __init__ (self, **kwargs): 
    93148        self.is_carryable = True 
     
    97152     
    98153class Container (object): 
     154    """Gives objects the capability to hold other objects""" 
    99155    def __init__ (self, **kwargs): 
    100156        self.is_container = True 
     
    103159         
    104160    def storeItem (self, item): 
     161        """Adds the provided carriable item to the inventory.  
     162        Runs an 'onStoreItem' script, if present"""     
    105163        if not item.trueAttr ('carryable'): 
    106164            raise ValueError ('% is not carriable!' % item) 
    107165        item.in_container = self 
    108166        self.items.append (item) 
     167        # Run any scripts associated with storing an item in the container 
     168        if self.trueAttr ('scriptable'): 
     169            self.runScript('onStoreItem') 
    109170         
    110171    def popItem (self, item, newcontainer): 
     172        """Takes the listed item out of the inventory, and adds it to 
     173        `newcontainer`. Runs an 'onPopItem' script"""         
    111174        if not item in self.items: 
    112175            raise ValueError ('I do not contain this item: %s' % item) 
    113176        self.items.remove (item) 
    114         newcontainer.putItem (item) 
     177        newcontainer.storeItem (item) 
     178        # Run any scripts associated with popping an item out of the container 
     179        if self.trueAttr ('scriptable'): 
     180            self.runScript('onPopItem') 
    115181         
    116182class Inventory (object): 
     
    129195         
    130196class Scriptable (object): 
     197    """Allows objects to have predefined scripts executed on certain events""" 
    131198    def __init__ (self, scripts = {}, **kwargs): 
     199        """Init operation for scriptable objects 
     200        @type scripts: Dictionary 
     201        @param scripts: Dictionary where the event strings are keys. The values  
     202        are 3-item tuples (function, positional_args, keyword_args) 
     203        """  
    132204        self.is_scriptable = True 
    133205        self.scripts = scripts  
    134206        super(Scriptable,self).__init__ (**kwargs) 
    135207         
    136     def runScript (self, script_name): 
    137         if script_name in self.scripts and self.scrpits[script_name]: 
    138             func, args, kwargs = self.scrpits[script_name] 
     208    def runScript (self, event): 
     209        """Runs the script for the given event""" 
     210        if event in self.scripts and self.scrpits[event]: 
     211            func, args, kwargs = self.scrpits[event] 
    139212            func (*args, **kwargs) 
     213             
     214    def setScript (self, event, func, args = [] , kwargs={}): 
     215        """Sets a script to be executed for the given event.""" 
     216        self.scripts[event] = (func, args, kwargs) 
    140217 
    141218class CharStats (object): 
     219    """Provides the object with character statistics""" 
    142220    def __init__ (self, **kwargs): 
    143221        self.is_charstats = True 
     
    146224class Wearable (object): 
    147225    def __init__ (self, **kwargs): 
     226        """Allows the object to be weared somewhere on the body (e.g. pants)""" 
    148227        self.is_wearable = True 
    149228        super(Wearable,self).__init__ (**kwargs) 
    150229     
    151230class Usable (object): 
     231    """Allows the object to be used in some way (e.g. a Zippo lighter to make a  
     232    fire) 
     233    """ 
    152234    def __init__ (self, **kwargs): 
    153235        self.is_usable = True 
     
    155237         
    156238class Weapon (object): 
     239    """Allows the object to be used as a weapon""" 
    157240    def __init__ (self, **kwargs): 
    158241        self.is_weapon = True 
     
    160243         
    161244class Destructable (object): 
     245    """Allows the object to be destroyed""" 
    162246    def __init__ (self, **kwargs): 
    163247        self.is_destructable = True 
     
    165249         
    166250class Trappable (object): 
     251    """Provides trap slots to the object""" 
    167252    def __init__ (self, **kwargs): 
    168253        self.is_trappable = True 
Note: See TracChangeset for help on using the changeset viewer.