Ignore:
Timestamp:
06/19/10 23:03:06 (9 years ago)
Author:
beliar
Message:

Merged changes from code-cleanup-and-refactoring back into the trunk.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/game/scripts/objects/action.py

    r477 r553  
    2424class ChangeMapAction(Action): 
    2525    """A change map scheduled""" 
    26     def __init__(self, engine, target_map_name, target_map_file , target_pos): 
     26    def __init__(self, model, view, target_map_name, target_map_file , target_pos): 
    2727        """Initiates a change of the position of the character 
    2828           possibly flagging a new map to be loaded. 
    29            @type engine: Engine reference 
    30            @param engine: A reference to the engine. 
     29           @type model: scripts.GameModel 
     30           @param model: A reference to the game model. 
     31           @type view: class derived from scripts.ViewBase 
     32           @param view: The view 
    3133           @type target_map_name: String 
    3234           @param target_map_name: Target map id  
     
    3638           @param target_pos: (X, Y) coordinates on the target map. 
    3739           @return: None""" 
    38         self.engine = engine 
     40        Action.__init__(self) 
     41        self.model = model 
     42        self.view = view 
    3943        self.target_pos = target_pos 
    4044        self.target_map_name = target_map_name 
     
    4347    def execute(self): 
    4448        """Executes the map change.""" 
    45         self.engine.changeMap(self.target_map_name, self.target_map_file,\ 
     49        self.model.changeMap(self.target_map_name, self.target_map_file,\ 
    4650                              self.target_pos) 
    4751        
    4852class OpenBoxAction(Action): 
    4953    """Open a box. Needs to be more generic, but will do for now.""" 
    50     def __init__(self, engine, container): 
    51         """@type engine: Engine reference 
    52            @param engine: A reference to the engine. 
     54    def __init__(self, model, view, container): 
     55        """@type model: Engine reference 
     56           @param model: A reference to the model. 
     57           @type view: class derived from scripts.ViewBase 
     58           @param view: The view 
    5359           @param container: A reference to the container""" 
    54         self.engine = engine 
     60        Action.__init__(self) 
     61        self.model = model 
     62        self.view = view 
    5563        self.container = container 
    5664         
     
    5967        try: 
    6068            self.container.open() 
    61             self.engine.view.hud.createBoxGUI(self.container.name, \ 
     69            self.view.hud.createBoxGUI(self.container.name, \ 
    6270                                              self.container) 
    6371 
    6472        except ValueError: 
    65             self.engine.view.hud.createExamineBox(self.container.name, \ 
     73            self.view.hud.createExamineBox(self.container.name, \ 
    6674                                                  "The container is locked") 
    6775         
     
    7078    def __init__(self,  container): 
    7179        """@param container: A reference to the container""" 
     80        Action.__init__(self) 
    7281        self.container = container 
    7382     
     
    8089    def __init__(self, container): 
    8190        """@param container: A reference to the container""" 
     91        Action.__init__(self) 
    8292        self.container = container 
    8393         
     
    8999class ExamineBoxAction(Action): 
    90100    """Examine a box. Needs to be more generic, but will do for now.""" 
    91     def __init__(self, engine, examine_name, examine_desc): 
    92         """@type engine: Engine reference 
    93            @param engine: A reference to the engine. 
     101    def __init__(self, model, view, examine_name, examine_desc): 
     102        """@type model: Engine reference 
     103           @param model: A reference to the model. 
     104           @type view: class derived from scripts.ViewBase 
     105           @param view: The view 
    94106           @type examine_name: String 
    95107           @param examine_name: Name of the object to be examined. 
    96108           @type examine_name: String 
    97109           @param examine_name: Description of the object to be examined.""" 
    98         self.engine = engine 
     110        Action.__init__(self) 
     111        self.model = model 
     112        self.view = view 
    99113        self.examine_name = examine_name 
    100114        self.examine_desc = examine_desc 
     
    102116    def execute(self): 
    103117        """Examine the box.""" 
    104         self.engine.view.hud.createExamineBox(self.examine_name, \ 
     118        self.view.hud.createExamineBox(self.examine_name, \ 
    105119                                              self.examine_desc) 
    106120 
    107121class TalkAction(Action): 
    108122    """An action to represent starting a dialogue""" 
    109     def __init__(self, engine, npc): 
    110         """@type engine: Engine reference 
    111            @param engine: A reference to the engine. 
     123    def __init__(self, model, view, npc): 
     124        """@type model: Engine reference 
     125           @param model: A reference to the model. 
     126           @type view: class derived from scripts.ViewBase 
     127           @param view: The view 
    112128           @type npc: NonPlayerCharacter 
    113129           @param npc: NPC to interact with.""" 
    114         self.engine = engine 
     130        Action.__init__(self) 
     131        self.model = model 
     132        self.view = view 
    115133        self.npc = npc 
    116134         
     
    119137           @return: None""" 
    120138       
    121         pc = self.engine.game_state.PC 
     139        pc = self.model.game_state.PlayerCharacter 
    122140        npc_coordinates = self.npc.getLocation().getLayerCoordinates() 
    123141        pc_coordinates = pc.behaviour.agent.getLocation().getLayerCoordinates() 
     
    134152                         self.npc.getLocation().\ 
    135153                         getLayerCoordinates().y], \ 
    136                         TalkAction(self.engine,\ 
     154                        TalkAction(self.model,\ 
    137155                                   self.npc))         
    138156        else: 
     
    141159            if self.npc.dialogue is not None: 
    142160                self.npc.talk(pc) 
    143                 self.engine.view.hud.showDialogue(self.npc) 
     161                self.view.hud.showDialogue(self.npc) 
    144162            else: 
    145163                self.npc.behaviour.agent.say("Leave me alone!", 1000) 
    146164                 
    147             self.engine.game_state.PC.behaviour.idle() 
    148             self.engine.game_state.PC.nextAction = None 
     165            self.model.game_state.PlayerCharacter.behaviour.idle() 
     166            self.model.game_state.PlayerCharacter.nextAction = None 
  • trunk/game/scripts/objects/actors.py

    r542 r553  
    1717 
    1818from fife import fife 
    19 from scripts import world 
    20 from base import * 
    21 from composed import * 
     19from base import GameObject, Living, Scriptable, CharStats 
     20from fife.extensions.fife_settings import Setting 
     21from composed import CarryableItem 
    2222from scripts.inventory import Inventory 
     23from random import randrange 
    2324 
    2425"""All actors go here. Concrete classes only.""" 
     
    3637        fife.InstanceActionListener.__init__(self) 
    3738        self.layer = layer 
     39        self.agent = None 
     40        self.state = None 
     41        self.speed = 0 
    3842     
    3943    def attachToLayer(self, agent_ID): 
     
    115119 
    116120        self.is_PC = True 
     121        self.behaviour = None 
    117122         
    118123        # PC _has_ an inventory, he _is not_ one 
     
    218223        self.parent = Parent 
    219224        self.state = _AGENT_STATE_NONE 
     225        self.pc = None 
     226        self.target_loc = None 
    220227         
    221228        # hard code this for now 
     
    286293 
    287294        self.is_NPC = True 
     295        self.behaviour = None 
     296         
    288297        if inventory == None: 
    289298            self.inventory = Inventory() 
     
    297306        self.createBehaviour(agent_layer)         
    298307        self.dialogue = kwargs.get('dialogue') 
    299  
     308         
    300309    def prepareStateForSaving(self, state): 
    301310        """Prepares state for saving 
  • trunk/game/scripts/objects/base.py

    r542 r553  
    2222   Some rules that should be followed when CREATING base property classes: 
    2323    
    24    1. If you want to support some custom initialization arguments, always define  
    25       them as keyword ones. Only GameObject would use positional arguments. 
     24   1. If you want to support some custom initialization arguments,  
     25      always define them as keyword ones. Only GameObject would use  
     26      positional arguments. 
    2627   2. In __init__() **ALWAYS** call the parent's __init__(**kwargs), preferably  
    2728      *at the end* of your __init__() (makes it easier to follow) 
     
    3839         
    3940 
    40    Some rules are to be followed when USING the base classes to make composed ones: 
     41   Some rules are to be followed when USING the base classes to make composed  
     42   ones: 
    4143 
    4244   1. The first parent should always be the base GameObject class 
     
    5153           super(TinCan,self).__init__ (*args, **kwargs) 
    5254           self.name = 'Tin Can'""" 
    53             
    54 from fife import fife 
    55 from fife.extensions.fife_settings import Setting 
    56 from random import randrange 
    57 from scripts.gui.popups import ExaminePopup, ContainerGUI 
    58  
     55          
    5956class DynamicObject (object): 
    6057    """A base class that only supports dynamic attributes functionality""" 
    61     def __init__ (self, name="Dynamic object", image=None, engine = None, **kwargs): 
     58    def __init__ (self, name="Dynamic object", image=None, **kwargs): 
    6259        """Initialise minimalistic set of data 
    6360           @type name: String 
     
    6764        self.name = name 
    6865        self.image = image 
    69         self.engine = engine 
    7066 
    7167    def prepareStateForSaving(self, state): 
     
    7470        @param state: State of the object   
    7571        """ 
    76         del state["engine"] 
     72        pass 
    7773     
    7874    def restoreState(self, state): 
     
    10096    """A base class to be inherited by all game objects. This must be the 
    10197       first class (left to right) inherited by any game object.""" 
    102     def __init__ (self, ID, gfx = {}, xpos = 0.0, ypos = 0.0, map_id = None,  
     98    def __init__ (self, ID, gfx = None, xpos = 0.0, ypos = 0.0, map_id = None,  
    10399                  blocking=True, name="Generic object", text="Item description", 
    104100                  desc="Detailed description", **kwargs): 
     
    123119        DynamicObject.__init__(self, name, **kwargs) 
    124120        self.ID = ID 
    125         self.gfx = gfx 
     121        self.gfx = gfx or {} 
    126122        self.X = xpos 
    127123        self.Y = ypos 
     
    140136         
    141137    coords = property (_getCoords, _setCoords,  
    142         doc = "Property allowing you to get and set the obejct's coordinates via tuples") 
     138        doc = "Property allowing you to get and set the obejct's \ 
     139                coordinates via tuples") 
    143140     
    144141    def __repr__(self): 
     
    146143        return "<%s:%s>" % (self.name, self.ID) 
    147144 
    148 class Openable(object): 
     145class Scriptable (object): 
     146    """Allows objects to have predefined scripts executed on certain events""" 
     147    def __init__ (self, scripts = None, **kwargs): 
     148        """Init operation for scriptable objects 
     149           @type scripts: Dictionary 
     150           @param scripts: Dictionary where the event strings are keys. The  
     151           values are 3-item tuples (function, positional_args, keyword_args)""" 
     152        self.is_scriptable = True 
     153        self.scripts = scripts or {} 
     154         
     155    def runScript (self, event): 
     156        """Runs the script for the given event""" 
     157        if event in self.scripts and self.scripts[event]: 
     158            func, args, kwargs = self.scripts[event] 
     159            func (*args, **kwargs) 
     160             
     161    def setScript (self, event, func, args = None , kwargs = None): 
     162        """Sets a script to be executed for the given event.""" 
     163        args = args or {} 
     164        kwargs = kwargs or {} 
     165        self.scripts[event] = (func, args, kwargs) 
     166 
     167class Openable(DynamicObject, Scriptable): 
    149168    """Adds open() and .close() capabilities to game objects 
    150169       The current state is tracked by the .is_open variable""" 
     
    153172           @type is_open: Boolean 
    154173           @param is_open: Keyword boolean argument sets the initial state.""" 
     174        DynamicObject.__init__(self, **kwargs) 
     175        Scriptable.__init__(self, **kwargs) 
    155176        self.is_openable = True 
    156177        self.is_open = is_open 
     
    187208        self.locked = locked 
    188209        if locked : 
    189             is_open=False 
     210            is_open = False 
    190211        Openable.__init__( self, is_open, **kwargs ) 
    191212         
     
    204225        if self.locked: 
    205226            raise ValueError ("Open failed: object locked") 
    206         super (Lockable,self).open(*args,**kwargs) 
    207          
    208 class Carryable (object): 
     227        super (Lockable, self).open(*args, **kwargs) 
     228         
     229class Carryable (DynamicObject): 
    209230    """Allows objects to be stored in containers""" 
    210231    def __init__ (self, weight=0.0, bulk=0.0, **kwargs): 
     232        DynamicObject.__init__(self, **kwargs) 
    211233        self.is_carryable = True 
    212234        self.in_container = None 
    213235        self.weight = weight 
    214         self.bulk=bulk 
     236        self.bulk = bulk 
    215237 
    216238    def getInventoryThumbnail(self): 
     239        """Returns the inventory thumbnail of the object""" 
    217240        # TODO: Implement properly after the objects database is in place 
    218241        if self.image == None: 
     
    221244            return self.image 
    222245     
    223 class Container (object): 
     246class Container (DynamicObject, Scriptable): 
    224247    """Gives objects the capability to hold other objects""" 
    225248    class TooBig(Exception): 
     
    233256 
    234257    def __init__ (self, capacity = 0, **kwargs): 
     258        DynamicObject.__init__(self, **kwargs) 
     259        Scriptable.__init__(self, **kwargs) 
    235260        self.is_container = True 
    236261        self.items = {} 
    237         self.capacity=capacity 
     262        self.capacity = capacity 
    238263         
    239264    def placeItem (self, item, index=None): 
     
    243268            raise TypeError ('%s is not carriable!' % item) 
    244269        if self.capacity and self.getContentsBulk()+item.bulk > self.capacity: 
    245             raise self.TooBig ('%s is too big to fit into %s' % (item,self)) 
     270            raise self.TooBig ('%s is too big to fit into %s' % (item, self)) 
    246271        item.in_container = self 
    247272        if index == None: 
     
    249274        else: 
    250275            if index in self.items : 
    251                 raise self.SlotBusy('Slot %d is busy in %s' % (index, self.name)) 
    252             self.items[index]=item 
     276                raise self.SlotBusy('Slot %d is busy in %s' % (index,  
     277                                                               self.name)) 
     278            self.items[index] = item 
    253279 
    254280        # Run any scripts associated with storing an item in the container 
     
    260286 
    261287    def placeAtVacant(self, item): 
     288        """Places an item at a vacant slot""" 
    262289        vacant = None 
    263290        for i in range(len(self.items)): 
     
    284311 
    285312    def count (self): 
     313        """Returns the number of items""" 
    286314        return len(self.items) 
    287315 
     
    291319 
    292320    def findItemByID(self, ID): 
     321        """Returns the item with the passed ID""" 
    293322        for i in self.items : 
    294323            if self.items[i].ID == ID: 
     
    312341         
    313342class Living (object): 
     343    """Objects that 'live'""" 
    314344    def __init__ (self, **kwargs): 
    315345        self.is_living = True 
    316346 
    317347    def die(self): 
    318         self.is_living = False 
    319          
    320 class Scriptable (object): 
    321     """Allows objects to have predefined scripts executed on certain events""" 
    322     def __init__ (self, scripts = {}, **kwargs): 
    323         """Init operation for scriptable objects 
    324            @type scripts: Dictionary 
    325            @param scripts: Dictionary where the event strings are keys. The  
    326            values are 3-item tuples (function, positional_args, keyword_args)""" 
    327         self.is_scriptable = True 
    328         self.scripts = scripts  
    329          
    330     def runScript (self, event): 
    331         """Runs the script for the given event""" 
    332         if event in self.scripts and self.scripts[event]: 
    333             func, args, kwargs = self.scripts[event] 
    334             func (*args, **kwargs) 
    335              
    336     def setScript (self, event, func, args = [] , kwargs={}): 
    337         """Sets a script to be executed for the given event.""" 
    338         self.scripts[event] = (func, args, kwargs) 
     348        """Kills the object""" 
     349        self.is_living = False    
    339350 
    340351class CharStats (object): 
     
    344355         
    345356class Wearable (object): 
     357    """Objects than can be weared""" 
    346358    def __init__ (self, slots, **kwargs): 
    347359        """Allows the object to be worn somewhere on the body (e.g. pants)""" 
    348360        self.is_wearable = True 
    349         if isinstance(slots,tuple) : 
     361        if isinstance(slots, tuple) : 
    350362            self.slots = slots 
    351363        else : 
  • trunk/game/scripts/objects/composed.py

    r477 r553  
    1818"""Composite game object classes are kept here""" 
    1919 
    20 from base import * 
     20from base import GameObject, Container, Lockable, \ 
     21                Scriptable, Trappable, Destructable, Carryable 
    2122 
    2223class ImmovableContainer(GameObject, Container, Lockable, Scriptable,  
     
    3233        self.blocking = True 
    3334 
    34 class CarryableContainer(DynamicObject, Container, Carryable): 
     35class CarryableContainer(Container, Carryable): 
    3536    """Composite class that will be used for backpack, pouches, etc.""" 
    3637    def __init__ (self, **kwargs): 
    37         DynamicObject.__init__(self, **kwargs) 
    38         Container.__init__(self,**kwargs) 
    39         Carryable.__init__(self,**kwargs) 
     38        Container.__init__(self, **kwargs) 
     39        Carryable.__init__(self, **kwargs) 
     40        self.own_bulk = 0 
     41        self.own_weight = 0 
    4042 
    4143    def getWeight(self): 
Note: See TracChangeset for help on using the changeset viewer.