changeset 118:29869273f9e1

Fixed moving between maps.
author KarstenBock@gmx.net
date Mon, 26 Sep 2011 15:44:42 +0200
parents b10de0310771
children 3564a46544bc
files src/parpg/controllerbase.py src/parpg/gamemodel.py src/parpg/gamescenecontroller.py src/parpg/gamestate.py
diffstat 4 files changed, 82 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/parpg/controllerbase.py	Sat Sep 24 16:48:06 2011 +0200
+++ b/src/parpg/controllerbase.py	Mon Sep 26 15:44:42 2011 +0200
@@ -17,9 +17,9 @@
 from parpg.common.listeners.key_listener import KeyListener
 from parpg.common.listeners.mouse_listener import MouseListener
 from parpg.common.listeners.command_listener import CommandListener
-from parpg.world import World
+from parpg.mode import FifeMode
 
-class ControllerBase(World, KeyListener, MouseListener, CommandListener):
+class ControllerBase(FifeMode, KeyListener, MouseListener, CommandListener):
     """Base of Controllers"""
     def __init__(self, 
                  engine, 
@@ -42,7 +42,7 @@
         KeyListener.__init__(self, application.event_listener)        
         MouseListener.__init__(self, application.event_listener)
         CommandListener.__init__(self, application.event_listener)
-        World.__init__(self)
+        FifeMode.__init__(self)
         self.engine = engine
         self.event_manager = engine.getEventManager()
         self.view = view
@@ -88,3 +88,5 @@
                            self.model.settings.parpg.CursorDefault])
         self.setMouseCursor(image, image)
     
+    def pump(self, dt):
+        pass
--- a/src/parpg/gamemodel.py	Sat Sep 24 16:48:06 2011 +0200
+++ b/src/parpg/gamemodel.py	Mon Sep 26 15:44:42 2011 +0200
@@ -31,6 +31,7 @@
 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError
 from parpg.entities import createEntity
 from parpg import behaviours
+from parpg import components
 from parpg.components import fifeagent, container, equip
 
 try:
@@ -398,17 +399,17 @@
             new_map.load(map_file)    
 
     def createAgent(self, agent, inst_id, world):
-        entity_data = agent["Entity"]
+        entity_data = deepcopy(agent["Entity"])
         if agent.has_key("Template"):
             entity_data = self.checkAttributes(entity_data, agent["Template"])
-        object_id = agent["Entity"]["fifeagent"]["gfx"] \
-                                if agent["Entity"]["fifeagent"].has_key("gfx") \
+        object_id = entity_data["fifeagent"]["gfx"] \
+                                if entity_data["fifeagent"].has_key("gfx") \
                                 else "generic_item"
         map_obj = self.fife_model.getObject(str(object_id), "PARPG")
         if not map_obj:
             logging.warning("Object with inst_id={0}, ns=PARPG, "
                                   "could not be found. "
-                                  "Omitting...".format(str(obj_id)))
+                                  "Omitting...".format(str(object_id)))
 
         x_pos = agent["Position"][0]
         y_pos = agent["Position"][1]
@@ -599,7 +600,7 @@
             # yes, use the current game state data
             obj.fifeagent.pos.X = ref.X
             obj.fifeagent.pos.Y = ref.Y
-            obj.fifeagent.gfx = ref.gfx  
+            obj.fifeagent.gfx = ref.gfx
              
         if obj.fifeagent.behaviour:
             obj.fifeagent.behaviour.parent = obj
@@ -656,6 +657,54 @@
         for object_info in database:
             self.object_db.update(object_info)
 
