diff systems/scriptingsystem.py @ 160:75c0b728ccf3

Further work on the scripting system.
author KarstenBock@gmx.net
date Sun, 13 Nov 2011 17:19:14 +0100
parents 1b66e1ce226b
children d224bbce512a
line wrap: on
line diff
--- a/systems/scriptingsystem.py	Sun Nov 13 13:37:24 2011 +0100
+++ b/systems/scriptingsystem.py	Sun Nov 13 17:19:14 2011 +0100
@@ -11,24 +11,31 @@
 #   You should have received a copy of the GNU General Public License
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from collections import deque
+from copy import deepcopy
+
 from parpg.bGrease import System
-from collections import deque
 
 class Script(object):
     """Script object"""
 
-    def __init__(self, condition, actions, system):
+    def __init__(self, actions, system):
         """Constructor"""
         assert(isinstance(actions, deque))
-        self.condition = condition
         self.actions = actions
+        assert(isinstance(system, ScriptingSystem))
         self.system = system
+        self.reset()
+
+    def reset(self):
+        """Resets the state of the script"""
+        self.running_actions = deepcopy(self.actions)
         self.running = False
         self.finished = False
         self.time = 0
         self.wait = 0
         self.cur_action = None
-
+    
     def update(self, time):
         """Advance the script"""
         if not self.running:
@@ -39,12 +46,21 @@
         if self.wait <= self.time:
             self.time = 0		    
             try:
-                action = self.actions.popleft()
-                self.cur_action = action[0]
-                self.wait = action[1]
-                if len(action) >= 3:
-                    vals = action[3:] if len(action) > 3 else ()
-                    command = action[2]
+                action_data = self.running_actions.popleft()
+                action = self.system.actions[action_data[0]]
+                action_params = eval(action_data[1], 
+                                          self.system.funcs, 
+                                          self.system.vals
+                                          ) 
+                self.cur_action = action(self.system.world, action_params)
+                self.wait = action_data[2]
+                if len(action_data) >= 4:
+                    vals = (
+                        eval(action_data[4], self.system.funcs, self.system.vals) 
+                        if len(action_data) > 4
+                        else ()
+                    )
+                    command = action_data[3]
                     self.system.commands[command](
                         *vals, 
                         action=self.cur_action
@@ -62,12 +78,15 @@
     their behavior.
     """
 
-    def __init__(self, funcs, commands):
+    def __init__(self, commands, actions):
         """Constructor"""
-        self.funcs = funcs
+        self.funcs = {}
         self.vals = {}
-        self.scripts = []
+        self.scripts = {}
         self.commands = commands
+        self.conditions = []
+        self.actions = actions
+        self.game_state = None
 
     def step(self, dt):
         """Execute a time step for the system. Must be defined
@@ -76,11 +95,50 @@
         :param dt: Time since last step invocation
         :type dt: float
         """
-        for script in self.scripts:
+        self.vals.clear()        
+        self.vals.update(
+            self.game_state.getObjectDictOfMap(
+                self.game_state.current_map_name)
+        )
+        self.funcs.clear()
+        self.funcs.update(self.game_state.funcs)
+        for condition_data in self.conditions:
+            condition = condition_data[0]
+            script_name = condition_data[1]
+            if not self.scripts.has_key(script_name):
+                return
+            script = self.scripts[script_name]
+            if eval(condition, self.funcs, self.vals) and not script.running:
+                script.running = True
+        for script in self.scripts.itervalues():
             assert(isinstance(script, Script))
             if script.finished:
-                self.scripts.remove(script)
+                script.reset()
             elif script.running:
                 script.update(dt)
-            elif eval(script.condition, self.funcs, self.vals):
-                script.running = True
\ No newline at end of file
+                
+    def setScript(self, name, actions):
+        """Sets a script.
+        @param name: The name of the script
+        @param actions: What the script does
+        """
+        self.scripts[name] = Script(actions, 
+                                    self
+                                    )
+        
+    def addCondition(self, condition, script_name):
+        """Adds a condition.
+        @param condition: Condition which will be evaluated
+        @param script_name: Name of the script that will be executed if the
+        condition evaluates to True.
+        """
+        self.conditions.append((condition, script_name))
+    
+    
+    def runScript(self, name):
+        """Runs a script with the given name
+        @param name: The name of the script"""
+        if self.scripts.has_key(name):
+            self.scripts[name].running = True
+        
+    
\ No newline at end of file