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

Revision 314, 4.8 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #66: Patch by saritor, eliedebrauwer & kaydeth cross map teleporting is now possible, we extended the objects within gamestate to make use of double hashing (f(obj_id,map_id)->object). Going back to a previously loaded map is still broken (camera issue). Also extended map.xml and map2.xml in order to make it possible to go back and forth between these two maps. comment[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.cameras = {}
37        self.agent_layer = None
38        self.model = engine.getModel()
39        self.view = engine.getView()
40        self.transitions = []
41        self.obj_hash = {}
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        # TODO: We're killing the PC now, but later we will have to save the PC
53        if self.map:
54            self.model.deleteObjects()
55            self.model.deleteMap(self.map)
56        self.transitions = []
57        self.obj_hash = {}
58        self.map = None
59        self.agent_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.cameras = {}
64        self.cur_cam2_x = 0
65        self.initial_cam2_x = 0
66        self.cam2_scrolling_right = True
67        self.target_rotation = 0
68        self.outline_renderer = None
69       
70    def makeActive(self):
71        """Makes this map the active one.
72        """
73        pass
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         
83        # there must be a PC object on the objects layer!
84        self.agent_layer = self.map.getLayer('ObjectLayer')
85       
86        # it's possible there's no transition layer
87        size = len('TransitionLayer')
88        for layer in self.map.getLayers():
89            # could be many layers, but hopefully no more than 3
90            if(layer.getId()[:size] == 'TransitionLayer'):
91                self.transitions.append(self.map.getLayer(layer.getId()))
92               
93        # init the camera
94        for cam in self.view.getCameras():
95            self.cameras[cam.getId()] = cam
96        self.view.resetRenderers()
97        self.target_rotation = self.cameras['main'].getRotation()
98       
99        self.outline_render = fife.InstanceRenderer.\
100                                        getInstance(self.cameras['main'])
101       
102        # set the render text
103        rend = fife.FloatingTextRenderer.getInstance(self.cameras['main'])
104        text = self.engine.getGuiManager().\
105                        createFont('fonts/rpgfont.png', 0, \
106                                   str(TDS.readSetting("FontGlyphs", \
107                                                       strip=False)))
108        rend.changeDefaultFont(text)
109               
110    def addPC(self, agent):
111        """Add the player character to the map
112           @type agent: Fife.instance of PC
113           @return: None"""
114        # actually this is real easy, we just have to
115        # attach the main camera to the PC, if a camera
116        # was already used, we simply recycle it.
117        if self.cameras['main'].getAttached() == None:
118            self.cameras['main'].attach(agent)
119
120    def addObject(self, name, obj):
121        """Add an object to this map0
122            Inputs:
123                name - ID of object
124                obj - FIFE instance of object
125            Return:
126                Nothing
127        """
128        # save it for later use
129        self.obj_hash[name]=obj
130       
131    def toggle_renderer(self, r_name):
132        """Enable or disable a renderer.
133           @return: None"""
134        renderer = self.cameras['main'].getRenderer(str(r_name))
135        renderer.setEnabled(not renderer.isEnabled())
136
Note: See TracBrowser for help on using the repository browser.