source: branches/active/character_customization/game/parpg/application.py @ 758

Revision 756, 9.0 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites:

+ maps load properly once again
+ obsoleted get/set methods in settings.py

  • pc speed is no longer set, so walk speed is unbearably slow
  • Property svn:eol-style set to native
RevLine 
[653]1#!/usr/bin/env python
2
3#   This program is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7
8#   This program is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12
13#   You should have received a copy of the GNU General Public License
14#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15"""This module contains the main Application class
16and the basic Listener for PARPG """
17
[742]18import os
[653]19
20from fife import fife
21from fife.extensions import pychan
[748]22from fife.extensions.serializers.xmlanimation import XMLAnimationLoader
[653]23from fife.extensions.basicapplication import ApplicationBase
24
[756]25from parpg import console
[736]26from parpg.gamemodel import GameModel
27from parpg.mainmenuview import MainMenuView
28from parpg.mainmenucontroller import MainMenuController
29from parpg.common.listeners.event_listener import EventListener
30from parpg.common.listeners.key_listener import KeyListener
31from parpg.common.listeners.mouse_listener import MouseListener
32from parpg.common.listeners.command_listener import CommandListener
33from parpg.common.listeners.console_executor import ConsoleExecuter
34from parpg.common.listeners.widget_listener import WidgetListener
[653]35
36class KeyFilter(fife.IKeyFilter):
37    """
38    This is the implementation of the fife.IKeyFilter class.
39   
40    Prevents any filtered keys from being consumed by guichan.
41    """
42    def __init__(self, keys):
43        fife.IKeyFilter.__init__(self)
44        self._keys = keys
45
46    def isFiltered(self, event):
47        """Checks if an key is filtered"""
48        return event.getKey().getValue() in self._keys
49
50class ApplicationListener(KeyListener,
51                        MouseListener,
52                        ConsoleExecuter,
53                        CommandListener,
54                        WidgetListener):   
55    """Basic listener for PARPG"""
56       
57    def __init__(self, event_listener, engine, view, model):
58        """Initialize the instance.
59           @type engine: fife.engine
60           @param engine: ???
61           @type view: viewbase.ViewBase
62           @param view: View that draws the current state
63           @type model: GameModel
64           @param model: The game model"""
65        KeyListener.__init__(self, event_listener)
66        MouseListener.__init__(self, event_listener)
67        ConsoleExecuter.__init__(self, event_listener)
68        CommandListener.__init__(self, event_listener)
69        WidgetListener.__init__(self, event_listener)       
70        self.engine = engine
71        self.view = view
72        self.model = model
73        keyfilter = KeyFilter([fife.Key.ESCAPE])
74        keyfilter.__disown__()       
75       
76        engine.getEventManager().setKeyFilter(keyfilter)
77        self.quit = False
78        self.about_window = None
79        self.console = console.Console(self)
80
81    def quitGame(self):
82        """Forces a quit game on next cycle.
83           @return: None"""
84        self.quit = True
85
86    def onConsoleCommand(self, command):
87        """
88        Called on every console comand, delegates calls  to the a console
89        object, implementing the callbacks
90        @type command: string
91        @param command: the command to run
92        @return: result
93        """
94        return self.console.handleConsoleCommand(command)
95
96    def onCommand(self, command):
97        """Enables the game to be closed via the 'X' button on the window frame
98           @type command: fife.Command
99           @param command: The command to read.
100           @return: None"""
101        if(command.getCommandType() == fife.CMD_QUIT_GAME):
102            self.quit = True
103            command.consume()
104
105class PARPGApplication(ApplicationBase):
106    """Main Application class
107       We use an MVC model model
108       self.gamesceneview is our view,self.model is our model
109       self.controller is the controller"""
110       
111    def __init__(self, setting):
112        """Initialise the instance.
113           @return: None"""
[748]114        self._setting = setting
115        self.engine = fife.Engine()
116        self.loadSettings()
117        self.engine.init()
118        self._animationloader = XMLAnimationLoader(self.engine.getImagePool(),
119                                                   self.engine.getVFS())
120        self.engine.getAnimationPool().addResourceLoader(self._animationloader)
121
[653]122        pychan.init(self.engine, debug = True)
[754]123        pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop)
124
125        self.quitRequested = False
126        self.breakRequested = False
127        self.returnValues = []
[653]128        #self.engine.getModel(self)
129        self.model = GameModel(self.engine, setting)
130        self.model.readMapFiles()
131        self.model.readObjectDB()
132        self.model.getAgentImportFiles()
133        self.model.readAllAgents()
134        self.model.getDialogues()
135        self.view = MainMenuView(self.engine, self.model)
[752]136        self.fonts_directory = self._setting.fife.FontsDirectory
[742]137        self.loadFonts(self.fonts_directory)
[653]138        self.event_listener = EventListener(self.engine)
139        self.controllers = []
[756]140        controller = MainMenuController(self.engine, self.view, self.model, 
141                                        self)
[653]142        #controller.initHud()
143        self.controllers.append(controller)
144        self.listener = ApplicationListener(self.event_listener,
145                                            self.engine, 
146                                            self.view, 
147                                            self.model)
148        #start_map = self._setting.get("PARPG", "Map")
149        #self.model.changeMap(start_map)
150
[742]151    def loadFonts(self, fonts_directory):
152        file_names = os.listdir(fonts_directory)
153        for file_name in file_names:
154            base_name, extension = os.path.splitext(file_name)
155            if extension == '.fontdef':
[743]156                pychan.loadFonts(file_name)
[742]157
[748]158    def loadSettings(self):
159        """
160        Load the settings from a python file and load them into the engine.
161        Called in the ApplicationBase constructor.
162        """
163
164        engineSetting = self.engine.getSettings()
165        engineSetting.setDefaultFontGlyphs(self._setting.fife.FontGlyphs)
166        engineSetting.setDefaultFontPath(self._setting.fife.Font)
167        engineSetting.setDefaultFontSize(self._setting.fife.DefaultFontSize)
168        engineSetting.setBitsPerPixel(self._setting.fife.BitsPerPixel)
169        engineSetting.setInitialVolume(self._setting.fife.InitialVolume)
170        engineSetting.setSDLRemoveFakeAlpha(self._setting.fife.SDLRemoveFakeAlpha)
171        engineSetting.setScreenWidth(self._setting.fife.ScreenWidth)
172        engineSetting.setScreenHeight(self._setting.fife.ScreenHeight)
173        engineSetting.setRenderBackend(self._setting.fife.RenderBackend)
174        engineSetting.setFullScreen(self._setting.fife.FullScreen)
175        engineSetting.setVideoDriver(self._setting.fife.VideoDriver)
176        engineSetting.setLightingModel(self._setting.fife.Lighting)
177        engineSetting.setColorKeyEnabled(self._setting.fife.ColorKeyEnabled)
178
179        key = [int(digit) for digit in self._setting.fife.ColorKey]
180        engineSetting.setColorKey(*key)
181
182        engineSetting.setWindowTitle(self._setting.fife.WindowTitle)
183        engineSetting.setWindowIcon(self._setting.fife.WindowIcon)
184
185        try:
186            engineSetting.setImageChunkSize(self._setting.fife.ImageChunkSize)
187        except:
188            pass
189
[738]190    def createListener(self):
191        """ __init__ takes care of the event listener, basicapplication's
192           createListener is only harmful. Without overriding it, the program
193           quit's on esc press, rather than invoking the main menu.
194        """
195        pass
196
[653]197    def pushController(self, controller):
198        """Adds a controller to the list to be the current active one."""
199        self.controllers[-1].pause(True)
200        self.controllers.append(controller)
201   
202    def popController(self):
203        """Removes and returns the current active controller, unless its the last one"""
204        ret_controller = None
205        if self.controllers.count > 1:
206            ret_controller = self.controllers.pop()
207            self.controllers[-1].pause(False)
208        ret_controller.onStop()
209        return ret_controller
210   
211    def switchController(self, controller):
212        """Clears the controller list and adds a controller to be the current active one"""
213        for old_controller in self.controllers:
214            old_controller.onStop()
215        self.controllers = []
216        self.controllers.append(controller)
217   
218    def _pump(self):
219        """Main game loop.
220           There are in fact 2 main loops, this one and the one in GameSceneView.
221           @return: None"""
222        if self.listener.quit:
223            self.breakRequested = True #pylint: disable-msg=C0103
224        else:
225            for controller in self.controllers:
[737]226                controller.pump()
Note: See TracBrowser for help on using the repository browser.