# HG changeset patch # User prock@33b003aa-7bff-0310-803a-e67f0ece8222 # Date 1275419850 0 # Node ID 2e739ae9a8bc9bfe8b92643f8ec7f10cc6794062 # Parent 4c7b5eee211c0c023253f258ed7d6247b0ad3092 Some misc code cleanup. Added the EnterPortalAction action. You can now move between town and level1. diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/maps/allobjects.xml --- a/demos/rpg/maps/allobjects.xml Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/maps/allobjects.xml Tue Jun 01 19:17:30 2010 +0000 @@ -2,8 +2,9 @@ type : ITEM ; file : objects/actors/player/warrior/object.xml ; model : goldstack - type : ITEM ; file : objects/items/goldstack/object.xml ; model : goldstack + type : GOLD ; file : objects/items/goldstack/object.xml ; model : goldstack type : QUESTGIVER ; file : objects/actors/player/warrior/object.xml ; model : warrior + type : PORTAL ; file : objects/ground/temple_entrance/object.xml ; model : temple_entrance diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/maps/level1.xml --- a/demos/rpg/maps/level1.xml Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/maps/level1.xml Tue Jun 01 19:17:30 2010 +0000 @@ -1,10 +1,6 @@ - + - - - - @@ -153,6 +149,14 @@ + + + + + + + + diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/maps/level1_objects.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/rpg/maps/level1_objects.xml Tue Jun 01 19:17:30 2010 +0000 @@ -0,0 +1,11 @@ + + + + gstack1 ; temple + typename : GoldStack ; value : 5000 ; posx : -3.0 ; posy : -3.0 + typename : TempleEntrance ; dest : town ; posx : 3.0 ; posy : 2.0 + + + + + diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/maps/town_objects.xml --- a/demos/rpg/maps/town_objects.xml Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/maps/town_objects.xml Tue Jun 01 19:17:30 2010 +0000 @@ -1,8 +1,9 @@ - gstack1 + gstack1 ; temple typename : GoldStack ; value : 5000 ; posx : 3.0 ; posy : 3.0 + typename : TempleEntrance ; dest : level1 ; posx : 3.0 ; posy : -2.0 Quiller diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/actors/baseactor.py --- a/demos/rpg/scripts/actors/baseactor.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/actors/baseactor.py Tue Jun 01 19:17:30 2010 +0000 @@ -71,6 +71,15 @@ def execute(self): self._actor.pickUpItem(self._item) + +class EnterPortalAction(BaseAction): + def __init__(self, actor, portal): + self._actiontype = Actions["ENTER"] + self._actor = actor + self._portal = portal + + def execute(self): + self._actor.enterPortal(self._portal) ActorStates = {'STAND':0, 'WALK':1, @@ -86,17 +95,11 @@ self._object.performNextAction() class Actor(BaseGameObject): - def __init__(self, gamecontroller, instancename, instanceid=None, createInstance=False): - - if not hasattr(self, "_type"): - self._type = GameObjectTypes["NPC"] - - super(Actor, self).__init__(gamecontroller, instancename, instanceid, createInstance) + def __init__(self, gamecontroller, actortype, instancename, instanceid=None, createInstance=False): + super(Actor, self).__init__(gamecontroller, actortype, instancename, instanceid, createInstance) self._walkspeed = self._gamecontroller.settings.get("RPG", "DefaultActorWalkSpeed", 4.0) - self._actionlistener = ActorActionListener(self._gamecontroller, self) - self._nextaction = None self._inventory = [] self._maxinventoryitems = 20 @@ -104,7 +107,7 @@ self._gold = 0 self.stand() - + def stand(self): self._state = ActorStates["STAND"] self._instance.act('stand', self._instance.getFacingLocation()) @@ -130,8 +133,14 @@ else: self._inventory.append(item) - item.onPickUp() + item.onLeftClick() + def enterPortal(self, portal): + if self._id == "player": + self._gamecontroller.switchMap(portal.dest) + else: + self._gamecontroller.scene.removeObjectFromScene(self._id) + def removeItemFromInventory(self, itemid): itemtoremove = None for item in self._inventory: diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/actors/player.py --- a/demos/rpg/scripts/actors/player.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/actors/player.py Tue Jun 01 19:17:30 2010 +0000 @@ -27,10 +27,10 @@ import sys, os, re, math, random, shutil from fife import fife -from scripts.actors.baseactor import Actor, ActorStates -from scripts.objects.baseobject import ObjectActionListener, BaseGameObject, GameObjectTypes +from scripts.actors.baseactor import Actor, ActorStates, ActorActionListener +from scripts.objects.baseobject import BaseGameObject, GameObjectTypes -class PlayerActionListener(ObjectActionListener): +class PlayerActionListener(ActorActionListener): def __init__(self, gamecontroller, obj): super(PlayerActionListener, self).__init__(gamecontroller, obj) @@ -38,12 +38,11 @@ super(PlayerActionListener, self).onInstanceActionFinished(instance, action) if action.getId() == 'walk': pass - #self._object.completeAction() class Player(Actor): def __init__(self, gamecontroller, playermodelname): - self._type = GameObjectTypes["PLAYER"] - super(Player, self).__init__(gamecontroller, playermodelname, "player", True) + super(Player, self).__init__(gamecontroller, GameObjectTypes["PLAYER"], playermodelname, "player", True) self._playermodelname = playermodelname - self._playeractionlistener = PlayerActionListener(self._gamecontroller, self) + self._actionlistener = PlayerActionListener(self._gamecontroller, self) + self._actionlistener.attachActionListener() diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/actors/questgiver.py --- a/demos/rpg/scripts/actors/questgiver.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/actors/questgiver.py Tue Jun 01 19:17:30 2010 +0000 @@ -33,8 +33,7 @@ class QuestGiver(Actor): def __init__(self, gamecontroller, instancename, instanceid=None, createInstance=False): - self._type = GameObjectTypes["QUESTGIVER"] - super(QuestGiver, self).__init__(gamecontroller, instancename, instanceid, createInstance) + super(QuestGiver, self).__init__(gamecontroller, GameObjectTypes["QUESTGIVER"], instancename, instanceid, createInstance) self._quests = [] diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/gamecontroller.py --- a/demos/rpg/scripts/gamecontroller.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/gamecontroller.py Tue Jun 01 19:17:30 2010 +0000 @@ -33,7 +33,7 @@ from scripts.scene import Scene from scripts.guicontroller import GUIController -from scripts.actors.baseactor import TalkAction, PickUpItemAction +from scripts.actors.baseactor import TalkAction, PickUpItemAction, EnterPortalAction from scripts.objects.baseobject import GameObjectTypes from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError @@ -60,7 +60,6 @@ self._gamecontroller = gamecontroller self._settings = gamecontroller.settings self._eventmanager = self._engine.getEventManager() - self._soundmanager = SoundManager(self._engine) fife.IMouseListener.__init__(self) fife.IKeyListener.__init__(self) @@ -108,8 +107,10 @@ if obj.type == GameObjectTypes["ITEM"]: action = PickUpItemAction(self._gamecontroller.scene.player, obj) self._gamecontroller.scene.player.nextaction = action + elif obj.type == GameObjectTypes["PORTAL"]: + action = EnterPortalAction(self._gamecontroller.scene.player, obj) + self._gamecontroller.scene.player.nextaction = action - if (event.getButton() == fife.MouseEvent.RIGHT): instances = self._gamecontroller.scene.getInstancesAt(clickpoint, self._gamecontroller.scene.actorlayer) if instances: @@ -184,6 +185,8 @@ self._engine = engine self._settings = settings + self._soundmanager = SoundManager(self._engine) + self._keystate = KeyState() self._guicontroller = GUIController(self) @@ -196,6 +199,9 @@ self._instancerenderer = None self._floatingtextrenderer = None + self._switchmaprequested = False + self._newmap = None + def onConsoleCommand(self, command): """ Might be useful if you want to have the game parse a command. @@ -252,6 +258,14 @@ def newGame(self): self._guicontroller.hideMainMenu() + mapname = self._settings.get("RPG", "TownMapFile", "town") + self.loadMap(mapname) + + + def loadMap(self, mapname): + if self._listener: + self._listener.detach() + self._keystate.reset() if self._scene: @@ -259,17 +273,22 @@ self._scene = None self._scene = Scene(self) - self._scene.createScene(self._settings.get("RPG", "TownMapFile", "maps/town.xml")) - + self._scene.createScene("maps/" + mapname + ".xml") + self._instancerenderer = fife.InstanceRenderer.getInstance(self._scene.cameras[self._settings.get("RPG", "DefaultCameraName", "camera1")]) self._floatingtextrenderer = fife.FloatingTextRenderer.getInstance(self._scene.cameras[self._settings.get("RPG", "DefaultCameraName", "camera1")]) self._floatingtextrenderer.addActiveLayer(self._scene.actorlayer) + + if self._listener: + self._listener.attach() - #start listening to events - self._listener.attach() - + def switchMap(self, newmapname): + self._switchmaprequested = True + self._newmap = newmapname + def endGame(self): if self._scene: + self._listener.detach() self._scene.destroyScene() self._scene = None self._instancerenderer = None @@ -280,6 +299,11 @@ self._application.requestQuit() def pump(self): + if self._switchmaprequested: + self.loadMap(self._newmap) + self._newmap = None + self._switchmaprequested = False + if self._scene: self._scene.updateScene() diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/objects/baseobject.py --- a/demos/rpg/scripts/objects/baseobject.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/objects/baseobject.py Tue Jun 01 19:17:30 2010 +0000 @@ -35,22 +35,29 @@ "QUESTGIVER":2, "PLAYER":3, "NPC":4, - "ENEMY":5 + "ENEMY":5, + "GOLD":6, + "PORTAL":7 } class ObjectActionListener(fife.InstanceActionListener): def __init__(self, gamecontroller, obj): fife.InstanceActionListener.__init__(self) - obj.instance.addActionListener(self) self._gamecontroller = gamecontroller self._object = obj + + def detachActionListener(self): + self._object.instance.removeActionListener(self) + + def attachActionListener(self): + self._object.instance.addActionListener(self) def onInstanceActionFinished(self, instance, action): pass class BaseGameObject(object): - def __init__(self, gamecontroller, instancename, instanceid=None, createInstance=False): + def __init__(self, gamecontroller, objtype, 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 @@ -72,35 +79,36 @@ self._instance = None self._position = fife.DoublePoint(0.0, 0.0) - if not hasattr(self, "_type"): - self._type = GameObjectTypes["DEFAULT"] + self._actionlistener = None + + self._type = objtype if createInstance: - if self._type == GameObjectTypes["ITEM"]: - layer = self._gamecontroller.scene.itemlayer + if self._type == GameObjectTypes["ITEM"] or self._type == GameObjectTypes["PORTAL"]: + self._layer = self._gamecontroller.scene.itemlayer else: - layer = self._gamecontroller.scene.actorlayer + self._layer = self._gamecontroller.scene.actorlayer - self._createFIFEInstance(layer) + self._createFIFEInstance(self._layer) else: - self._instance = self._gamecontroller.scene.actorlayer.getInstance(self._id) + self._instance = self._layer.getInstance(self._id) self._instance.thisown = 0 self._activated = True - def destroy(self): """ Deletes the FIFE instance from the actor layer on the map. """ + if self._actionlistener: + self._actionlistener.detachActionListener() + self._actionlistener = None + if self._instance : - if self._type == GameObjectTypes["ITEM"]: - layer = self._gamecontroller.scene.itemlayer - else: - layer = self._gamecontroller.scene.actorlayer - - layer.deleteInstance(self._instance) - self._instance = None + self._layer.deleteInstance(self._instance) + self._instance = None + + self._activated = False def setMapPosition(self, x, y): curloc = self.location diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/objects/items.py --- a/demos/rpg/scripts/objects/items.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/objects/items.py Tue Jun 01 19:17:30 2010 +0000 @@ -32,11 +32,10 @@ class BaseItem(BaseGameObject): - def __init__(self, gamecontroller, itemtype, itemname): - self._type = GameObjectTypes["ITEM"] - super(BaseItem, self).__init__(gamecontroller, itemtype, itemname, True) + def __init__(self, gamecontroller, objtype, itemtype, itemname): + super(BaseItem, self).__init__(gamecontroller, objtype, itemtype, itemname, True) - def onPickUp(self): + def onLeftClick(self): #remove item from the scene self._gamecontroller.scene.removeObjectFromScene(self) @@ -58,7 +57,7 @@ class GoldStack(BaseItem): def __init__(self, gamecontroller, itemtype, itemname): - super(GoldStack, self).__init__(gamecontroller, itemtype, itemname) + super(GoldStack, self).__init__(gamecontroller, GameObjectTypes["ITEM"], itemtype, itemname) self._value = 0 @@ -69,3 +68,20 @@ self._value = int(value) value = property(_getValue, _setValue) + +class Portal(BaseItem): + def __init__(self, gamecontroller, itemtype, itemname): + super(Portal, self).__init__(gamecontroller, GameObjectTypes["PORTAL"], itemtype, itemname) + + self._dest = None + + def onLeftClick(self): + pass + + def _getDest(self): + return self._dest + + def _setDest(self, dest): + self._dest = dest + + dest = property(_getDest, _setDest) diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/quests/basequest.py --- a/demos/rpg/scripts/quests/basequest.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/quests/basequest.py Tue Jun 01 19:17:30 2010 +0000 @@ -30,6 +30,9 @@ from fife import fife class Quest(object): + """ + @todo: do a little refactoring here to split out the type of quests. + """ def __init__(self, owner, questname, questtext): self._owner = owner self._name = questname diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/scripts/scene.py --- a/demos/rpg/scripts/scene.py Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/scripts/scene.py Tue Jun 01 19:17:30 2010 +0000 @@ -36,7 +36,7 @@ from scripts.quests.basequest import Quest from scripts.actors.player import Player from scripts.objects.baseobject import GameObjectTypes -from scripts.objects.items import BaseItem, GoldStack +from scripts.objects.items import BaseItem, GoldStack, Portal from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError class Scene(object): @@ -63,9 +63,13 @@ loadImportFile(modeldict["file"], self._gamecontroller.engine) - if itemdict["typename"] == "GoldStack": + if modeldict["type"] == "GOLD": newitem = GoldStack(self._gamecontroller, modeldict["model"], itemid) newitem.value = itemdict["value"] + elif modeldict["type"] == "PORTAL": + print "portal" + newitem = Portal(self._gamecontroller, modeldict["model"], itemid) + newitem.dest = itemdict["dest"] else: newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid) @@ -113,6 +117,7 @@ for item in self._objectsettings.get("items", "itemlist", []): try: newitem = self.loadItem(item) + self._gamecontroller.logger.log_debug("Loaded item: " + item) except ObjectNotFoundError, e: self._gamecontroller.logger.log_error("Error while loading item: " + item) continue @@ -126,8 +131,9 @@ for npc in self._objectsettings.get("npcs", "npclist", []): try: actor = self.loadActor(npc) + self._gamecontroller.logger.log_debug("Loaded actor: " + npc) except ObjectNotFoundError, e: - self._gamecontroller.logger.log_error("Error while loading actor:" + actor) + self._gamecontroller.logger.log_error("Error while loading actor:" + npc) continue try: @@ -139,19 +145,17 @@ """ @todo: once we have all art assets this should be able to load one of 3 player models """ - modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") - modelsettings = Setting(app_name="", settings_file=modelfile) - modeldict = modelsettings.get("models", "Player", {}) + modeldict = self._modelsettings.get("models", "Player", {}) loadImportFile(modeldict["file"], self._gamecontroller.engine) self._player = Player(self._gamecontroller, "warrior") def createScene(self, mapfilename): - if not self._map: - self._map = loadMapFile(mapfilename, self._gamecontroller.engine) - else: + if self._map: self.destroyScene() + self._map = loadMapFile(mapfilename, self._gamecontroller.engine) + self._mapname = os.path.splitext(os.path.basename(mapfilename))[0] objectfile = "maps/" + self._mapname + "_objects.xml" modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") @@ -162,8 +166,8 @@ for cam in self._map.getCameras(): self._cameras[cam.getId()] = cam - self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 2.0)) - + self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 1.0)) + self._actorlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ActorLayer", "actor_layer")) self._itemlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ItemLayer", "item_layer")) @@ -172,18 +176,26 @@ #finally load the player self.loadPlayer() - def destroyScene(self): - self._cameras.clear() - - self._player.destroy() - for obj in self._objectlist.values(): obj.destroy() + self._objectlist.clear() + self._objectlist = {} + self._cameras.clear() + self._cameras = {} + + self._player.destroy() + self._player = None + + for cam in self._map.getCameras(): + cam.resetRenderers() + if self._map: self._gamecontroller.engine.getModel().deleteMap(self._map) + + retval = self._gamecontroller.engine.getModel().deleteObjects() self._map = None self._mapname = None @@ -191,9 +203,6 @@ self._actorlayer = None self._itemlayer = None - self._player = None - self._objectlist.clear() - self._objectsettings = None self._modelsettings = None diff -r 4c7b5eee211c -r 2e739ae9a8bc demos/rpg/settings-dist.xml --- a/demos/rpg/settings-dist.xml Tue Jun 01 18:28:25 2010 +0000 +++ b/demos/rpg/settings-dist.xml Tue Jun 01 19:17:30 2010 +0000 @@ -22,9 +22,8 @@ - maps/town.xml + town maps/allobjects.xml - maps/level1.xml camera1 1.0 http://www.fifengine.de/xml/rpg