comparison 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
comparison
equal deleted inserted replaced
159:1b66e1ce226b 160:75c0b728ccf3
9 # GNU General Public License for more details. 9 # GNU General Public License for more details.
10 # 10 #
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
15 from copy import deepcopy
16
14 from parpg.bGrease import System 17 from parpg.bGrease import System
15 from collections import deque
16 18
17 class Script(object): 19 class Script(object):
18 """Script object""" 20 """Script object"""
19 21
20 def __init__(self, condition, actions, system): 22 def __init__(self, actions, system):
21 """Constructor""" 23 """Constructor"""
22 assert(isinstance(actions, deque)) 24 assert(isinstance(actions, deque))
23 self.condition = condition
24 self.actions = actions 25 self.actions = actions
26 assert(isinstance(system, ScriptingSystem))
25 self.system = system 27 self.system = system
28 self.reset()
29
30 def reset(self):
31 """Resets the state of the script"""
32 self.running_actions = deepcopy(self.actions)
26 self.running = False 33 self.running = False
27 self.finished = False 34 self.finished = False
28 self.time = 0 35 self.time = 0
29 self.wait = 0 36 self.wait = 0
30 self.cur_action = None 37 self.cur_action = None
31 38
32 def update(self, time): 39 def update(self, time):
33 """Advance the script""" 40 """Advance the script"""
34 if not self.running: 41 if not self.running:
35 return 42 return
36 if self.cur_action and not self.cur_action.executed: 43 if self.cur_action and not self.cur_action.executed:
37 return 44 return
38 self.time += time 45 self.time += time
39 if self.wait <= self.time: 46 if self.wait <= self.time:
40 self.time = 0 47 self.time = 0
41 try: 48 try:
42 action = self.actions.popleft() 49 action_data = self.running_actions.popleft()
43 self.cur_action = action[0] 50 action = self.system.actions[action_data[0]]
44 self.wait = action[1] 51 action_params = eval(action_data[1],
45 if len(action) >= 3: 52 self.system.funcs,
46 vals = action[3:] if len(action) > 3 else () 53 self.system.vals
47 command = action[2] 54 )
55 self.cur_action = action(self.system.world, action_params)
56 self.wait = action_data[2]
57 if len(action_data) >= 4:
58 vals = (
59 eval(action_data[4], self.system.funcs, self.system.vals)
60 if len(action_data) > 4
61 else ()
62 )
63 command = action_data[3]
48 self.system.commands[command]( 64 self.system.commands[command](
49 *vals, 65 *vals,
50 action=self.cur_action 66 action=self.cur_action
51 ) 67 )
52 else: 68 else:
60 """ 76 """
61 System responsible for managing scripts attached to entities to define 77 System responsible for managing scripts attached to entities to define
62 their behavior. 78 their behavior.
63 """ 79 """
64 80
65 def __init__(self, funcs, commands): 81 def __init__(self, commands, actions):
66 """Constructor""" 82 """Constructor"""
67 self.funcs = funcs 83 self.funcs = {}
68 self.vals = {} 84 self.vals = {}
69 self.scripts = [] 85 self.scripts = {}
70 self.commands = commands 86 self.commands = commands
87 self.conditions = []
88 self.actions = actions
89 self.game_state = None
71 90
72 def step(self, dt): 91 def step(self, dt):
73 """Execute a time step for the system. Must be defined 92 """Execute a time step for the system. Must be defined
74 by all system classes. 93 by all system classes.
75 94
76 :param dt: Time since last step invocation 95 :param dt: Time since last step invocation
77 :type dt: float 96 :type dt: float
78 """ 97 """
79 for script in self.scripts: 98 self.vals.clear()
99 self.vals.update(
100 self.game_state.getObjectDictOfMap(
101 self.game_state.current_map_name)
102 )
103 self.funcs.clear()
104 self.funcs.update(self.game_state.funcs)
105 for condition_data in self.conditions:
106 condition = condition_data[0]
107 script_name = condition_data[1]
108 if not self.scripts.has_key(script_name):
109 return
110 script = self.scripts[script_name]
111 if eval(condition, self.funcs, self.vals) and not script.running:
112 script.running = True
113 for script in self.scripts.itervalues():
80 assert(isinstance(script, Script)) 114 assert(isinstance(script, Script))
81 if script.finished: 115 if script.finished:
82 self.scripts.remove(script) 116 script.reset()
83 elif script.running: 117 elif script.running:
84 script.update(dt) 118 script.update(dt)
85 elif eval(script.condition, self.funcs, self.vals): 119
86 script.running = True 120 def setScript(self, name, actions):
121 """Sets a script.
122 @param name: The name of the script
123 @param actions: What the script does
124 """
125 self.scripts[name] = Script(actions,
126 self
127 )
128
129 def addCondition(self, condition, script_name):
130 """Adds a condition.
131 @param condition: Condition which will be evaluated
132 @param script_name: Name of the script that will be executed if the
133 condition evaluates to True.
134 """
135 self.conditions.append((condition, script_name))
136
137
138 def runScript(self, name):
139 """Runs a script with the given name
140 @param name: The name of the script"""
141 if self.scripts.has_key(name):
142 self.scripts[name].running = True
143
144