Mercurial > fife-parpg
changeset 513:edf5c0cf52f3
Added the Actor and Player classes. Actor is the base class that the player and all enemies + NPCs will inherit.
Added a help file for the console.
Added a KeyState class for keeping track of the state of the keys.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 21 May 2010 20:33:43 +0000 |
parents | 6ddb1eb9dfa6 |
children | 0cdc727f9f66 |
files | demos/rpg/maps/level1.xml demos/rpg/maps/town.xml demos/rpg/misc/help.txt demos/rpg/scripts/actors/__init__.py demos/rpg/scripts/actors/baseactor.py demos/rpg/scripts/actors/player.py demos/rpg/scripts/gamecontroller.py demos/rpg/scripts/rpg.py demos/rpg/scripts/scene.py demos/rpg/settings-dist.xml |
diffstat | 9 files changed, 196 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/demos/rpg/maps/level1.xml Fri May 21 17:31:08 2010 +0000 +++ b/demos/rpg/maps/level1.xml Fri May 21 20:33:43 2010 +0000 @@ -1,6 +1,10 @@ <?xml version="1.0" encoding="ascii"?> <map id="maintown" format="1.0"> <import file="../objects/ground/grass/01/object.xml"></import> + <layer y_scale="1.0" y_offset="0.0" pathing="cell_edges_only" grid_type="square" id="actor_layer" rotation="0.0" x_scale="1.0" x_offset="0.0" transparency="0"> + <instances> + </instances> + </layer> <layer y_scale="1.0" y_offset="0.0" pathing="cell_edges_only" grid_type="square" id="ground_layer" rotation="0.0" x_scale="1.0" x_offset="0.0" transparency="0"> <instances> <i o="grass:01" ns="http://www.fifengine.de/xml/rpg" x="-2.0" r="0" y="0.0" z="0.0"></i> @@ -149,6 +153,6 @@ <i r="0" x="4.0" o="grass:01" z="0.0" y="8.0"></i> </instances> </layer> - <camera ref_cell_height="64" zoom="1.0" rotation="45.0" ref_layer_id="ground_layer" ref_cell_width="64" id="camera1" tilt="60.0"> + <camera ref_cell_height="64" zoom="1.0" rotation="45.0" ref_layer_id="ground_layer" ref_cell_width="64" id="camera1" tilt="-60.0"> </camera> </map>
--- a/demos/rpg/maps/town.xml Fri May 21 17:31:08 2010 +0000 +++ b/demos/rpg/maps/town.xml Fri May 21 20:33:43 2010 +0000 @@ -1,6 +1,10 @@ <?xml version="1.0" encoding="ascii"?> <map id="maintown" format="1.0"> <import file="../objects/ground/grass/01/object.xml"></import> + <layer y_scale="1.0" y_offset="0.0" pathing="cell_edges_only" grid_type="square" id="actor_layer" rotation="0.0" x_scale="1.0" x_offset="0.0" transparency="0"> + <instances> + </instances> + </layer> <layer y_scale="1.0" y_offset="0.0" pathing="cell_edges_only" grid_type="square" id="ground_layer" rotation="0.0" x_scale="1.0" x_offset="0.0" transparency="0"> <instances> <i o="grass:01" ns="http://www.fifengine.de/xml/rpg" x="-2.0" r="0" y="0.0" z="0.0"></i> @@ -149,6 +153,6 @@ <i r="0" x="4.0" o="grass:01" z="0.0" y="8.0"></i> </instances> </layer> - <camera ref_cell_height="64" zoom="1.0" rotation="45.0" ref_layer_id="ground_layer" ref_cell_width="64" id="camera1" tilt="60.0"> + <camera ref_cell_height="64" zoom="1.0" rotation="45.0" ref_layer_id="ground_layer" ref_cell_width="64" id="camera1" tilt="-60.0"> </camera> </map>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/rpg/misc/help.txt Fri May 21 20:33:43 2010 +0000 @@ -0,0 +1,3 @@ +help - This message +exit - Quit the game +quit - Quit the game
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/rpg/scripts/actors/baseactor.py Fri May 21 20:33:43 2010 +0000 @@ -0,0 +1,85 @@ +#!/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 + +from fife import fife +from fife.extensions.loaders import loadMapFile + +class Actor(object): + def __init__(self, gamecontroller, instancename, instanceid=None, createInstance=False): + """ + @param gamecontroller: A reference to the master game controller + @param instancename: The name of the object to load. The object's XML file must + be part of the map file or added with fife.extensions.loaders.loadImportFile + @param instanceid: used if you want to give a specific ID to the instance to + differenciate it from other instances + @param createInstance: If this is True it will attempt to be loaded from disk and not + use one that has already been loaded from the map file. See the note about the + instancename paramater above. + """ + self._gamecontroller = gamecontroller + self._fifeobject = None + self._name = instancename + if instanceid: + self._id = instanceid + else: + self._id = self._name + + self._instance = None + + if createInstance: + self._createFIFEInstance(self._name) + else: + self._instance = self._gamecontroller.scene.layer.getInstance(self._id) + self._instance.thisown = 0 + + def destroy(self): + """ + Deletes the FIFE instance from the actor layer on the map. + """ + if self._instance : + self._gamecontroller.scene.actorlayer.deleteInstance(self._instance) + self._instance = None + + def _createFIFEInstance(self): + """ + Should not be called directly. Use the constructor! + """ + mapmodel = self._gamecontroller.engine.getModel() + self._fifeobject = mapmodel.getObject(self._name, self._gamecontroller.settings.get("RPG", "ObjectNamespace", "http://www.fifengine.de/xml/rpg")) + + self._instance = self._gamecontroller.scene.actorlayer.createInstance(self._fifeobject, fife.ModelCoordinate(0,0), self._id) + fife.InstanceVisual.create(self._instance) + self._instance.thisown = 0 + + def _getLocation(self): + return self._instance.getLocation() + + def _setLocation(self, loc): + self._instance.setLocation(loc) + + location = property(_getLocation,_setLocation)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/rpg/scripts/actors/player.py Fri May 21 20:33:43 2010 +0000 @@ -0,0 +1,35 @@ +#!/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 + +from fife import fife +from scripts.actors.baseactor import Actor + +class Player(Actor): + def __init__(self, gamecontroller, playermodelname): + super(Player, self).__init__(gamecontroller, playermodelname, "player", True) + self._playermodelname = playermodelname
--- a/demos/rpg/scripts/gamecontroller.py Fri May 21 17:31:08 2010 +0000 +++ b/demos/rpg/scripts/gamecontroller.py Fri May 21 20:33:43 2010 +0000 @@ -31,6 +31,22 @@ from scripts.scene import Scene from scripts.guicontroller import GUIController +class KeyState(object): + def __init__(self): + self._keystate = { } + + def updateKey(self, key, state): + self._keystate[key] = state + + def checkKey(self, key): + if key in self._keystate: + return self._keystate[key] + else: + return False + + def reset(self): + self._keystate.clear() + class GameListener(fife.IKeyListener, fife.IMouseListener): def __init__(self, gamecontroller): self._engine = gamecontroller.engine @@ -83,9 +99,15 @@ self.detach() self._gamecontroller.guicontroller.showMainMenu() event.consume() + + self._gamecontroller.keystate.updateKey(keyval, True) def keyReleased(self, event): - pass + keyval = event.getKey().getValue() + keystr = event.getKey().getAsString().lower() + + self._gamecontroller.keystate.updateKey(keyval, False) + class GameController(object): def __init__(self, application, engine, settings): @@ -93,6 +115,8 @@ self._engine = engine self._settings = settings + self._keystate = KeyState() + self._guicontroller = GUIController(self) self._listener = GameListener(self) @@ -112,6 +136,8 @@ def newGame(self): self._guicontroller.hideMainMenu() + self._keystate.reset() + if self._scene: self._scene.destroyScene() self._scene = None @@ -122,7 +148,13 @@ #start listening to events self._listener.attach() + def endGame(self): + if self._scene: + self._scene.destroyScene() + self._scene = None + def quit(self): + self.endGame() self._application.requestQuit() def pump(self): @@ -139,6 +171,14 @@ def _getSettings(self): return self._settings + def _getScene(self): + return self._scene + + def _getKeyState(self): + return self._keystate + guicontroller = property(_getGUIController) engine = property(_getEngine) settings = property(_getSettings) + scene = property(_getScene) + keystate = property(_getKeyState)
--- a/demos/rpg/scripts/rpg.py Fri May 21 17:31:08 2010 +0000 +++ b/demos/rpg/scripts/rpg.py Fri May 21 20:33:43 2010 +0000 @@ -96,10 +96,10 @@ 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 --" + helptextfile = self._gamecontroller.settings.get("RPG", "HelpText", "misc/help.txt") + self._engine.getGuiManager().getConsole().println( open( helptextfile, 'r' ).read() ) + result = "-OK-" else: - pass result = self._gamecontroller.onConsoleCommand(command) if not result: try: @@ -107,7 +107,7 @@ except: pass if not result: - result = 'no result' + result = 'Command Not Found...' return result def onToolsClick(self): @@ -139,5 +139,6 @@ def _pump(self): if self._listener.quit: self.breakRequested = True + self._gamecontroller.endGame() else: self._gamecontroller.pump()
--- a/demos/rpg/scripts/scene.py Fri May 21 17:31:08 2010 +0000 +++ b/demos/rpg/scripts/scene.py Fri May 21 20:33:43 2010 +0000 @@ -29,6 +29,8 @@ from fife import fife from fife.extensions.loaders import loadMapFile +from scripts.actors.player import Player + class Scene(object): def __init__(self, gamecontroller): self._gamecontroller = gamecontroller @@ -36,6 +38,10 @@ self._map = None self._cameras = {} + self._actorlayer = None + + self._player = None + def createScene(self, mapfilename): if not self._map: self._map = loadMapFile(mapfilename, self._gamecontroller.engine) @@ -47,6 +53,8 @@ self._cameras[self._gamecontroller.settings.get("RPG", "DefaultCameraName", "camera1")].setZoom(1.0) + self._actorlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ActorLayer", "actor_layer")) + def destroyScene(self): self._cameras.clear() @@ -54,7 +62,13 @@ self._gamecontroller.engine.getModel().deleteMap(self._map) self._map = None + self._actorlayer = None def updateScene(self): pass + def _getActorLayer(self): + return self._actorlayer + + actorlayer = property(_getActorLayer) +
--- a/demos/rpg/settings-dist.xml Fri May 21 17:31:08 2010 +0000 +++ b/demos/rpg/settings-dist.xml Fri May 21 20:33:43 2010 +0000 @@ -26,5 +26,8 @@ <Setting name="TownMapFile" type="str"> maps/town.xml </Setting> <Setting name="Level1MapFile" type="str"> maps/level1.xml </Setting> <Setting name="DefaultCameraName" type="str"> camera1 </Setting> + <Setting name="ObjectNamespace" type="str"> http://www.fifengine.de/xml/rpg </Setting> + <Setting name="ActorLayer" type="str"> actor_layer </Setting> + <Setting name="HelpText" type="str"> misc/help.txt </Setting> </Module> </Settings>