source: trunk/game/scripts/map.py @ 286

Revision 264, 4.5 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket #72: Patch by Kaydeth. The separate objects file was merged back into the main map file that FIFE parses to load each Map. The Map, Engine, and World classes were all updated to allow game object creation to happen as the map file is loaded. loaders.py and xmlmap.py were copied from the FIFE code base and made local so that we can now customize how we pass the parsed map and object information to FIFE.

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import fife, time
19from local_loaders.loaders import loadMapFile
20from scripts.common.eventlistenerbase import EventListenerBase
21
22from settings import Setting
23TDS = Setting()
24
25class Map(fife.MapChangeListener):
26    """Map class used to flag changes in the map"""
27    def __init__(self, engine, data):
28        # init mapchange listener
29        fife.MapChangeListener.__init__(self)
30        self.map = None
31        self.engine = engine
32        self.data = data
33       
34        # init map attributes
35        self.cameras = {}
36        self.agent_layer = None
37        self.model = engine.getModel()
38        self.view = engine.getView()
39        self.transitions = []
40        self.obj_hash = {}
41        self.cur_cam2_x, self.initial_cam2_x, self.cam2_scrolling_right = 0,0,True
42        self.target_rotation = 0
43        self.outline_renderer = None
44       
45    def reset(self):
46        """Reset the data to default settings.
47           @return: None"""
48        # We have to delete the map in Fife.
49        # TODO: We're killing the PC now, but later we will have to save the PC
50        if self.map:
51            self.model.deleteObjects()
52            self.model.deleteMap(self.map)
53        self.transitions = []
54        self.obj_hash = {}
55        self.map,self.agent_layer = None,None
56        # We have to clear the cameras in the view as well, or we can't reuse
57        # camera names like 'main'
58        self.view.clearCameras()
59        self.cameras = {}
60        self.cur_cam2_x,self.initial_cam2_x,self.cam2_scrolling_right = 0,0,True
61        self.target_rotation = 0
62        self.outline_renderer = None
63       
64    def makeActive(self):
65        """Makes this map the active one.
66        """
67        pass
68       
69    def load(self, filename):
70        """Load a map given the filename.
71           @type filename: string
72           @param filename: Name of map to load
73           @return: None"""
74        self.reset()
75        self.map = loadMapFile(filename, self.engine, self.data)
76         
77        # there must be a PC object on the objects layer!
78        self.agent_layer = self.map.getLayer('ObjectLayer')
79       
80        # it's possible there's no transition layer
81        size = len('TransitionLayer')
82        for layer in self.map.getLayers():
83            # could be many layers, but hopefully no more than 3
84            if(layer.getId()[:size] == 'TransitionLayer'):
85                self.transitions.append(self.map.getLayer(layer.getId()))
86               
87        # init the camera
88        for cam in self.view.getCameras():
89            self.cameras[cam.getId()] = cam
90        self.view.resetRenderers()
91        self.target_rotation = self.cameras['main'].getRotation()
92       
93        self.outline_render = fife.InstanceRenderer.getInstance(self.cameras['main'])
94       
95        # set the render text
96        rend = fife.FloatingTextRenderer.getInstance(self.cameras['main'])
97        text = self.engine.getGuiManager().createFont('fonts/rpgfont.png',
98                                                       0, str(TDS.readSetting("FontGlyphs", strip=False)))
99        rend.changeDefaultFont(text)
100               
101    def addPC(self, agent):
102        """Add the player character to the map
103           @type agent: Fife.instance of PC
104           @return: None"""
105        # actually this is real easy, we just have to
106        # attach the main camera to the PC
107        self.cameras['main'].attach(agent)
108
109    def addObject(self, name, obj):
110        """Add an object to this map0
111            Inputs:
112                name - ID of object
113                obj - FIFE instance of object
114            Return:
115                Nothing
116        """
117        # save it for later use
118        self.obj_hash[name]=obj
119       
120    def toggle_renderer(self, r_name):
121        """Enable or disable a renderer.
122           @return: None"""
123        renderer = self.cameras['main'].getRenderer(str(r_name))
124        renderer.setEnabled(not renderer.isEnabled())
Note: See TracBrowser for help on using the repository browser.