source: trunk/game/scripts/world.py @ 316

Revision 315, 10.7 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #2: Patch by eliedebrauwer. Performed some cleanup on scripts/common and scripts/objects. comment[s:trac, t:66]

  • Property svn:eol-style set to native
RevLine 
[26]1#!/usr/bin/python
2
[30]3#   This file is part of PARPG.
[26]4
[30]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.
[26]9
[30]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.
[26]14
[30]15#   You should have received a copy of the GNU General Public License
[35]16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
[26]17
[313]18import time
19import fife
[154]20import pychan
[253]21from sounds import SoundEngine
[56]22from datetime import date
[26]23from scripts.common.eventlistenerbase import EventListenerBase
[264]24from local_loaders.loaders import loadMapFile
25from sounds import SoundEngine
[26]26from settings import Setting
[270]27from scripts import hud
[189]28from scripts.popups import *
[74]29from pychan.tools import callbackWithArguments as cbwa
[253]30from map import Map
[26]31
32TDS = Setting()
33
[55]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
[26]42class World(EventListenerBase):
[30]43    """World holds the data needed by fife to render the engine
[54]44       The engine keeps a copy of this class"""
[30]45    def __init__(self, engine):
[104]46        """Constructor for engine
[141]47           @type engine: fife.Engine
48           @param engine: A fife.Engine instance
49           @return: None"""
[315]50        super(World, self).__init__(engine, reg_mouse=True, reg_keys=True)
[128]51        # self.engine is a fife.Engine object, not an Engine object
[66]52        self.engine = engine
[313]53        self.event_manager = engine.getEventManager()
[66]54        self.quitFunction = None
[253]55       
[128]56        # self.data is an engine.Engine object, but is set in run.py
[68]57        self.data = None
[66]58        self.mouseCallback = None
[77]59        self.obj_hash={}
[270]60
[128]61        # self.map is a Map object, set to none here
[313]62        self.active_map = None
[253]63        self.maps = {}
[261]64
[270]65        # setup the inventory model
66        # make slot 'A1' and 'A3' container daggers
67        inv_model = {'A1':'dagger01', 'A3':'dagger01'}
68
69        hud_callbacks = {
70            'saveGame': self.saveGame,
71            'loadGame': self.loadGame,
72            'quitGame': self.quitGame,
73        }
74        self.hud = hud.Hud(self.engine, TDS, inv_model, hud_callbacks)
[74]75        self.action_number = 1
[270]76
[253]77        # init the sound
78        self.sounds = SoundEngine(engine)
79       
[140]80        # don't force restart if skipping to new section
[160]81        if (TDS.readSetting("PlaySounds") == "1"):
82            if(self.sounds.music_init == False):
83                self.sounds.playMusic("/music/preciouswasteland.ogg")
[253]84               
[270]85    def quitGame(self):
86        """Quits the game
87        @return: None"""
88        self.quitFunction()
89
90    def saveGame(self, *args, **kwargs):
91        """Saves the game state
[261]92           @return: None"""
93        self.data.save(*args, **kwargs)
94
[270]95    def loadGame(self, *args, **kwargs):
96        """Loads the game state
[261]97           @return: None"""
98        self.data.load(*args, **kwargs)
99
[313]100    def loadMap(self, map_name, filename):
[302]101        """Loads a map and stores it under the given name in the maps list.
[313]102           @type map_name: text
103           @param map_name: The name of the map to load
[302]104           @type filename: text
105           @param filename: File which contains the map to be loaded
106           @return: None
[253]107        """
[313]108        if not map_name in self.maps:
[302]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)
[313]114            self.maps[map_name] = map       
115            self.setActiveMap(map_name)
[302]116            map.load(filename)
[253]117   
[313]118    def setActiveMap(self, map_name):
[253]119        """Sets the active map that is to be rendered.
[313]120           @type map_name: text
121           @param map_name: The name of the map to load
[302]122           @return: None
[253]123        """
[313]124        self.active_map = self.maps[map_name]
125        self.active_map.makeActive()
[26]126
[77]127    def displayObjectText(self, obj, text):
[141]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"""
[120]134        obj.say(str(text), 1000)
[77]135
[54]136    # all key / mouse event handling routines go here
[30]137    def keyPressed(self, evt):
[141]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"""
[66]142        key = evt.getKey()
[313]143        key_val = key.getValue()
[26]144
[313]145        if(key_val == key.Q):
[58]146            # we need to quit the game
[261]147            self.hud.quitGame()
[313]148        if(key_val == key.T):
149            self.active_map.toggle_renderer('GridRenderer')
150        if(key_val == key.F1):
[58]151            # display the help screen and pause the game
[261]152            self.hud.displayHelp()
[313]153        if(key_val == key.F5):
[46]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())
[313]157        if(key_val == key.F7):
[56]158            # F7 saves a screenshot to fife/clients/parpg/screenshots
[313]159            t = "screenshots/screen-%s-%s.png" % \
160                    (date.today().strftime('%Y-%m-%d'),\
161                    time.strftime('%H-%M-%S'))
[91]162            print "PARPG: Saved:",t
[58]163            self.engine.getRenderBackend().captureScreen(t)
[313]164        if(key_val == key.F10):
[221]165            # F10 shows/hides the console
166            self.engine.getGuiManager().getConsole().toggleShowHide()
[313]167        if(key_val == key.I):
[72]168            # I opens and closes the inventory
[270]169            self.hud.toggleInventory()
[313]170        if(key_val == key.A):
[74]171            # A adds a test action to the action box
[313]172            # The test actions will follow this format: Action 1,
173            # Action 2, etc.
[74]174            self.hud.addAction("Action " + str(self.action_number))
175            self.action_number += 1
[313]176        if(key_val == key.ESCAPE):
[74]177            # Escape brings up the main menu
178            self.hud.displayMenu()
[294]179            # Hide the quit menu
[313]180            self.hud.quit_window.hide()
181        if(key_val == key.M):
[135]182            self.sounds.toggleMusic()
[63]183
[156]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.
[141]188           @type evt: fife.event
189           @param evt: The event that fife caught
190           @return: None"""
[313]191        self.hud.hideContextMenu()
[157]192        scr_point = fife.ScreenPoint(evt.getX(), evt.getY())
[68]193        if(evt.getButton() == fife.MouseEvent.LEFT):
[157]194            self.data.handleMouseClick(self.getCoords(scr_point))     
[68]195        elif(evt.getButton() == fife.MouseEvent.RIGHT):
[121]196            # is there an object here?
[313]197            instances = self.active_map.cameras['main'].\
198                            getMatchingInstances(scr_point, \
199                                                 self.active_map.agent_layer)
[150]200            info = None
[157]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               
[313]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)]]
[157]212            # show the menu
[261]213            self.hud.showContextMenu(data, (scr_point.x, scr_point.y))
[156]214
[150]215    def onWalk(self, click):
[277]216        """Callback sample for the context menu."""
[270]217        self.hud.hideContainer()
[312]218        self.data.game_state.PC.run(click)
[115]219
[310]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)
[313]230        location = fife.Location(self.active_map.agent_layer)
[310]231        location.setMapCoordinates(coord)
[312]232        self.data.game_state.PC.teleport(location)
[310]233
[104]234    def mouseMoved(self, evt):
[141]235        """Called when the mouse is moved
236           @type evt: fife.event
237           @param evt: The event that fife caught
238           @return: None"""
[104]239        click = fife.ScreenPoint(evt.getX(), evt.getY())
[313]240        i=self.active_map.cameras['main'].getMatchingInstances(click, \
241                                                self.active_map.agent_layer)
[104]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
[150]246                if(self.data.objectActive(obj.getId())):
[264]247                    # yes, so outline
[313]248                    self.active_map.outline_render.addOutlined(obj, 0, \
249                                                               137, 255, 2)
[120]250                    # get the text
[150]251                    item = self.data.objectActive(obj.getId())
[313]252                    if(item is not None):
[225]253                        self.displayObjectText(obj, item.name)
[104]254        else:
255            # erase the outline
[313]256            self.active_map.outline_render.removeAllOutlines()
[104]257
[141]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"""
[313]264        coord = self.active_map.cameras["main"].toMapCoordinates(click, False)
[141]265        coord.z = 0
[313]266        location = fife.Location(self.active_map.agent_layer)
[141]267        location.setMapCoordinates(coord)
268        return location
269
[30]270    def pump(self):
[54]271        """Routine called during each frame. Our main loop is in ./run.py
[141]272           We ignore this main loop (but FIFE complains if it is missing)."""
[30]273        pass
Note: See TracBrowser for help on using the repository browser.