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

Revision 253, 5.1 KB checked in by tZee_parpg, 10 years ago (diff)

#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.

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 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):
28        # init mapchange listener
29        fife.MapChangeListener.__init__(self)
30        self.map = None
31        self.engine = engine
32       
33        # init map attributes
34        self.cameras = {}
35        self.agent_layer = None
36        self.model = engine.getModel()
37        self.view = engine.getView()
38        self.transitions = []
39        self.obj_hash = {}
40        self.cur_cam2_x, self.initial_cam2_x, self.cam2_scrolling_right = 0,0,True
41        self.target_rotation = 0
42        self.outline_renderer = None
43       
44    def reset(self):
45        """Reset the data to default settings.
46           @return: None"""
47        # We have to delete the map in Fife.
48        # TODO: We're killing the PC now, but later we will have to save the PC
49        if self.map:
50            self.model.deleteObjects()
51            self.model.deleteMap(self.map)
52        self.transitions = []
53        self.obj_hash = {}
54        self.map,self.agent_layer = None,None
55        # We have to clear the cameras in the view as well, or we can't reuse
56        # camera names like 'main'
57        self.view.clearCameras()
58        self.cameras = {}
59        self.cur_cam2_x,self.initial_cam2_x,self.cam2_scrolling_right = 0,0,True
60        self.target_rotation = 0
61        self.outline_renderer = None
62       
63    def makeActive(self):
64        """Makes this map the active one.
65        """
66        pass
67       
68    def load(self, filename):
69        """Load a map given the filename.
70           @type filename: string
71           @param filename: Name of map to load
72           @return: None"""
73        self.reset()
74        self.map = loadMapFile(filename, self.engine)
75       
76        # there must be a PC object on the objects layer!
77        self.agent_layer = self.map.getLayer('ObjectLayer')
78       
79        # it's possible there's no transition layer
80        size = len('TransitionLayer')
81        for layer in self.map.getLayers():
82            # could be many layers, but hopefully no more than 3
83            if(layer.getId()[:size] == 'TransitionLayer'):
84                self.transitions.append(self.map.getLayer(layer.getId()))
85               
86        # init the camera
87        for cam in self.view.getCameras():
88            self.cameras[cam.getId()] = cam
89        self.view.resetRenderers()
90        self.target_rotation = self.cameras['main'].getRotation()
91       
92        self.outline_render = fife.InstanceRenderer.getInstance(self.cameras['main'])
93       
94        # set the render text
95        rend = fife.FloatingTextRenderer.getInstance(self.cameras['main'])
96        text = self.engine.getGuiManager().createFont('fonts/rpgfont.png',
97                                                       0, str(TDS.readSetting("FontGlyphs", strip=False)))
98        rend.changeDefaultFont(text)
99               
100    def addPC(self, agent):
101        """Add the player character to the map
102           @type agent: Fife.instance
103           @param : The object to use as the PC sprite
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, xpos, ypos, gfx, name):
110        """Add an object or an NPC to the map.
111           It makes no difference to fife which is which.
112           @type xpos: integer
113           @param xpos: x position of object
114           @type ypos: integer
115           @param ypos: y position of object
116           @type gfx: string
117           @param gfx: name of gfx image
118           @type name: string
119           @param name: name of object
120           @return: None"""
121        obj = self.agent_layer.createInstance(
122                self.model.getObject(str(gfx), "PARPG"),
123                fife.ExactModelCoordinate(float(xpos), float(ypos), 0.0), str(name))
124        obj.setRotation(0)
125        fife.InstanceVisual.create(obj)
126        # save it for later use
127        self.obj_hash[name]=obj
128       
129    def toggle_renderer(self, r_name):
130        """Enable or disable a renderer.
131           @return: None"""
132        renderer = self.cameras['main'].getRenderer(str(r_name))
133        renderer.setEnabled(not renderer.isEnabled())
Note: See TracBrowser for help on using the repository browser.