+    def updateObjectDB(self, world):
+        """Updates the values in the object database with the worlds values"""
+        
+        for entity in world.entities:
+            identifier = entity.general.identifier
+            agent_data = {}
+            map_id = self.game_state.getMapOfObject(identifier)
+            if not map_id:
+                continue
+            if self.agents[self.ALL_AGENTS_KEY].has_key(identifier):
+                agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
+            else:
+                agent_data = self.agents[map_id][identifier]
+            
+            entity_data = {}
+            entity_data["general"] = {"identifier": identifier}
+            for name, component in components.components.iteritems():
+                if hasattr(entity, name):
+                    comp_data = {}
+                    if name == "fifeagent":
+                        if agent_data["Entity"].has_key("fifeagent"):
+                            comp_data = agent_data["Entity"]["fifeagent"]
+                        if entity.fifeagent.layer:
+                            layer = entity.fifeagent.layer
+                            inst = layer.getInstance(identifier)
+                            loc = inst.getLocation().getExactLayerCoordinates()
+                            agent_data["Position"] = (loc.x, loc.y, loc.z)
+                            agent_data["Map"] = map_id
+                            agent_data["Rotation"]  = inst.getRotation()
+                    elif name == "container":
+                        #TODO: Save Inventory
+                        pass
+                    elif name == "equip":
+                        #TODO: Save Equipment
+                        pass                        
+                    else:
+                        comp_vals = getattr(entity, name)
+                        for field in component.fields:
+                            try:
+                                comp_data[field] = getattr(comp_vals, field)
+                            except AttributeError:                            
+                                #The entity doesn't have this specific value,
+                                #ignore it
+                                pass
+                    entity_data[name] = comp_data
+            agent_data["Entity"] = entity_data
+            
+        
     def getAgentImportFiles(self):
         """Searches the agents directory for import files """
         filepaths = locateFiles("*.xml", self.objects_directory)
--- a/src/parpg/gamescenecontroller.py	Sat Sep 24 16:48:06 2011 +0200
+++ b/src/parpg/gamescenecontroller.py	Mon Sep 26 15:44:42 2011 +0200
@@ -32,6 +32,8 @@
                            UnlockBoxAction, LockBoxAction, TalkAction, \
                            PickUpAction, DropItemAction
 
+from parpg.world import World
+
 #For debugging/code analysis
 if False:
     from gamesceneview import GameSceneView
@@ -41,7 +43,7 @@
 
 logger = logging.getLogger('gamescenecontroller')
 
-class GameSceneController(ControllerBase):
+class GameSceneController(World, ControllerBase):
     '''
     This controller handles inputs when the game is in "scene" state.
     "Scene" state is when the player can move around and interact
@@ -68,6 +70,7 @@
                                 view,
                                 model,
                                 application)
+        World.__init__(self)
         #this can be helpful for IDEs code analysis
         if False:
             assert(isinstance(self.engine, fife.Engine))
@@ -327,18 +330,19 @@
         if self.model.map_change:
             self.pause(True)
             if self.model.active_map:
-                player_char = self.model.game_state.getObjectById("PlayerCharacter").fifeagent
-                self.model.game_state.getObjectById("PlayerCharacter").fifeagent = None
-                pc_agent = self.model.agents[self.model.ALL_AGENTS_KEY]\
-                                                ["PlayerCharacter"]
-                pc_agent.update(player_char.getStateForSaving())
+                self.model.updateObjectDB(self)
+                player_char = self.model.game_state.\
+                    getObjectById("PlayerCharacter").fifeagent
+                pc_agent = self.model.agents\
+                    [self.model.ALL_AGENTS_KEY]["PlayerCharacter"]
                 pc_agent["Map"] = self.model.target_map_name 
                 pc_agent["Position"] = self.model.target_position
-                pc_agent["Inventory"] = \
-                        player_char.inventory.serializeInventory()
                 player_agent = self.model.active_map.\
                                     agent_layer.getInstance("PlayerCharacter")
-                self.model.active_map.agent_layer.deleteInstance(player_agent)
+                self.model.game_state.deleteObject("PlayerCharacter")
+                self.model.game_state.deleteObjectsFromMap(
+                    self.model.game_state.current_map_name
+                )
             
             self.model.loadMap(self.model.target_map_name)
             
--- a/src/parpg/gamestate.py	Sat Sep 24 16:48:06 2011 +0200
+++ b/src/parpg/gamestate.py	Mon Sep 26 15:44:42 2011 +0200
@@ -54,6 +54,7 @@
             if map_id:
                 inst = self.maps[map_id].agent_layer.getInstance(object_id)
                 self.maps[map_id].agent_layer.deleteInstance(inst)
+            self.objects[map_id][object_id].delete()
             del self.objects[map_id][object_id]
             del self.object_ids[object_id]
             
@@ -69,6 +70,15 @@
         
         return {}
     
+    def deleteObjectsFromMap(self, map_id):
+        """Deletes all objects of the given map.
+           @type map: String
+           @param map: The map name.
+           @returns: None"""
+        if map_id in self.objects:
+            for obj in self.objects[map_id].copy():
+                self.deleteObject(obj)
+    
     def hasObject(self, object_id):
         """Check if an object with the given id is present 
         @param object_id: ID of the object