source: trunk/game/run.py @ 281

Revision 281, 6.2 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket: #94: Patch by amo-ej1. "grid" console command added to display a grid overlay on the map. Console command logic moved to it's own class (console.py). Also unit tests added for console class. fixes[s:trac, t:94]

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