source: trunk/PARPG/run.py @ 54

Revision 54, 4.5 KB checked in by maximinus_parpg, 11 years ago (diff)

Set Model / View data model for PARPG.
Code no longer looks like Rio demo.
Started to split map from objects.
Added crate and NPC character.

  • 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
17
18from scripts.common import utils
19# add paths to the swig extensions
20utils.addPaths ('../../engine/swigwrappers/python', '../../engine/extensions')
21
22import fife_compat
23import fife, fifelog
24from scripts import world
25from scripts import engine
26from scripts.common import eventlistenerbase
27from basicapplication import ApplicationBase
28from settings import Setting
29
30TDS = Setting()
31
32class ApplicationListener(eventlistenerbase.EventListenerBase):
33    def __init__(self, engine, world):
34        super(ApplicationListener, self).__init__(engine,
35                                                  regKeys=True,regCmd=True, regMouse=False, 
36                                                  regConsole=True, regWidget=True)
37        self.engine = engine
38        self.world = world
39        engine.getEventManager().setNonConsumableKeys([fife.Key.ESCAPE,])
40        self.quit = False
41        self.aboutWindow = None
42
43    def keyPressed(self, evt):
44        """Function to deal with keypress events
45           @param evt: The event that was captured"""
46        keyval = evt.getKey().getValue()
47        if keyval == fife.Key.ESCAPE:
48            self.quit = True
49        evt.consume()
50         
51    def onCommand(self, command):
52        """Enables the game to be closed via the 'X' button on the window frame"""
53        self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME)
54        if self.quit:
55            command.consume()
56
57class PARPG(ApplicationBase):
58    """Main Application class
59       We use an MV data model.
60       self.world is our view
61       self.engine is our model"""
62    def __init__(self):
63        super(PARPG,self).__init__()
64        self.world = world.World(self.engine)
65        self.model = engine.Engine(self.world)
66        self.listener = ApplicationListener(self.engine, self.world)
67        self.model.loadMap(str(TDS.readSetting("MapFile")))
68
69    def loadSettings(self):
70        """Load the settings from a python file and load them into the engine.
71           Called in the ApplicationBase constructor."""
72        import settings
73        self.settings = settings
74
75        eSet = self.engine.getSettings()
76        eSet.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs",strip=False)))
77        eSet.setDefaultFontPath(str(TDS.readSetting("Font")))
78        eSet.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
79        eSet.setInitialVolume(float(TDS.readSetting("InitialVolume")))
80        eSet.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
81        eSet.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
82        eSet.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
83        eSet.setRenderBackend(str(TDS.readSetting("RenderBackend")))
84        eSet.setFullScreen(int(TDS.readSetting("FullScreen")))
85        try:
86            eSet.setWindowTitle(str(TDS.readSetting("WindowTitle")))
87            eSet.setWindowIcon(str(TDS.readSetting("WindowIcon")))
88        except:
89            pass           
90        try:
91            eSet.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
92        except:
93            pass
94
95    def initLogging(self):
96        """Initialize the LogManager"""
97        LogModules = TDS.readSetting("LogModules",type='list')
98        self.log = fifelog.LogManager(self.engine,
99                                      int(TDS.readSetting("LogToPrompt")),
100                                      int(TDS.readSetting("LogToFile")))
101        if LogModules:
102            self.log.setVisibleModules(*LogModules)
103
104    def createListener(self):
105        # already created in constructor
106        pass
107
108    def _pump(self):
109        """Main game loop
110           There are in fact 2 main loops, this one and the one in World"""
111        if self.listener.quit:
112            self.breakRequested = True
113        else:
114            self.world.pump()
115
116def main():
117    """Application code starts from here"""
118    app = PARPG()
119    app.run()
120
121if __name__ == '__main__':
122    main()
123
Note: See TracBrowser for help on using the repository browser.