diff systems/scriptingsystem.py @ 174:230d316cc43b

Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
author Beliar
date Fri, 02 Mar 2012 19:42:26 +0100
parents 3abd31885f0f
children 2a12e2843984
line wrap: on
line diff
--- a/systems/scriptingsystem.py	Thu Mar 01 14:49:09 2012 +0100
+++ b/systems/scriptingsystem.py	Fri Mar 02 19:42:26 2012 +0100
@@ -13,7 +13,6 @@
 
 from collections import deque
 from copy import deepcopy
-import math
 
 from parpg.bGrease import System
 
@@ -47,11 +46,13 @@
         if self.wait <= self.time:
             self.time = 0		    
             try:
+                globals, locals = self.system.game_state.getGameEnvironment()
                 action_data = self.running_actions.popleft()
                 action = self.system.actions[action_data[0]]
-                action_params = eval(action_data[1], 
-                                          *self.system.getEnvironment()
-                                          ) 
+                action_params = eval(
+                                     action_data[1], 
+                                     globals, locals
+                                     ) 
                 if not (isinstance(action_params, list) 
                         or isinstance(action_params, tuple)):
                     action_params = [action_params]
@@ -59,7 +60,7 @@
                 self.wait = action_data[2]
                 if len(action_data) >= 4:
                     vals = (
-                        eval(action_data[4], *self.system.getEnvironment()) 
+                        eval(action_data[4], globals, locals) 
                         if len(action_data) > 4
                         else ()
                     )
@@ -83,12 +84,6 @@
 
     def __init__(self, commands, actions):
         """Constructor"""
-        self.__funcs = {}        
-        self.common_funcs = {
-                                "sqrt":math.sqrt,
-                                "log":math.log,   
-                            }
-        self.__vals = {}
         self.commands = commands
         self.actions = actions
         self.game_state = None
@@ -98,18 +93,6 @@
         """Resets the script and condition collections"""
         self.scripts = {}
         self.conditions = []
-        
-    def getEnvironment(self):
-        """Returns the globals and locals used by the scripting system"""
-        return self.__vals, self.__funcs
-    
-
-    def updateEnvironment(self):
-        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)
-        self.__funcs.update(self.common_funcs)
 
     def step(self, dt):
         """Execute a time step for the system. Must be defined
@@ -118,14 +101,14 @@
         :param dt: Time since last step invocation
         :type dt: float
         """
-        self.updateEnvironment()
         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.getEnvironment()) and not script.running:
+            if (eval(condition, *self.game_state.getGameEnvironment()) 
+                and not script.running):
                 script.running = True
         for script in self.scripts.itervalues():
             assert(isinstance(script, Script))