Mercurial > parpg-source
comparison 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 |
comparison
equal
deleted
inserted
replaced
173:3abd31885f0f | 174:230d316cc43b |
---|---|
11 # You should have received a copy of the GNU General Public License | 11 # You should have received a copy of the GNU General Public License |
12 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 12 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
13 | 13 |
14 from collections import deque | 14 from collections import deque |
15 from copy import deepcopy | 15 from copy import deepcopy |
16 import math | |
17 | 16 |
18 from parpg.bGrease import System | 17 from parpg.bGrease import System |
19 | 18 |
20 class Script(object): | 19 class Script(object): |
21 """Script object""" | 20 """Script object""" |
45 return | 44 return |
46 self.time += time | 45 self.time += time |
47 if self.wait <= self.time: | 46 if self.wait <= self.time: |
48 self.time = 0 | 47 self.time = 0 |
49 try: | 48 try: |
49 globals, locals = self.system.game_state.getGameEnvironment() | |
50 action_data = self.running_actions.popleft() | 50 action_data = self.running_actions.popleft() |
51 action = self.system.actions[action_data[0]] | 51 action = self.system.actions[action_data[0]] |
52 action_params = eval(action_data[1], | 52 action_params = eval( |
53 *self.system.getEnvironment() | 53 action_data[1], |
54 ) | 54 globals, locals |
55 ) | |
55 if not (isinstance(action_params, list) | 56 if not (isinstance(action_params, list) |
56 or isinstance(action_params, tuple)): | 57 or isinstance(action_params, tuple)): |
57 action_params = [action_params] | 58 action_params = [action_params] |
58 self.cur_action = action(self.system.world, *action_params) | 59 self.cur_action = action(self.system.world, *action_params) |
59 self.wait = action_data[2] | 60 self.wait = action_data[2] |
60 if len(action_data) >= 4: | 61 if len(action_data) >= 4: |
61 vals = ( | 62 vals = ( |
62 eval(action_data[4], *self.system.getEnvironment()) | 63 eval(action_data[4], globals, locals) |
63 if len(action_data) > 4 | 64 if len(action_data) > 4 |
64 else () | 65 else () |
65 ) | 66 ) |
66 command = action_data[3] | 67 command = action_data[3] |
67 self.system.commands[command]( | 68 self.system.commands[command]( |
81 their behavior. | 82 their behavior. |
82 """ | 83 """ |
83 | 84 |
84 def __init__(self, commands, actions): | 85 def __init__(self, commands, actions): |
85 """Constructor""" | 86 """Constructor""" |
86 self.__funcs = {} | |
87 self.common_funcs = { | |
88 "sqrt":math.sqrt, | |
89 "log":math.log, | |
90 } | |
91 self.__vals = {} | |
92 self.commands = commands | 87 self.commands = commands |
93 self.actions = actions | 88 self.actions = actions |
94 self.game_state = None | 89 self.game_state = None |
95 self.reset() | 90 self.reset() |
96 | 91 |
97 def reset(self): | 92 def reset(self): |
98 """Resets the script and condition collections""" | 93 """Resets the script and condition collections""" |
99 self.scripts = {} | 94 self.scripts = {} |
100 self.conditions = [] | 95 self.conditions = [] |
101 | |
102 def getEnvironment(self): | |
103 """Returns the globals and locals used by the scripting system""" | |
104 return self.__vals, self.__funcs | |
105 | |
106 | |
107 def updateEnvironment(self): | |
108 self.__vals.clear() | |
109 self.__vals.update(self.game_state.getObjectDictOfMap(self.game_state.current_map_name)) | |
110 self.__funcs.clear() | |
111 self.__funcs.update(self.game_state.funcs) | |
112 self.__funcs.update(self.common_funcs) | |
113 | 96 |
114 def step(self, dt): | 97 def step(self, dt): |
115 """Execute a time step for the system. Must be defined | 98 """Execute a time step for the system. Must be defined |
116 by all system classes. | 99 by all system classes. |
117 | 100 |
118 :param dt: Time since last step invocation | 101 :param dt: Time since last step invocation |
119 :type dt: float | 102 :type dt: float |
120 """ | 103 """ |
121 self.updateEnvironment() | |
122 for condition_data in self.conditions: | 104 for condition_data in self.conditions: |
123 condition = condition_data[0] | 105 condition = condition_data[0] |
124 script_name = condition_data[1] | 106 script_name = condition_data[1] |
125 if not self.scripts.has_key(script_name): | 107 if not self.scripts.has_key(script_name): |
126 return | 108 return |
127 script = self.scripts[script_name] | 109 script = self.scripts[script_name] |
128 if eval(condition, *self.getEnvironment()) and not script.running: | 110 if (eval(condition, *self.game_state.getGameEnvironment()) |
111 and not script.running): | |
129 script.running = True | 112 script.running = True |
130 for script in self.scripts.itervalues(): | 113 for script in self.scripts.itervalues(): |
131 assert(isinstance(script, Script)) | 114 assert(isinstance(script, Script)) |
132 if script.finished: | 115 if script.finished: |
133 script.reset() | 116 script.reset() |