source: branches/active/character_customization/game/parpg/gamemap.py @ 766

Revision 755, 6.5 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites:

  • no longer generates settings.xml files
  • removed more references to old Settings API from model classes
  • Property svn:eol-style set to native
RevLine 
[584]1#!/usr/bin/env python
[262]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
[473]18from fife import fife
[627]19
[643]20from fife.extensions.loaders import loadMapFile
[262]21
[559]22class GameMap(fife.MapChangeListener):
[262]23    """Map class used to flag changes in the map"""
[632]24    def __init__(self, engine, model):
[262]25        # init mapchange listener
26        fife.MapChangeListener.__init__(self)
27        self.map = None
28        self.engine = engine
[632]29        self.model = model
[340]30
[262]31        # init map attributes
[340]32        self.my_cam_id = None
[262]33        self.cameras = {}
34        self.agent_layer = None
[412]35        self.top_layer = None
[553]36        self.fife_model = engine.getModel()
[262]37        self.transitions = []
[313]38        self.cur_cam2_x = 0
39        self.initial_cam2_x = 0
40        self.cam2_scrolling_right = True
[262]41        self.target_rotation = 0
42        self.outline_renderer = None
43       
44    def reset(self):
[553]45        """Reset the model to default settings.
[262]46           @return: None"""
47        # We have to delete the map in Fife.
48        if self.map:
49            self.model.deleteObjects()
50            self.model.deleteMap(self.map)
51        self.transitions = []
[313]52        self.map = None
[340]53        self.agent_layer = None       
[412]54        self.top_layer = None
[262]55        # We have to clear the cameras in the view as well, or we can't reuse
56        # camera names like 'main'
[340]57        #self.view.clearCameras()
[313]58        self.initial_cam2_x = 0
59        self.cam2_scrolling_right = True
[340]60        #self.cameras = {}
61        self.cur_cam2_x = 0
[262]62        self.target_rotation = 0
63        self.outline_renderer = None
64       
65    def makeActive(self):
66        """Makes this map the active one.
[359]67           @return: None"""
[340]68        self.cameras[self.my_cam_id].setEnabled(True)
[262]69       
70    def load(self, filename):
71        """Load a map given the filename.
[340]72           @type filename: String
[262]73           @param filename: Name of map to load
74           @return: None"""
75        self.reset()
[643]76        self.map = loadMapFile(filename, self.engine)
[262]77        self.agent_layer = self.map.getLayer('ObjectLayer')
[560]78        self.top_layer = self.map.getLayer('TopLayer')     
[356]79           
[262]80        # it's possible there's no transition layer
81        size = len('TransitionLayer')
82        for layer in self.map.getLayers():
83            # could be many layers, but hopefully no more than 3
84            if(layer.getId()[:size] == 'TransitionLayer'):
85                self.transitions.append(self.map.getLayer(layer.getId()))
[340]86
87        """ Initialize the camera.
88        Note that if we have more than one camera in a map file
89        we will have to rework how self.my_cam_id works. To make sure
[359]90        the proper camera is set as the 'main' camera.
91        At this point we also set the viewport to the current resolution."""
[484]92        for cam in self.map.getCameras():
[755]93            width = self.model.settings.fife.ScreenWidth
94            height = self.model.settings.fife.ScreenHeight
[553]95            viewport = fife.Rect(0, 0, width, height)
[359]96            cam.setViewPort(viewport)
[340]97            self.my_cam_id = cam.getId()
98            self.cameras[self.my_cam_id] = cam
[484]99            cam.resetRenderers()
100       
[340]101        self.target_rotation = self.cameras[self.my_cam_id].getRotation()
102
[553]103        self.outline_renderer = fife.InstanceRenderer.\
104                                        getInstance(
105                                                    self.cameras[
106                                                                 self.my_cam_id
107                                                                 ])
[340]108
[262]109        # set the render text
[553]110        rend = fife.FloatingTextRenderer.getInstance(self.cameras[
111                                                                  self.my_cam_id
112                                                                  ])
[313]113        text = self.engine.getGuiManager().\
114                        createFont('fonts/rpgfont.png', 0, \
[755]115                                   self.model.settings.fife.FontGlyphs)
[262]116        rend.changeDefaultFont(text)
[501]117        rend.activateAllLayers(self.map)
[612]118        rend.setEnabled(True)
[553]119       
120        # Activate the grid renderer on all layers
121        rend = self.cameras['map_camera'].getRenderer('GridRenderer')
122        rend.activateAllLayers(self.map)
[501]123         
[614]124        # Activate the grid renderer on all layers
125        rend = fife.CoordinateRenderer.getInstance(self.cameras[
126                                                                  self.my_cam_id
127                                                                  ])
128        rend.setColor(0, 0, 0)
129        rend.addActiveLayer(self.map.getLayer("GroundLayer"))
130
[340]131        # Make World aware that this is now the active map.
[553]132        self.model.active_map = self
[446]133
[340]134    def addPC(self):
[262]135        """Add the player character to the map
136           @return: None"""
[579]137        # Update gamestate.player_character
138        self.model.game_state.player_character.behaviour.onNewMap(self.agent_layer)
[632]139        self.centerCameraOnPlayer()
[340]140
[448]141    def toggleRenderer(self, r_name):
[262]142        """Enable or disable a renderer.
143           @return: None"""
[340]144        renderer = self.cameras[self.my_cam_id].getRenderer(str(r_name))
[314]145        renderer.setEnabled(not renderer.isEnabled())
146
[632]147    def isPaused(self):
148        """Returns wheter the map is currentply paused or not"""
149        # Time multiplier is a float, never do equals on floats
150        return not self.map.getTimeMultiplier() >= 1.0
151   
152    def pause(self, paused):
[446]153        """ Pause/Unpause the game.
[632]154        @return: nothing"""
155        if paused:
[446]156            self.map.setTimeMultiplier(0.0)
[632]157        if not paused and self.isPaused():
[446]158            self.map.setTimeMultiplier(1.0)
[632]159       
160    def togglePause(self):
161        """ Toggle paused state.
162        @return: nothing"""
163        self.pause(not self.isPaused())
164
165    def centerCameraOnPlayer(self):
166        """Center the camera on the player"""
167        camera = self.cameras[self.my_cam_id]
168        player_agent = self.model.game_state.player_character.behaviour.agent
169        camera.setLocation(player_agent.getLocation())
Note: See TracBrowser for help on using the repository browser.