1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # This file is part of PARPG. |
---|
4 | |
---|
5 | # PARPG is free software: you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU General Public License as published by |
---|
7 | # the Free Software Foundation, either version 3 of the License, or |
---|
8 | # (at your option) any later version. |
---|
9 | |
---|
10 | # PARPG is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | # GNU General Public License for more details. |
---|
14 | |
---|
15 | # You should have received a copy of the GNU General Public License |
---|
16 | # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | import time |
---|
19 | import fife |
---|
20 | import pychan |
---|
21 | from sounds import SoundEngine |
---|
22 | from datetime import date |
---|
23 | from scripts.common.eventlistenerbase import EventListenerBase |
---|
24 | from local_loaders.loaders import loadMapFile |
---|
25 | from sounds import SoundEngine |
---|
26 | from settings import Setting |
---|
27 | from scripts import hud |
---|
28 | from scripts.popups import * |
---|
29 | from pychan.tools import callbackWithArguments as cbwa |
---|
30 | from map import Map |
---|
31 | |
---|
32 | TDS = Setting() |
---|
33 | |
---|
34 | # this file should be the meta-file for all FIFE-related code |
---|
35 | # engine.py handles is our data model, whilst this is our view |
---|
36 | # in order to not replicate data, some of our data model will naturally |
---|
37 | # reside on this side of the fence (PC xpos and ypos, for example). |
---|
38 | # we should aim to never replicate any data as this leads to maintainance |
---|
39 | # issues (and just looks plain bad). |
---|
40 | # however, any logic needed to resolve this should sit in engine.py |
---|
41 | |
---|
42 | class World(EventListenerBase): |
---|
43 | """World holds the data needed by fife to render the engine |
---|
44 | The engine keeps a copy of this class""" |
---|
45 | def __init__(self, engine): |
---|
46 | """Constructor for engine |
---|
47 | @type engine: fife.Engine |
---|
48 | @param engine: A fife.Engine instance |
---|
49 | @return: None""" |
---|
50 | super(World, self).__init__(engine, reg_mouse=True, reg_keys=True) |
---|
51 | # self.engine is a fife.Engine object, not an Engine object |
---|
52 | self.engine = engine |
---|
53 | self.event_manager = engine.getEventManager() |
---|
54 | self.quitFunction = None |
---|
55 | |
---|
56 | # self.data is an engine.Engine object, but is set in run.py |
---|
57 | self.data = None |
---|
58 | self.mouseCallback = None |
---|
59 | |
---|
60 | # self.map is a Map object, set to none here |
---|
61 | self.active_map = None |
---|
62 | self.maps = {} |
---|
63 | |
---|
64 | # setup the inventory model |
---|
65 | # make slot 'A1' and 'A3' container daggers |
---|
66 | inv_model = {'A1':'dagger01', 'A3':'dagger01'} |
---|
67 | |
---|
68 | hud_callbacks = { |
---|
69 | 'saveGame': self.saveGame, |
---|
70 | 'loadGame': self.loadGame, |
---|
71 | 'quitGame': self.quitGame, |
---|
72 | } |
---|
73 | self.hud = hud.Hud(self.engine, TDS, inv_model, hud_callbacks) |
---|
74 | self.action_number = 1 |
---|
75 | |
---|
76 | # init the sound |
---|
77 | self.sounds = SoundEngine(engine) |
---|
78 | |
---|
79 | # don't force restart if skipping to new section |
---|
80 | if (TDS.readSetting("PlaySounds") == "1"): |
---|
81 | if(self.sounds.music_init == False): |
---|
82 | self.sounds.playMusic("/music/preciouswasteland.ogg") |
---|
83 | |
---|
84 | def quitGame(self): |
---|
85 | """Quits the game |
---|
86 | @return: None""" |
---|
87 | self.quitFunction() |
---|
88 | |
---|
89 | def saveGame(self, *args, **kwargs): |
---|
90 | """Saves the game state |
---|
91 | @return: None""" |
---|
92 | self.data.save(*args, **kwargs) |
---|
93 | |
---|
94 | def loadGame(self, *args, **kwargs): |
---|
95 | """Loads the game state |
---|
96 | @return: None""" |
---|
97 | print self.data |
---|
98 | self.data.load(*args, **kwargs) |
---|
99 | |
---|
100 | def loadMap(self, map_name, filename): |
---|
101 | """Loads a map and stores it under the given name in the maps list. |
---|
102 | @type map_name: text |
---|
103 | @param map_name: The name of the map to load |
---|
104 | @type filename: text |
---|
105 | @param filename: File which contains the map to be loaded |
---|
106 | @return: None |
---|
107 | """ |
---|
108 | if not map_name in self.maps: |
---|
109 | """Need to set active map before we load it because the map |
---|
110 | loader uses call backs that expect to find an active map. |
---|
111 | This needs to be reworked. |
---|
112 | """ |
---|
113 | map = Map(self.engine, self.data) |
---|
114 | self.maps[map_name] = map |
---|
115 | self.setActiveMap(map_name) |
---|
116 | map.load(filename) |
---|
117 | |
---|
118 | def setActiveMap(self, map_name): |
---|
119 | """Sets the active map that is to be rendered. |
---|
120 | @type map_name: text |
---|
121 | @param map_name: The name of the map to load |
---|
122 | @return: None |
---|
123 | """ |
---|
124 | self.active_map = self.maps[map_name] |
---|
125 | self.active_map.makeActive() |
---|
126 | |
---|
127 | def displayObjectText(self, obj, text): |
---|
128 | """Display on screen the text of the object over the object. |
---|
129 | @type obj: fife.instance |
---|
130 | @param obj: object to draw over |
---|
131 | @type text: text |
---|
132 | @param text: text to display over object |
---|
133 | @return: None""" |
---|
134 | obj.say(str(text), 1000) |
---|
135 | |
---|
136 | # all key / mouse event handling routines go here |
---|
137 | def keyPressed(self, evt): |
---|
138 | """Whenever a key is pressed, fife calls this routine. |
---|
139 | @type evt: fife.event |
---|
140 | @param evt: The event that fife caught |
---|
141 | @return: None""" |
---|
142 | key = evt.getKey() |
---|
143 | key_val = key.getValue() |
---|
144 | |
---|
145 | if(key_val == key.Q): |
---|
146 | # we need to quit the game |
---|
147 | self.hud.quitGame() |
---|
148 | if(key_val == key.T): |
---|
149 | self.active_map.toggle_renderer('GridRenderer') |
---|
150 | if(key_val == key.F1): |
---|
151 | # display the help screen and pause the game |
---|
152 | self.hud.displayHelp() |
---|
153 | if(key_val == key.F5): |
---|
154 | # logic would say we use similar code to above and toggle |
---|
155 | # logic here does not work, my friend :-( |
---|
156 | self.cord_render.setEnabled(not self.cord_render.isEnabled()) |
---|
157 | if(key_val == key.F7): |
---|
158 | # F7 saves a screenshot to fife/clients/parpg/screenshots |
---|
159 | t = "screenshots/screen-%s-%s.png" % \ |
---|
160 | (date.today().strftime('%Y-%m-%d'),\ |
---|
161 | time.strftime('%H-%M-%S')) |
---|
162 | print "PARPG: Saved:",t |
---|
163 | self.engine.getRenderBackend().captureScreen(t) |
---|
164 | if(key_val == key.F10): |
---|
165 | # F10 shows/hides the console |
---|
166 | self.engine.getGuiManager().getConsole().toggleShowHide() |
---|
167 | if(key_val == key.I): |
---|
168 | # I opens and closes the inventory |
---|
169 | self.hud.toggleInventory() |
---|
170 | if(key_val == key.A): |
---|
171 | # A adds a test action to the action box |
---|
172 | # The test actions will follow this format: Action 1, |
---|
173 | # Action 2, etc. |
---|
174 | self.hud.addAction("Action " + str(self.action_number)) |
---|
175 | self.action_number += 1 |
---|
176 | if(key_val == key.ESCAPE): |
---|
177 | # Escape brings up the main menu |
---|
178 | self.hud.displayMenu() |
---|
179 | # Hide the quit menu |
---|
180 | self.hud.quit_window.hide() |
---|
181 | if(key_val == key.M): |
---|
182 | self.sounds.toggleMusic() |
---|
183 | |
---|
184 | def mouseReleased(self, evt): |
---|
185 | """If a mouse button is released, fife calls this routine. |
---|
186 | We want to wait until the button is released, because otherwise |
---|
187 | pychan captures the release if a menu is opened. |
---|
188 | @type evt: fife.event |
---|
189 | @param evt: The event that fife caught |
---|
190 | @return: None""" |
---|
191 | self.hud.hideContextMenu() |
---|
192 | scr_point = fife.ScreenPoint(evt.getX(), evt.getY()) |
---|
193 | if(evt.getButton() == fife.MouseEvent.LEFT): |
---|
194 | self.data.handleMouseClick(self.getCoords(scr_point)) |
---|
195 | elif(evt.getButton() == fife.MouseEvent.RIGHT): |
---|
196 | # is there an object here? |
---|
197 | instances = self.active_map.cameras['main'].\ |
---|
198 | getMatchingInstances(scr_point, \ |
---|
199 | self.active_map.agent_layer) |
---|
200 | info = None |
---|
201 | for inst in instances: |
---|
202 | # check to see if this is an active item |
---|
203 | if(self.data.objectActive(inst.getId())): |
---|
204 | # yes, get the data |
---|
205 | info = self.data.getItemActions(inst.getId()) |
---|
206 | break |
---|
207 | |
---|
208 | # take the menu items returned by the engine or show a |
---|
209 | # default menu if no items |
---|
210 | data = info or \ |
---|
211 | [["Walk", "Walk here", self.onWalk, self.getCoords(scr_point)]] |
---|
212 | # show the menu |
---|
213 | self.hud.showContextMenu(data, (scr_point.x, scr_point.y)) |
---|
214 | |
---|
215 | def onWalk(self, click): |
---|
216 | """Callback sample for the context menu.""" |
---|
217 | self.hud.hideContainer() |
---|
218 | self.data.game_state.PC.run(click) |
---|
219 | |
---|
220 | def teleport(self, position): |
---|
221 | """Called when a door is used that moves a player to a new |
---|
222 | location on the same map. the setting of position may want |
---|
223 | to be created as its own method down the road. |
---|
224 | |
---|
225 | @type position: String Tuple |
---|
226 | @param position: X,Y coordinates passed from enigine.changeMap |
---|
227 | @return: fife.Location |
---|
228 | """ |
---|
229 | coord = fife.DoublePoint3D(float(position[0]), float(position[1]), 0) |
---|
230 | location = fife.Location(self.active_map.agent_layer) |
---|
231 | location.setMapCoordinates(coord) |
---|
232 | self.data.game_state.PC.teleport(location) |
---|
233 | |
---|
234 | def mouseMoved(self, evt): |
---|
235 | """Called when the mouse is moved |
---|
236 | @type evt: fife.event |
---|
237 | @param evt: The event that fife caught |
---|
238 | @return: None""" |
---|
239 | click = fife.ScreenPoint(evt.getX(), evt.getY()) |
---|
240 | i=self.active_map.cameras['main'].getMatchingInstances(click, \ |
---|
241 | self.active_map.agent_layer) |
---|
242 | # no object returns an empty tuple |
---|
243 | if(i != ()): |
---|
244 | for obj in i: |
---|
245 | # check to see if this in our list at all |
---|
246 | if(self.data.objectActive(obj.getId())): |
---|
247 | # yes, so outline |
---|
248 | self.active_map.outline_render.addOutlined(obj, 0, \ |
---|
249 | 137, 255, 2) |
---|
250 | # get the text |
---|
251 | item = self.data.objectActive(obj.getId()) |
---|
252 | if(item is not None): |
---|
253 | self.displayObjectText(obj, item.name) |
---|
254 | else: |
---|
255 | # erase the outline |
---|
256 | self.active_map.outline_render.removeAllOutlines() |
---|
257 | |
---|
258 | def getCoords(self, click): |
---|
259 | """Get the map location x, y cords from the screen co-ords |
---|
260 | @type click: fife.ScreenPoint |
---|
261 | @param click: Screen co-ords |
---|
262 | @rtype: fife.Location |
---|
263 | @return: The map co-ords""" |
---|
264 | coord = self.active_map.cameras["main"].toMapCoordinates(click, False) |
---|
265 | coord.z = 0 |
---|
266 | location = fife.Location(self.active_map.agent_layer) |
---|
267 | location.setMapCoordinates(coord) |
---|
268 | return location |
---|
269 | |
---|
270 | def pump(self): |
---|
271 | """Routine called during each frame. Our main loop is in ./run.py |
---|
272 | We ignore this main loop (but FIFE complains if it is missing).""" |
---|
273 | pass |
---|