Changeset 198


Ignore:
Timestamp:
07/09/09 23:19:35 (10 years ago)
Author:
tZee_parpg
Message:
  • changed objectLoader to do only what it is supposed to do
  • thus the engine can now add FIFE related references in an intermediate step to the object creation
  • which happens in the factory method, which is now located in scripts/objects/init
  • disregard the save/load mess for now
  • deleted some obsolete stuff
Location:
trunk/PARPG/scripts
Files:
2 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/PARPG/scripts/engine.py

    r189 r198  
    1818# there should be NO references to FIFE here! 
    1919import pickle, sys 
    20 from agents.hero import Hero 
    21 from agents.npc import NPC 
    22 from saver import Saver 
    2320from gamestate import GameState 
    2421from objects import * 
     
    5047        # a World object (the fife stuff, essentially) 
    5148        self.view = view 
    52         self.doors = {} 
    53         self.npcs = [] 
    54         self.PC = None 
    5549        self.mapchange = False 
    5650        self.gameState = GameState() 
     
    6054           other maps hanging around. 
    6155           @return: None""" 
    62         self.PC = None 
    63         self.npcs = [] 
    64         self.doors = {} 
    6556 
    6657    def save(self, path, filename): 
     
    6960           @param filename: the name of the file to write to 
    7061           @return: None""" 
    71         self.updateGameState() 
    7262        fname = '/'.join([path,filename]) 
    7363        try: 
     
    7666            sys.stderr.write("Error: Can't find save game: " + fname + "\n") 
    7767            return 
     68         
     69        # can't pickle SwigPyObjects 
     70        behaviours = {} 
     71        behaviours[self.gameState.PC.ID] = self.gameState.PC.behaviour; 
     72        self.gameState.PC.behaviour = None; 
     73         
     74        npcs = [npc for npc in self.gameState.objects.values() if npc.trueAttr("NPC")] 
     75        for npc in npcs: 
     76            behaviours[npc.ID] = npc.behaviour; 
     77            npc.behaviour = None; 
     78         
    7879        pickle.dump(self.gameState, f) 
    7980        f.close() 
     81         
     82        # restore behaviours 
     83        for npc in npcs: 
     84            npc.behaviour = behaviours[npc.ID]; 
     85        self.gameState.PC.behaviour = behaviours[self.gameState.PC.ID] 
    8086 
    8187    def load(self, path, filename): 
     
    94100        if self.gameState.currentMap: 
    95101            self.loadMap(self.gameState.currentMap) 
    96              
    97     def updateGameState(self): 
    98         """Stores the current pc and npcs positons in the game state.""" 
    99         # save the PC position 
    100         self.gameState.PC.posx = self.PC.getX() 
    101         self.gameState.PC.posy = self.PC.getY() 
    102         #save npc positions 
    103         for i in self.npcs: 
    104             if str(i.id) in self.gameState.objects: 
    105                 self.gameState.getObjectById(str(i.id)).posx = i.getX() 
    106                 self.gameState.getObjectById(str(i.id)).posy = i.getY() 
    107102 
    108103    def loadObjects(self, filename): 
     
    120115        # now open and read the XML file 
    121116        other_handler = ObjectXMLParser() 
    122         other_handler.getObjects(objects_file, self.view.agent_layer) 
     117        other_handler.getObjects(objects_file) 
    123118        objects_file.close() 
    124119             
    125120        # now add to the map and the engine 
    126         self.addGameObjs(other_handler.local_info) 
     121        self.createGameObjects(other_handler.local_info) 
    127122        return True 
    128123 
    129     def addGameObjs(self, objList): 
     124    def createGameObjects(self, objList): 
    130125        """Add all found game objects to the world 
    131126           @type objList: list 
    132127           @param objList: a list of the objects found in the xml file 
    133128           @return: None""" 
    134  
    135         self.addPC(*[obj for obj in objList if obj.trueAttr("PC")]) 
    136         self.addNPCs([obj for obj in objList if obj.trueAttr("NPC")]) 
    137         self.addObjects([obj for obj in objList if (not obj.trueAttr("PC") and not obj.trueAttr("NPC"))]) 
     129         
     130        # create the extra data 
     131        extra = {} 
     132        extra['agent_layer'] = self.view.agent_layer 
     133        extra['engine'] = self 
     134         
     135        # create the objects 
     136        for info in objList: 
     137            obj = createObject(info, extra) 
     138            if obj.trueAttr("PC"): 
     139                self.addPC(obj) 
     140            else: 
     141                self.addObject(obj) 
    138142 
    139143    def addPC(self,pc): 
     
    155159        self.gameState.PC.start() 
    156160 
    157     def addObjects(self,objects): 
    158         """Add all of the objects we found into the fife map 
    159            and into our class. An NPC is just an object to FIFE 
    160            @type objects: list 
    161            @param objects: List of objects to add 
    162            @return: None""" 
    163         for i in objects: 
    164             # already in game data? 
    165             ref = self.gameState.getObjectById(i.ID) 
    166             if ref is None: 
    167                 # no, add it to the game state 
    168                 i.map_id = self.gameState.currentMap 
    169                 self.gameState.objects[i.ID] = i 
    170             else: 
    171                 # yes, use the current game state data 
    172                 i.X = ref.X 
    173                 i.Y = ref.Y 
    174                 i.gfx = ref.gfx         
    175              
    176             self.view.addObject(i.X, i.Y, i.gfx, i.ID) 
    177  
    178     def addNPCs(self,npcs): 
    179         """Add all of the NPCs we found into the fife map to FIFE. 
     161    def addObject(self,obj): 
     162        """Adds an object to the game state. 
    180163           @type npcs: list 
    181164           @param npcs: List of NPC's to add 
    182165           @return: None""" 
    183         for i in npcs: 
    184             # already in the game data? 
    185             ref = self.gameState.getObjectById(i.ID)  
    186             if ref is None: 
    187                 # no, add it to the game state 
    188                 i.map_id = self.gameState.currentMap 
    189                 self.gameState.objects[i.ID] = i 
    190             else: 
    191                 # yes, use the current game state data 
    192                 i.X = ref.X 
    193                 i.Y = ref.Y 
    194                 i.gfx = ref.gfx   
    195                  
    196             # add it to the view 
    197             self.view.addObject(i.X, i.Y, i.gfx, i.ID)           
    198              
     166         
     167        ref = self.gameState.getObjectById(obj.ID)  
     168        if ref is None: 
     169            # no, add it to the game state 
     170            obj.map_id = self.gameState.currentMap 
     171            self.gameState.objects[obj.ID] = obj 
     172        else: 
     173            # yes, use the current game state data 
     174            obj.X = ref.X 
     175            obj.Y = ref.Y 
     176            obj.gfx = ref.gfx   
     177             
     178        # add it to the view 
     179        self.view.addObject(obj.X, obj.Y, obj.gfx, obj.ID)           
     180         
     181        if obj.trueAttr("NPC"): 
    199182            # create the agent 
    200             i.setup() 
     183            obj.setup() 
    201184             
    202185            # create the PC agent 
    203             i.start() 
     186            obj.start() 
    204187 
    205188    def addDoors(self, doors): 
  • trunk/PARPG/scripts/objectLoader.py

    r187 r198  
    1818from xml.sax import make_parser 
    1919from xml.sax.handler import ContentHandler 
    20 from objects import getAllObjects 
    21 from objects.containers import * 
    2220import sys 
    2321 
     
    3230        # parser is created when getObjects is called. 
    3331        self.parser = None 
    34         # an agent layer, which is set to something in getObjects and is set 
    35         # to None at the end of getObjects to ensure that it is always current. 
    36         self.agent_layer = None 
    3732     
    38     def getObjects(self, file_desc, a_layer): 
    39         """Gets the objects from the file. Populates local_info. This function 
    40            is how the scripts.Engine object interacts with the objectLoader. 
    41            So, this function takes the current agent_layer from the engine and 
    42            sets self.agent_layer so that it can be used in startElement. 
     33    def getObjects(self, file_desc): 
     34        """Reads the object data into dictionaries out of which 
     35        the game objects can be constructed. 
    4336           @type file_desc: File 
    4437           @param file_desc: an open file from which we read 
     
    4639        parser = make_parser() 
    4740        parser.setContentHandler(self) 
    48         self.agent_layer = a_layer 
    4941        parser.parse(file_desc) 
    5042        self.agent_layer = None 
     
    6052        # For now, only looking for game_obj things 
    6153        if str(name) == "object": 
    62             obj_info = dict(attrs.items()) 
     54            obj_info = {} 
    6355            # we need to convert all the unicode strings to ascii strings 
    6456            for key, val in attrs.items(): 
    65                 obj_info.pop(key) 
    6657                obj_info[str(key)] = str(val) 
    67             self.local_info.append(self.createObject(obj_info)) 
    68   
    69     def createObject(self, info): 
    70         """Called when we need to get an actual object.  
    71            @type info: dict 
    72            @param info: stores information about the object we want to create 
    73            @return: the object""" 
    74         # First, we try to get the type and ID, which every game_obj needs. 
    75         try: 
    76             obj_type = info.pop('type') 
    77             ID = info.pop('id') 
    78         except KeyError: 
    79             sys.stderr.write("Error: Game object missing type or id.") 
    80             sys.exit(False) 
    81          
    82         # add the agent_layer to the object dictionary in case it is needed by 
    83         # the object we are constructing. If it is not needed, it will be  
    84         # ignored 
    85         info['agent_layer'] = self.agent_layer 
    86  
    87         all_types = getAllObjects() 
    88         return all_types[obj_type](ID, **info) 
     58            self.local_info.append(obj_info) 
  • trunk/PARPG/scripts/objects/__init__.py

    r187 r198  
    1717import containers 
    1818import actors 
     19import sys 
    1920 
    2021object_modules = [containers, actors,] 
     
    2930             
    3031    return result 
     32 
     33def createObject(info, extra = {}): 
     34        """Called when we need to get an actual object.  
     35           @type info: dict 
     36           @param info: stores information about the object we want to create 
     37           @type extra: dict 
     38           @param extra: stores additionally required attributes, like agent layer, engine etc. 
     39           @return: the object""" 
     40        # First, we try to get the type and ID, which every game_obj needs. 
     41        try: 
     42            obj_type = info.pop('type') 
     43            ID = info.pop('id') 
     44        except KeyError: 
     45            sys.stderr.write("Error: Game object missing type or id.") 
     46            sys.exit(False) 
     47         
     48        # add the extra info 
     49        for key, val in extra.items(): 
     50            info[key] = val 
     51 
     52        return getAllObjects()[obj_type](ID, **info) 
  • trunk/PARPG/scripts/objects/actors.py

    r197 r198  
    2929    """Fife agent listener 
    3030    """ 
    31     def __init__(self): 
     31    def __init__(self, Layer): 
    3232        fife.InstanceActionListener.__init__(self) 
    33      
    34     def attachToLayer(self, fifeLayer, agentID): 
     33        self.layer = Layer 
     34     
     35    def attachToLayer(self, agentID): 
    3536        # init listener 
    36         self.agent = fifeLayer.getInstance(agentID) 
     37        self.agent = self.layer.getInstance(agentID) 
    3738        self.agent.addActionListener(self) 
    3839        self.state = _AGENT_STATE_NONE 
     
    4748    def getY(self): 
    4849        """Get the NPC's y position on the map. 
    49 s           @rtype: integer 
     50           @rtype: integer 
    5051           @return: the y coordinate of the NPC's location""" 
    5152        return self.agent.getLocation().getLayerCoordinates().y 
     
    5657     
    5758class PCBehaviour (ActorBehaviour): 
    58     def __init__(self, Parent = None, Engine = None): 
    59         super(PCBehaviour, self).__init__() 
     59    def __init__(self, Parent = None, Layer = None, Engine = None): 
     60        super(PCBehaviour, self).__init__(Layer) 
    6061         
    6162        self.parent = Parent 
     
    112113        super(PlayerCharacter, self).__init__(ID, **kwargs) 
    113114        self.is_PC = True 
    114         self.agent_layer = agent_layer 
    115115         
    116116        # PC _has_ an inventory, he _is not_ one 
    117117        self.inventory = None 
    118         self.engine = engine 
    119118         
    120119        self.state = _AGENT_STATE_NONE 
    121         self.behaviour = PCBehaviour(self, engine) 
     120        self.behaviour = PCBehaviour(self, agent_layer, engine) 
    122121     
    123122    def setup(self): 
    124123        """@return: None""" 
    125         self.behaviour.attachToLayer(self.agent_layer, self.ID) 
     124        self.behaviour.attachToLayer(self.ID) 
    126125 
    127126    def start(self): 
     
    201200 
    202201class NPCBehaviour(ActorBehaviour): 
    203     def __init__(self, Parent = None): 
    204         super(NPCBehaviour, self).__init__() 
     202    def __init__(self, Parent = None, Layer = None): 
     203        super(NPCBehaviour, self).__init__(Layer) 
    205204         
    206205        self.parent = Parent 
     
    273272        self.is_NPC = True 
    274273        self.inventory = None 
    275         self.agent_layer = agent_layer 
    276          
    277         self.behaviour = NPCBehaviour(self) 
     274         
     275        self.behaviour = NPCBehaviour(self, agent_layer) 
    278276 
    279277    def getLocation(self): 
     
    305303    def setup(self): 
    306304        """@return: None""" 
    307         self.behaviour.attachToLayer(self.agent_layer, self.ID) 
     305        self.behaviour.attachToLayer(self.ID) 
    308306 
    309307    def start(self): 
  • trunk/PARPG/scripts/world.py

    r197 r198  
    2222from scripts.common.eventlistenerbase import EventListenerBase 
    2323from loaders import loadMapFile 
    24 from agents.hero import Hero 
    25 from agents.npc import NPC 
    2624from sounds import SoundEngine 
    2725from settings import Setting 
Note: See TracChangeset for help on using the changeset viewer.