Mercurial > parpg-source
changeset 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 | 94196a3e9c07 |
files | gamestate.py systems/scriptingsystem.py |
diffstat | 2 files changed, 22 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/gamestate.py Thu Mar 01 14:49:09 2012 +0100 +++ b/gamestate.py Fri Mar 02 19:42:26 2012 +0100 @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with PARPG. If not, see <http://www.gnu.org/licenses/>. +import math + from parpg.quest_engine import QuestEngine class GameState(object): @@ -28,8 +30,10 @@ self.npcs_met = set() self.funcs = { "meet":self.meet, - "met":self.met - } + "met":self.met, + "sqrt":math.sqrt, + "log":math.log, + } def addObject(self, object_id, map_id, game_object): @@ -164,4 +168,11 @@ @type npc: str @param npc: The NPC's name or id @return: None""" - return npc in self.npcs_met \ No newline at end of file + return npc in self.npcs_met + + def getGameEnvironment(self): + """Returns a 2 item list containing the entities and functions that + can work with it. This can be used in functions like eval or exec""" + vals = {} + vals.update(self.getObjectDictOfMap(self.current_map_name)) + return vals, self.funcs \ No newline at end of file
--- 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))