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

Revision 340, 5.4 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #66: Patch by Saritor, This patch implements Kaydeth's camera fix as well as a fix for letting us be able to remove the PC entry on individual map files. However the very first map a player starts the game on will need such an entry. fixes[s:trac, t:66]

  • 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        """
71        self.cameras[self.my_cam_id].setEnabled(True)
72
73       
74    def load(self, filename):
75        """Load a map given the filename.
76           @type filename: String
77           @param filename: Name of map to load
78           @return: None"""
79        self.reset()
80        self.map = loadMapFile(filename, self.engine, self.data)
81        self.agent_layer = self.map.getLayer('ObjectLayer')
82
83        # if this is not the very first map load in the game carry over the PC instance
84        if self.data.target_position:
85            x = float(self.data.target_position[0])
86            y = float(self.data.target_position[1])
87            z = 0
88            pc_obj = self.model.getObject("player", "PARPG")
89            inst = self.agent_layer.createInstance(pc_obj,\
90                                            fife.ExactModelCoordinate(x,y,z),\
91                                            "PC")
92            inst.setRotation(self.data.game_state.PC.behaviour.agent.getRotation())
93            fife.InstanceVisual.create(inst)
94
95        # it's possible there's no transition layer
96        size = len('TransitionLayer')
97        for layer in self.map.getLayers():
98            # could be many layers, but hopefully no more than 3
99            if(layer.getId()[:size] == 'TransitionLayer'):
100                self.transitions.append(self.map.getLayer(layer.getId()))
101
102        """ Initialize the camera.
103        Note that if we have more than one camera in a map file
104        we will have to rework how self.my_cam_id works. To make sure
105        the proper camera is set as the 'main' camera."""
106        for cam in self.view.getCameras():
107            self.my_cam_id = cam.getId()
108            self.cameras[self.my_cam_id] = cam
109        self.view.resetRenderers()
110        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
111
112        self.outline_render = fife.InstanceRenderer.\
113                                        getInstance(self.cameras[self.my_cam_id])
114
115        # set the render text
116        rend = fife.FloatingTextRenderer.getInstance(self.cameras[self.my_cam_id])
117        text = self.engine.getGuiManager().\
118                        createFont('fonts/rpgfont.png', 0, \
119                                   str(TDS.readSetting("FontGlyphs", \
120                                                       strip=False)))
121        rend.changeDefaultFont(text)
122
123        # Make World aware that this is now the active map.
124        self.data.view.active_map = self
125               
126    def addPC(self):
127        """Add the player character to the map
128           @return: None"""
129
130        # Update gamestate.PC
131        self.data.game_state.PC.behaviour.onNewMap(self.agent_layer)
132
133        # actually this is real easy, we just have to
134        # attach the main camera to the PC, if a camera
135        # was already used, we simply recycle it.
136        if self.cameras[self.my_cam_id].getAttached() == None:
137            self.cameras[self.my_cam_id].attach(self.data.game_state.PC.behaviour.agent)
138
139       
140    def toggle_renderer(self, r_name):
141        """Enable or disable a renderer.
142           @return: None"""
143        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
144        renderer.setEnabled(not renderer.isEnabled())
145
Note: See TracBrowser for help on using the repository browser.