Changeset 253 for trunk/game


Ignore:
Timestamp:
08/18/09 17:24:58 (10 years ago)
Author:
tZee_parpg
Message:

#65 Exported map data in a separate class. Next is to sort out the loading strategy (right now the call goes through the engine, then to the world and then to the map class) and the ownership of objects like the cameras.

Location:
trunk/game/scripts
Files:
1 added
2 edited

Legend:

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

    r225 r253  
    131131        # create the extra data 
    132132        extra = {} 
    133         extra['agent_layer'] = self.view.agent_layer 
     133        extra['agent_layer'] = self.view.activeMap.agent_layer 
    134134        extra['engine'] = self 
    135135         
     
    148148           @return: None""" 
    149149        # add to view data     
    150         self.view.addObject(pc.X, pc.X, pc.gfx, pc.ID) 
     150        self.view.activeMap.addObject(pc.X, pc.X, pc.gfx, pc.ID) 
    151151         
    152152        # sync with game data 
     
    155155             
    156156        self.gameState.PC.setup() 
    157         self.view.addPC(self.gameState.PC.behaviour.agent) 
     157        self.view.activeMap.addPC(self.gameState.PC.behaviour.agent) 
    158158             
    159159        # create the PC agent 
     
    178178             
    179179        # add it to the view 
    180         self.view.addObject(obj.X, obj.Y, obj.gfx, obj.ID)           
     180        self.view.activeMap.addObject(obj.X, obj.Y, obj.gfx, obj.ID)           
    181181         
    182182        if obj.trueAttr("NPC"): 
     
    255255        self.gameState.PC.approach([npc.getLocation().getLayerCoordinates().x, npc.getLocation().getLayerCoordinates().y]) 
    256256 
    257     def loadMap(self, map_file): 
     257    def loadMap(self, map_name, map_file): 
    258258        """Load a new map. TODO: needs some error checking 
    259259           @type map_file: string 
     
    261261           @return: None""" 
    262262        # then we let FIFE load the rest of the map 
    263         self.view.load(str(map_file)) 
     263        self.view.loadMap(map_name, str(map_file)) 
     264        self.view.setActiveMap(map_name) 
    264265        # then we update FIFE with the PC, NPC and object details 
    265266        self.reset() 
  • trunk/game/scripts/world.py

    r225 r253  
    1818import fife, time 
    1919import pychan 
     20from sounds import SoundEngine 
    2021from scripts.parpgfilebrowser import PARPGFileBrowser 
    2122from datetime import date 
    2223from scripts.common.eventlistenerbase import EventListenerBase 
    23 from loaders import loadMapFile 
    24 from sounds import SoundEngine 
    2524from settings import Setting 
    2625from scripts import inventory, hud 
     
    2827from scripts.context_menu import ContextMenu 
    2928from pychan.tools import callbackWithArguments as cbwa 
     29from map import Map 
    3030 
    3131TDS = Setting() 
     
    3939# however, any logic needed to resolve this should sit in engine.py 
    4040 
    41 class Map(fife.MapChangeListener): 
    42     """Map class used to flag changes in the map""" 
    43     def __init__(self, fife_map): 
    44         fife.MapChangeListener.__init__(self) 
    45         self.map = fife_map 
    46  
    4741class World(EventListenerBase): 
    4842    """World holds the data needed by fife to render the engine 
     
    5751        self.engine = engine 
    5852        self.eventmanager = engine.getEventManager() 
    59         self.model = engine.getModel() 
    60         self.view = self.engine.getView() 
    6153        self.quitFunction = None 
    62         self.inventoryShown = False 
    63         self.agent_layer = None 
    64         self.cameras = {} 
     54        self.inventoryShown = False  
     55         
    6556        # self.data is an engine.Engine object, but is set in run.py 
    6657        self.data = None 
     
    6859        self.obj_hash={} 
    6960        # self.map is a Map object, set to none here 
    70         self.map = None 
     61        self.activeMap = None 
     62        self.maps = {} 
    7163        self.hud = hud.Hud(self.engine, TDS) 
    7264        self.hud.events_to_map["inventoryButton"] = cbwa(self.displayInventory, True) 
     
    9082        self.inventory.inventory.mapEvents(self.inventory.events_to_map) 
    9183        self.refreshReadyImages() 
    92         # init the sound (don't start playing yet) 
    93         self.sounds = SoundEngine(self.engine) 
    9484         
    9585        self.context_menu = ContextMenu (self.engine, [], (0,0)) 
     
    9787        self.boxOpen = False 
    9888        self.boxCreated = False 
    99  
    100     def reset(self): 
    101         """Reset the data to default settings. 
    102            @return: None""" 
    103         # We have to delete the map in Fife. 
    104         # TODO: We're killing the PC now, but later we will have to save the PC 
    105         if self.map: 
    106             self.model.deleteObjects() 
    107             self.model.deleteMap(self.map) 
    108         self.transitions = [] 
    109         self.map,self.agent_layer = None,None 
    110         # We have to clear the cameras in the view as well, or we can't reuse 
    111         # camera names like 'main' 
    112         self.view.clearCameras() 
    113         self.cameras = {} 
    114         self.cur_cam2_x,self.initial_cam2_x,self.cam2_scrolling_right = 0,0,True 
    115         self.target_rotation = 0 
    116  
    117     def load(self, filename): 
    118         """Load a map given the filename. 
    119            @type filename: string 
    120            @param filename: Name of map to load 
    121            @return: None""" 
    122         self.reset() 
    123         # some messy code to handle music changes when we enter a new map 
    124         if(self.sounds.music_on == True): 
    125             self.sounds.pauseMusic() 
    126             unpause = True 
    127         else: 
    128             unpause = False 
    129         self.map = loadMapFile(filename, self.engine) 
    130         self.maplistener = Map(self.map) 
    131         # there must be a PC object on the objects layer! 
    132         self.agent_layer = self.map.getLayer('ObjectLayer') 
    133         # it's possible there's no transition layer 
    134         size = len('TransitionLayer') 
    135         for layer in self.map.getLayers(): 
    136             # could be many layers, but hopefully no more than 3 
    137             if(layer.getId()[:size] == 'TransitionLayer'): 
    138                 self.transitions.append(self.map.getLayer(layer.getId())) 
    139         # init the camera 
    140         for cam in self.view.getCameras(): 
    141             self.cameras[cam.getId()] = cam 
    142         self.view.resetRenderers() 
    143         self.target_rotation = self.cameras['main'].getRotation() 
    144         self.cord_render = self.cameras['main'].getRenderer('CoordinateRenderer') 
    145         self.outline_render = fife.InstanceRenderer.getInstance(self.cameras['main']) 
    146         # set the render text 
    147         rend = fife.FloatingTextRenderer.getInstance(self.cameras['main']) 
    148         text = self.engine.getGuiManager().createFont('fonts/rpgfont.png', 
    149                                                        0, str(TDS.readSetting("FontGlyphs", strip=False))) 
    150         rend.changeDefaultFont(text) 
    151         # start playing the music 
    152         # TODO: remove hard coding by putting this in the level data 
     89         
     90        # init the sound 
     91        self.sounds = SoundEngine(engine) 
     92         
    15393        # don't force restart if skipping to new section 
    15494        if (TDS.readSetting("PlaySounds") == "1"): 
    15595            if(self.sounds.music_init == False): 
    15696                self.sounds.playMusic("/music/preciouswasteland.ogg") 
    157             elif(unpause == True): 
    158                 self.sounds.playMusic() 
    159  
    160     def addPC(self, agent): 
    161         """Add the player character to the map 
    162            @type agent: Fife.instance 
    163            @param : The object to use as the PC sprite 
    164            @return: None""" 
    165         # actually this is real easy, we just have to 
    166         # attach the main camera to the PC 
    167         self.cameras['main'].attach(agent) 
    168  
    169     def addObject(self, xpos, ypos, gfx, name): 
    170         """Add an object or an NPC to the map. 
    171            It makes no difference to fife which is which. 
    172            @type xpos: integer 
    173            @param xpos: x position of object 
    174            @type ypos: integer 
    175            @param ypos: y position of object 
    176            @type gfx: string 
    177            @param gfx: name of gfx image 
    178            @type name: string 
    179            @param name: name of object 
    180            @return: None""" 
    181         obj = self.agent_layer.createInstance( 
    182                 self.model.getObject(str(gfx), "PARPG"), 
    183                 fife.ExactModelCoordinate(float(xpos), float(ypos), 0.0), str(name)) 
    184         obj.setRotation(0) 
    185         fife.InstanceVisual.create(obj) 
    186         # save it for later use 
    187         self.obj_hash[name]=obj 
     97                 
     98    def loadMap(self, mapname, filename): 
     99        """Loads a map an stores it under the given name in the maps list. 
     100        """ 
     101        map = Map(self.engine) 
     102        map.load(filename) 
     103        self.maps[mapname] = map 
     104     
     105    def setActiveMap(self, mapname): 
     106        """Sets the active map that is to be rendered. 
     107        """ 
     108        self.activeMap = self.maps[mapname] 
     109        self.activeMap.makeActive() 
    188110 
    189111    def displayObjectText(self, obj, text): 
     
    301223        elif(evt.getButton() == fife.MouseEvent.RIGHT): 
    302224            # is there an object here? 
    303             instances = self.cameras['main'].getMatchingInstances(scr_point, self.agent_layer) 
     225            instances = self.activeMap.cameras['main'].getMatchingInstances(scr_point, self.activeMap.agent_layer) 
    304226            info = None 
    305227            for inst in instances: 
     
    327249           @return: None""" 
    328250        click = fife.ScreenPoint(evt.getX(), evt.getY()) 
    329         i=self.cameras['main'].getMatchingInstances(click, self.agent_layer) 
     251        i=self.activeMap.cameras['main'].getMatchingInstances(click, self.activeMap.agent_layer) 
    330252        # no object returns an empty tuple 
    331253        if(i != ()): 
     
    334256                if(self.data.objectActive(obj.getId())): 
    335257                    # yes, so outline     
    336                     self.outline_render.addOutlined(obj, 0, 137, 255, 2) 
     258                    self.activeMap.outline_render.addOutlined(obj, 0, 137, 255, 2) 
    337259                    # get the text 
    338260                    item = self.data.objectActive(obj.getId()) 
     
    341263        else: 
    342264            # erase the outline 
    343             self.outline_render.removeAllOutlines() 
     265            self.activeMap.outline_render.removeAllOutlines() 
    344266 
    345267    def getCoords(self, click): 
     
    349271           @rtype: fife.Location 
    350272           @return: The map co-ords""" 
    351         coord = self.cameras["main"].toMapCoordinates(click, False) 
     273        coord = self.activeMap.cameras["main"].toMapCoordinates(click, False) 
    352274        coord.z = 0 
    353         location = fife.Location(self.agent_layer) 
     275        location = fife.Location(self.activeMap.agent_layer) 
    354276        location.setMapCoordinates(coord) 
    355277        return location 
    356  
    357     def toggle_renderer(self, r_name): 
    358         """Enable or disable the grid renderer. 
    359            @return: None""" 
    360         renderer = self.cameras['main'].getRenderer('GridRenderer') 
    361         renderer.setEnabled(not renderer.isEnabled()) 
    362278 
    363279    def clearMenu(self): 
Note: See TracChangeset for help on using the changeset viewer.