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

Revision 359, 6.5 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #132: Patch by eliedebrauwer. Added logic to automatically learn the correct viewport (this is the current resolution) and apply this on a camera whenever the map gets loaded. fixes[s:trac, t:132]

  • 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
19import time
20from local_loaders.loaders import loadMapFile
21from scripts.common.eventlistenerbase import EventListenerBase
22
23from settings import Setting
24TDS = Setting()
25
26class Map(fife.MapChangeListener):
27    """Map class used to flag changes in the map"""
28    def __init__(self, engine, data):
29        # init mapchange listener
30        fife.MapChangeListener.__init__(self)
31        self.map = None
32        self.engine = engine
33        self.data = data
34
35        # init map attributes
36        self.my_cam_id = None
37        self.cameras = {}
38        self.agent_layer = None
39        self.model = engine.getModel()
40        self.view = engine.getView()
41        self.transitions = []
42        self.cur_cam2_x = 0
43        self.initial_cam2_x = 0
44        self.cam2_scrolling_right = True
45        self.target_rotation = 0
46        self.outline_renderer = None
47       
48    def reset(self):
49        """Reset the data to default settings.
50           @return: None"""
51        # We have to delete the map in Fife.
52        if self.map:
53            self.model.deleteObjects()
54            self.model.deleteMap(self.map)
55        self.transitions = []
56        self.map = None
57        self.agent_layer = None       
58        # We have to clear the cameras in the view as well, or we can't reuse
59        # camera names like 'main'
60        #self.view.clearCameras()
61        self.initial_cam2_x = 0
62        self.cam2_scrolling_right = True
63        #self.cameras = {}
64        self.cur_cam2_x = 0
65        self.target_rotation = 0
66        self.outline_renderer = None
67       
68    def makeActive(self):
69        """Makes this map the active one.
70           @return: None"""
71        self.cameras[self.my_cam_id].setEnabled(True)
72       
73    def load(self, filename):
74        """Load a map given the filename.
75           @type filename: String
76           @param filename: Name of map to load
77           @return: None"""
78        self.reset()
79        self.map = loadMapFile(filename, self.engine, self.data)
80        self.agent_layer = self.map.getLayer('ObjectLayer')
81       
82        #find out if a PC instance was created when the map was loaded
83        found = False
84        for inst in self.agent_layer.getInstances():
85            if inst.getId() == "PC":
86                found = True
87                break
88               
89        #If there is not a PC instance created yet than we construct the PC
90        #instance from what we have saved in the PC Game Object
91        if not found:
92            x = float(self.data.target_position[0])
93            y = float(self.data.target_position[1])
94            z = 0
95            pc_obj = self.model.getObject("player", "PARPG")
96            inst = self.agent_layer.createInstance(pc_obj,\
97                                            fife.ExactModelCoordinate(x,y,z),\
98                                            "PC")
99            fife.InstanceVisual.create(inst)
100        #If the PC instance exists already then make sure it's set to correct
101        #location for this new map
102        elif self.data.target_position is not None:
103            pos = self.data.target_position
104            coord = fife.DoublePoint3D(float(pos[0]), float(pos[1]), 0)
105            location = fife.Location(self.agent_layer)
106            location.setMapCoordinates(coord)
107            inst.setLocation(location)
108        #else we are loading the first map and the PC position were set by
109        #the coordinates in the Map file
110           
111        # it's possible there's no transition layer
112        size = len('TransitionLayer')
113        for layer in self.map.getLayers():
114            # could be many layers, but hopefully no more than 3
115            if(layer.getId()[:size] == 'TransitionLayer'):
116                self.transitions.append(self.map.getLayer(layer.getId()))
117
118        """ Initialize the camera.
119        Note that if we have more than one camera in a map file
120        we will have to rework how self.my_cam_id works. To make sure
121        the proper camera is set as the 'main' camera.
122        At this point we also set the viewport to the current resolution."""
123        for cam in self.view.getCameras():
124            width = int(TDS.readSetting(name="ScreenWidth"))
125            height = int(TDS.readSetting(name="ScreenHeight"))
126            viewport = fife.Rect(0,0,width,height)
127            cam.setViewPort(viewport)
128            self.my_cam_id = cam.getId()
129            self.cameras[self.my_cam_id] = cam
130        self.view.resetRenderers()
131        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
132
133        self.outline_render = fife.InstanceRenderer.\
134                                        getInstance(self.cameras[self.my_cam_id])
135
136        # set the render text
137        rend = fife.FloatingTextRenderer.getInstance(self.cameras[self.my_cam_id])
138        text = self.engine.getGuiManager().\
139                        createFont('fonts/rpgfont.png', 0, \
140                                   str(TDS.readSetting("FontGlyphs", \
141                                                       strip=False)))
142        rend.changeDefaultFont(text)
143       
144        # Make World aware that this is now the active map.
145        self.data.view.active_map = self
146               
147    def addPC(self):
148        """Add the player character to the map
149           @return: None"""
150
151        # Update gamestate.PC
152        self.data.game_state.PC.behaviour.onNewMap(self.agent_layer)
153
154        # actually this is real easy, we just have to
155        # attach the main camera to the PC, if a camera
156        # was already used, we simply recycle it.
157        if self.cameras[self.my_cam_id].getAttached() == None:
158            self.cameras[self.my_cam_id].attach(self.data.game_state.PC.behaviour.agent)
159
160       
161    def toggle_renderer(self, r_name):
162        """Enable or disable a renderer.
163           @return: None"""
164        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
165        renderer.setEnabled(not renderer.isEnabled())
166
Note: See TracBrowser for help on using the repository browser.