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

Revision 412, 6.6 KB checked in by orlandov, 10 years ago (diff)

Ticket #63 - patch by or1andov

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