source: trunk/game/scripts/parpg.py @ 566

Revision 566, 5.7 KB checked in by beliar, 10 years ago (diff)

Patch by Beliar.

  • Fixed inventory/container contents not saved/restored correctly
  • Changed numbering of inventory-item id's to only add a number if the original id is in use. (Id's are being saved and we don't want to add another number to one already present)
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
16from fife import fife
17from fife.extensions import pychan
18from fife.extensions.basicapplication import ApplicationBase
19from scripts import gamemodel
20
21from scripts import gamesceneview
22from scripts import console
23from scripts import gamescenecontroller
24from scripts.common import eventlistenerbase
25
26class KeyFilter(fife.IKeyFilter):
27    """
28    This is the implementation of the fife.IKeyFilter class.
29   
30    Prevents any filtered keys from being consumed by guichan.
31    """
32    def __init__(self, keys):
33        fife.IKeyFilter.__init__(self)
34        self._keys = keys
35
36    def isFiltered(self, event):
37        return event.getKey().getValue() in self._keys
38
39class ApplicationListener(eventlistenerbase.EventListenerBase):
40    def __init__(self, engine, world, model):
41        """Initialise the instance.
42           @type engine: fife.GameModel
43           @param engine: ???
44           @type gamesceneview: gamesceneview.GameSceneView
45           @param gamesceneview: ???
46           @type model: script.GameModel
47           @param model: The game model"""
48        super(ApplicationListener, self).__init__(engine,
49                                                  reg_keys=True,reg_cmd=True,
50                                                  reg_mouse=False, 
51                                                  reg_console=True,
52                                                  reg_widget=True)
53        self.engine = engine
54        self.view = world
55        self.model = model
56        keyfilter = KeyFilter([fife.Key.ESCAPE])
57        keyfilter.__disown__()       
58       
59        engine.getEventManager().setKeyFilter(keyfilter)
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        return self.console.handleConsoleCommand(command)
78
79    def onCommand(self, command):
80        """Enables the game to be closed via the 'X' button on the window frame
81           @type command: fife.Command
82           @param command: The command to read.
83           @return: None"""
84        if(command.getCommandType() == fife.CMD_QUIT_GAME):
85            self.quit = True
86            command.consume()
87
88class PARPGApplication(ApplicationBase):
89    """Main Application class
90       We use an MVC model model
91       self.gamesceneview is our view,self.model is our model
92       self.controller is the controller"""
93    def __init__(self, setting):
94        """Initialise the instance.
95           @return: None"""
96        super(PARPGApplication, self).__init__(setting)
97        #self.engine.getModel(self)
98        self.model = gamemodel.GameModel(self.engine, setting)
99        self.model.maps_file = self._setting.get("PARPG", "MapsFile")
100        self.model.readMapFiles()
101        self.model.object_db_file = self._setting.get("PARPG", 
102                                                      "ObjectDatabaseFile") 
103        self.model.readObjectDB()
104        self.model.agents_directory = self._setting.get("PARPG",
105                                                        "AgentsDirectory") 
106        self.model.getAgentImportFiles()
107        self.model.all_agents_file = self._setting.get("PARPG", "AllAgentsFile")
108        self.model.readAllAgents()
109        self.model.dialogues_directory = self._setting.get("PARPG", 
110                                                           "DialoguesDirectory")
111        self.model.getDialogues()
112        self.view = gamesceneview.GameSceneView(self.engine, self.model)
113        self.controller = gamescenecontroller.GameSceneController(self.engine, 
114                                                        self.view, 
115                                                        self.model, 
116                                                        self)
117        self.controller.initHud()
118        self.listener = ApplicationListener(self.engine, 
119                                            self.view, 
120                                            self.model)
121        start_map = self._setting.get("PARPG", "Map")
122        #self.model.loadMap(start_map)
123        #self.model.readAgentsOfMap(start_map)
124        self.model.changeMap(start_map)
125        #self.model.placeAgents()
126        #self.model.placePC()
127        pychan.init(self.engine, debug = True)
128
129    def createListener(self):
130        """@return: None"""
131        # already created in constructor
132        # but if we don't put one here, Fife gets all fussy :-)
133        pass
134
135    def _pump(self):
136        """Main game loop.
137           There are in fact 2 main loops, this one and the one in GameSceneView.
138           @return: None"""
139        if self.listener.quit:
140            self.breakRequested = True
141        else:
142            self.controller.pump()
Note: See TracBrowser for help on using the repository browser.