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

Revision 737, 7.0 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites:

  • removed createListeners method as it didn't add functionality and in fact prevented esc key from being recognized.
  • Property svn:eol-style set to native
Line 
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
18
19from fife import fife
20from fife.extensions import pychan
21from fife.extensions.basicapplication import ApplicationBase
22
23from parpg.gamemodel import GameModel
24from parpg.mainmenuview import MainMenuView
25from parpg import console
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        KeyListener.__init__(self, event_listener)
64        MouseListener.__init__(self, event_listener)
65        ConsoleExecuter.__init__(self, event_listener)
66        CommandListener.__init__(self, event_listener)
67        WidgetListener.__init__(self, event_listener)       
68        self.engine = engine
69        self.view = view
70        self.model = model
71        keyfilter = KeyFilter([fife.Key.ESCAPE])
72        keyfilter.__disown__()       
73       
74        engine.getEventManager().setKeyFilter(keyfilter)
75        self.quit = False
76        self.about_window = None
77        self.console = console.Console(self)
78
79    def quitGame(self):
80        """Forces a quit game on next cycle.
81           @return: None"""
82        self.quit = True
83
84    def onConsoleCommand(self, command):
85        """
86        Called on every console comand, delegates calls  to the a console
87        object, implementing the callbacks
88        @type command: string
89        @param command: the command to run
90        @return: result
91        """
92        return self.console.handleConsoleCommand(command)
93
94    def onCommand(self, command):
95        """Enables the game to be closed via the 'X' button on the window frame
96           @type command: fife.Command
97           @param command: The command to read.
98           @return: None"""
99        if(command.getCommandType() == fife.CMD_QUIT_GAME):
100            self.quit = True
101            command.consume()
102
103class PARPGApplication(ApplicationBase):
104    """Main Application class
105       We use an MVC model model
106       self.gamesceneview is our view,self.model is our model
107       self.controller is the controller"""
108       
109    def __init__(self, setting):
110        """Initialise the instance.
111           @return: None"""
112        super(PARPGApplication, self).__init__(setting)
113        pychan.init(self.engine, debug = True)
114        #self.engine.getModel(self)
115        self.model = GameModel(self.engine, setting)
116        self.model.maps_file = self._setting.get("PARPG", "MapsFile")
117        self.model.readMapFiles()
118        self.model.object_db_file = self._setting.get("PARPG", 
119                                                      "ObjectDatabaseFile") 
120        self.model.readObjectDB()
121        self.model.agents_directory = self._setting.get("PARPG",
122                                                        "AgentsDirectory") 
123        self.model.getAgentImportFiles()
124        self.model.all_agents_file = self._setting.get("PARPG", "AllAgentsFile")
125        self.model.readAllAgents()
126        self.model.dialogues_directory = self._setting.get("PARPG", 
127                                                           "DialoguesDirectory")
128        self.model.getDialogues()
129        self.view = MainMenuView(self.engine, self.model)
130        self.event_listener = EventListener(self.engine)
131        self.controllers = []
132        controller = MainMenuController(self.engine, 
133                                                        self.view, 
134                                                        self.model,
135                                                        self)
136        #controller.initHud()
137        self.controllers.append(controller)
138        self.listener = ApplicationListener(self.event_listener,
139                                            self.engine, 
140                                            self.view, 
141                                            self.model)
142        #start_map = self._setting.get("PARPG", "Map")
143        #self.model.changeMap(start_map)
144
145    def pushController(self, controller):
146        """Adds a controller to the list to be the current active one."""
147        self.controllers[-1].pause(True)
148        self.controllers.append(controller)
149   
150    def popController(self):
151        """Removes and returns the current active controller, unless its the last one"""
152        ret_controller = None
153        if self.controllers.count > 1:
154            ret_controller = self.controllers.pop()
155            self.controllers[-1].pause(False)
156        ret_controller.onStop()
157        return ret_controller
158   
159    def switchController(self, controller):
160        """Clears the controller list and adds a controller to be the current active one"""
161        for old_controller in self.controllers:
162            old_controller.onStop()
163        self.controllers = []
164        self.controllers.append(controller)
165   
166    def _pump(self):
167        """Main game loop.
168           There are in fact 2 main loops, this one and the one in GameSceneView.
169           @return: None"""
170        if self.listener.quit:
171            self.breakRequested = True #pylint: disable-msg=C0103
172        else:
173            for controller in self.controllers:
174                controller.pump()
Note: See TracBrowser for help on using the repository browser.