view demos/rpg/scripts/rpg.py @ 512:6ddb1eb9dfa6

Added the Scene which loads the town map. It is no longer a black screen demo. Changed the keys to be more common. Screenshot is now the print screen key. The console is now the backtick key. Screenshots are now date/time stamped as well. Removed the scripts/common directory as I have removed the dependency on the EventListenerBase class.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 21 May 2010 17:31:08 +0000
parents cd959b05a262
children edf5c0cf52f3
line wrap: on
line source

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# ####################################################################
#  Copyright (C) 2005-2010 by the FIFE team
#  http://www.fifengine.net
#  This file is part of FIFE.
#
#  FIFE is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# ####################################################################
# This is the rio de hola client for FIFE.

import sys, os, re, math, random, shutil, time
from datetime import datetime

from fife import fife
from fife.extensions import *
from scripts.gamecontroller import GameController
from fife.extensions.basicapplication import ApplicationBase
from fife.extensions import pychan
from fife.extensions.pychan import widgets
from fife.extensions.fife_utils import getUserDataDirectory

class KeyFilter(fife.IKeyFilter):
	def __init__(self, keys):
		fife.IKeyFilter.__init__(self)
		self._keys = keys

	def isFiltered(self, event):
		return event.getKey().getValue() in self._keys

class ApplicationListener(fife.IKeyListener, fife.ICommandListener, fife.ConsoleExecuter):
	def __init__(self, engine, gamecontroller):
		self._engine = engine
		self._gamecontroller = gamecontroller
		self._eventmanager = self._engine.getEventManager()
		
		fife.IKeyListener.__init__(self)
		self._eventmanager.addKeyListener(self)
		
		fife.ICommandListener.__init__(self)
		self._eventmanager.addCommandListener(self)
		
		fife.ConsoleExecuter.__init__(self)
		self._engine.getGuiManager().getConsole().setConsoleExecuter(self)
		
		keyfilter = KeyFilter([fife.Key.ESCAPE, fife.Key.BACKQUOTE, fife.Key.PRINT_SCREEN])
		keyfilter.__disown__()		
		
		self._eventmanager.setKeyFilter(keyfilter)
		
		self.quit = False

	def keyPressed(self, event):
		keyval = event.getKey().getValue()
		keystr = event.getKey().getAsString().lower()
		
		if event.isConsumed():
			return
		
		if keyval == fife.Key.ESCAPE:
			self.quit = True
			event.consume()
		elif keyval == fife.Key.BACKQUOTE:
			self._engine.getGuiManager().getConsole().toggleShowHide()
			event.consume()
		elif keyval == fife.Key.PRINT_SCREEN:
			self._engine.getRenderBackend().captureScreen(time.strftime("%Y%m%d_%H%M%S", time.localtime()) + ".png")
			event.consume()

	def keyReleased(self, event):
		pass

	def onCommand(self, command):
		self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME)
		if self.quit:
			command.consume()

	def onConsoleCommand(self, command):
		result = ''
		if command.lower() in ('quit', 'exit'):
			self.quit = True
			result = 'quitting'
		elif command.lower() in ( 'help', 'help()' ):
			self._engine.getGuiManager().getConsole().println( open( 'misc/infotext.txt', 'r' ).read() )
			result = "-- End of help --"
		else:
			pass
			result = self._gamecontroller.onConsoleCommand(command)
		if not result:
			try:
				result = str(eval(command))
			except:
				pass
		if not result:
			result = 'no result'
		return result
		
	def onToolsClick(self):
		print "No tools set up yet"

class RPGApplication(ApplicationBase):
	def __init__(self, TDS):
		super(RPGApplication,self).__init__(TDS)
		self._settings = TDS
		
		pychan.init(self.engine, debug=self._settings.get("FIFE", "PychanDebug", False))
		
		self._gamecontroller = GameController(self, self.engine, self._settings)

	def createListener(self):
		"""
		@note: This function had to be overloaded otherwise the default
		listener would have been created.
		"""
		self._listener = ApplicationListener(self.engine, self._gamecontroller)
		return self._listener
		
	def requestQuit(self):
		cmd = fife.Command()
		cmd.setSource(None)
		cmd.setCommandType(fife.CMD_QUIT_GAME)
		self.engine.getEventManager().dispatchCommand(cmd)

	def _pump(self):
		if self._listener.quit:
			self.breakRequested = True
		else:
			self._gamecontroller.pump()