changeset 193:728eabc921f2

Implemented loading scripts from files.
author KarstenBock@gmx.net
date Thu, 17 Nov 2011 20:36:08 +0100
parents 191f89a22303
children 8dd835103854
files src/parpg/gamemodel.py src/parpg/gamescenecontroller.py src/parpg/systems/scriptingsystem.py
diffstat 3 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/parpg/gamemodel.py	Sun Nov 13 17:19:14 2011 +0100
+++ b/src/parpg/gamemodel.py	Thu Nov 17 20:36:08 2011 +0100
@@ -353,7 +353,30 @@
         for agent in agents:
             if not agent == None:
                 self.addAgent(map_name, agent)  
-    
+        
+    def readScriptsOfMap(self, map_name, world):
+        """Read the scripts of the map
+        @param map_name: Name of the map
+        @type map_name: str 
+        @param world: The current active world
+        @type world: parpg.world.World"""
+        map_scripts_file = (
+            self.map_files[map_name].replace(".xml", "_scripts.yaml")
+        )
+        if vfs.VFS.exists(map_scripts_file):
+            scripts_file = vfs.VFS.open(map_scripts_file)
+            scripts_data = yaml.load(scripts_file)
+            scripts = (scripts_data["Scripts"])
+            conditions = (
+                scripts_data["Conditions"] if 
+                scripts_data.has_key("Conditions") else ()
+            )
+            scripting = world.systems.scripting
+            for name, actions in scripts.iteritems():
+                scripting.setScript(name, actions)
+            for condition in conditions:
+                scripting.addCondition(*condition)            
+            
     def readAllAgents(self):
         """Read the agents of the all_agents_file and store them"""
         agents_file = vfs.VFS.open(self.all_agents_file)
@@ -601,10 +624,12 @@
         self.game_state.clearObjects()
         self.game_state.maps = {}
         
-    def setActiveMap(self, map_name):
+    def setActiveMap(self, map_name, world):
         """Sets the active map that is to be rendered.
            @type map_name: String
            @param map_name: The name of the map to load
+           @param world: The active world
+           @type world: parpg.world.World
            @return: None"""
         # Turn off the camera on the old map before we turn on the camera
         # on the new map.
--- a/src/parpg/gamescenecontroller.py	Sun Nov 13 17:19:14 2011 +0100
+++ b/src/parpg/gamescenecontroller.py	Thu Nov 17 20:36:08 2011 +0100
@@ -355,10 +355,10 @@
                 )
                 for obj in deleted:
                     obj.delete()
+            self.model.loadMap(self.model.target_map_name)
+            self.setupScripts(self.model.target_map_name)
             
-            self.model.loadMap(self.model.target_map_name)
-            
-            self.model.setActiveMap(self.model.target_map_name)          
+            self.model.setActiveMap(self.model.target_map_name, self)          
             
             self.model.placeAgents(self)
             self.model.placePC(self)
@@ -372,6 +372,11 @@
             self.view.hud.initializeInventory()         
             self.pause(False)
 
+    def setupScripts(self, map_name):
+        """Read scripts for the current map"""
+        self.systems.scripting.reset()
+        self.model.readScriptsOfMap(map_name, self)
+
     def handleScrolling(self):
         """
         Merge kb and mouse related scroll data, limit the speed and
@@ -538,6 +543,7 @@
             self.model.loadMap(self.model.game_state.current_map_name)   
         self.model.placeAgents(self)
         self.model.placePC(self)
+        self.setupScripts(self.model.game_state.current_map_name)
         self.view.hud.initializeInventory()          
 
     def quitGame(self):
--- a/src/parpg/systems/scriptingsystem.py	Sun Nov 13 17:19:14 2011 +0100
+++ b/src/parpg/systems/scriptingsystem.py	Thu Nov 17 20:36:08 2011 +0100
@@ -82,12 +82,17 @@
         """Constructor"""
         self.funcs = {}
         self.vals = {}
-        self.scripts = {}
         self.commands = commands
-        self.conditions = []
         self.actions = actions
         self.game_state = None
+        self.reset()
 
+    def reset(self):
+        """Resets the script and condition collections"""
+        self.scripts = {}
+        self.conditions = []
+        
+    
     def step(self, dt):
         """Execute a time step for the system. Must be defined
         by all system classes.
@@ -121,7 +126,10 @@
         """Sets a script.
         @param name: The name of the script
         @param actions: What the script does
+        @type actions: deque or iterable
         """
+        if not(isinstance(actions, deque)):
+            actions = deque(actions)
         self.scripts[name] = Script(actions, 
                                     self
                                     )