source: trunk/game/run.py @ 351

Revision 339, 6.3 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket #121. Patch by Zenbitz. I hacked up the dialogue-demo.py script so that I could test my fguard.yaml file. There were lots of bugs! I had to add several callbacks and functions to the dummy objects in the demo script. I also modified it so that it will use a .yaml file as the first command line argument instead of hard-coded dialogues/sample.yaml (still the default).

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[339]1#!/usr/bin/env python
[26]2
[30]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.
[26]7
[30]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.
[26]12
[30]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/>.
[26]15
[222]16import sys, os, shutil, re
[26]17
[32]18from scripts.common import utils
[33]19# add paths to the swig extensions
[48]20utils.addPaths ('../../engine/swigwrappers/python', '../../engine/extensions')
[26]21
[88]22if not os.path.exists('settings.xml'):
23    shutil.copyfile('settings-dist.xml', 'settings.xml')
24
[143]25import fife_compat, fife, fifelog
[157]26import pychan
[26]27from scripts import world
[54]28from scripts import engine
[281]29from scripts import console
[222]30from scripts.engine import Engine
[26]31from scripts.common import eventlistenerbase
32from basicapplication import ApplicationBase
33from settings import Setting
34
35TDS = Setting()
36
[143]37"""This folder holds the main meta-data for PARPG. This file should be
38   minimal, since folding code into the controller with MVC is usually bad
[312]39   All game and logic and data is held held and referenced in
40   /scripts/engine.py. All fife stuff goes in /scripts/world.py"""
[58]41
[26]42class ApplicationListener(eventlistenerbase.EventListenerBase):
[222]43    def __init__(self, engine, world, model):
[143]44        """Initialise the instance.
[302]45           @type engine: engine.Engine
[143]46           @param engine: ???
[302]47           @type world: world.World
[143]48           @param world: ???
[222]49           @type model: engine.Engine
50           @param model: an instance of PARPG's engine"""
[30]51        super(ApplicationListener, self).__init__(engine,
[315]52                                                  reg_keys=True,reg_cmd=True,
53                                                  reg_mouse=False, 
54                                                  reg_console=True,
55                                                  reg_widget=True)
[66]56        self.engine = engine
57        self.world = world
[222]58        self.model = model
[30]59        engine.getEventManager().setNonConsumableKeys([fife.Key.ESCAPE,])
[66]60        self.quit = False
61        self.aboutWindow = None
[281]62        self.console=console.Console(self)
[26]63
[58]64    def quitGame(self):
[143]65        """Forces a quit game on next cycle.
66           @return: None"""
[66]67        self.quit = True
[58]68
[220]69    def onConsoleCommand(self, command):
70        """
[281]71        Called on every console comand, delegates calls  to the a console
72        object, implementing the callbacks
[220]73        @type command: string
74        @param command: the command to run
75        @return: result
76        """
[222]77
[281]78        return self.console.handleConsoleCommand(command)
[222]79
[66]80    def onCommand(self, command):
[143]81        """Enables the game to be closed via the 'X' button on the window frame
82           @type command: fife.Command
83           @param command: The command to read.
84           @return: None"""
[66]85        if(command.getCommandType() == fife.CMD_QUIT_GAME):
86            self.quit = True
[32]87            command.consume()
[26]88
89class PARPG(ApplicationBase):
[54]90    """Main Application class
[58]91       We use an MVC data model.
92       self.world is our view,self.engine is our model
93       This file is the minimal controller"""
[30]94    def __init__(self):
[143]95        """Initialise the instance.
96           @return: None"""
[30]97        super(PARPG,self).__init__()
[66]98        self.world = world.World(self.engine)
99        self.model = engine.Engine(self.world)
[68]100        self.world.data = self.model
[222]101        self.listener = ApplicationListener(self.engine,self.world,self.model)
[66]102        self.world.quitFunction = self.listener.quitGame
[310]103        self.model.loadMap("main-map", str(TDS.readSetting("MapFile")))
[157]104        pychan.init(self.engine, debug = True)
[26]105
[30]106    def loadSettings(self):
107        """Load the settings from a python file and load them into the engine.
[143]108           Called in the ApplicationBase constructor.
109           @return: None"""
[30]110        import settings
[66]111        self.settings = settings
[315]112        e_set = self.engine.getSettings()
113        e_set.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs",
[66]114                                                      strip=False)))
[315]115        e_set.setDefaultFontPath(str(TDS.readSetting("Font")))
116        e_set.setDefaultFontSize(int(TDS.readSetting("FontSize", default=12)))
117        e_set.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
118        e_set.setInitialVolume(float(TDS.readSetting("InitialVolume")))
119        e_set.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
120        e_set.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
121        e_set.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
122        e_set.setRenderBackend(str(TDS.readSetting("RenderBackend")))
123        e_set.setFullScreen(int(TDS.readSetting("FullScreen")))
[30]124        try:
[315]125            e_set.setWindowTitle(str(TDS.readSetting("WindowTitle")))
126            e_set.setWindowIcon(str(TDS.readSetting("WindowIcon")))
[30]127        except:
128            pass           
129        try:
[315]130            e_set.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
[30]131        except:
132            pass
[26]133
[30]134    def initLogging(self):
[143]135        """Initialize the LogManager.
136           @return: None"""
[66]137        LogModules = TDS.readSetting("LogModules",type='list')
138        self.log = fifelog.LogManager(self.engine,
[32]139                                      int(TDS.readSetting("LogToPrompt")),
140                                      int(TDS.readSetting("LogToFile")))
[58]141        if(LogModules):
[30]142            self.log.setVisibleModules(*LogModules)
[26]143
[30]144    def createListener(self):
[143]145        """@return: None"""
[30]146        # already created in constructor
[143]147        # but if we don't put one here, Fife gets all fussy :-)
[30]148        pass
[26]149
[30]150    def _pump(self):
[143]151        """Main game loop.
152           There are in fact 2 main loops, this one and the one in World.
153           @return: None"""
[30]154        if self.listener.quit:
155            self.breakRequested = True
156        else:
[150]157            self.model.pump()
[30]158            self.world.pump()
[26]159
160def main():
[30]161    """Application code starts from here"""
[66]162    app = PARPG()
[30]163    app.run()
[26]164
165if __name__ == '__main__':
[30]166    main()
[26]167
Note: See TracBrowser for help on using the repository browser.