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

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

Patch by Technomage

  • Added a loadFonts method to PARPGApplication which is called upon instantiation and loads all .fontdef files used to define fonts for PyChan?;

Patch by Technomage

  • Fixed a bug in my last commit that caused the game to crash becase a) it was reading the wrong settings section and b) it was using pychan.loadFont instead of pychange.loadFonts;

Ticket #305: Patch by beliar.
Additions to the public interface of the CharacterCreationController? class:

  • Added read-only properties for name, age, gender, origin and picture.
  • Added getStatisticValue method to get the value of a primary or secondary statistic.

Note that these do not return the actual value at the moment, as there are no values stored yet.

Patch by Aspidites:

+ fixed bug caused by an object not having a description

Patch by Aspidites:

  • started integrating settings.py into current modules.

Patch by Aspidites

+ resolved conflicts before merging my setings branch

Patch by Aspidites

+ added quotes back to FontGlyphs? option

Patch by Aspidites

+ added FontsDirectory? back to config file (which is now settings.ini

Patch by Aspidites

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