comparison systems/scriptingsystem.py @ 157:79d6b17b80a3

Implemented simple script system.
author KarstenBock@gmx.net
date Sat, 12 Nov 2011 16:27:39 +0100
parents e856b604b650
children 1b66e1ce226b
comparison
equal deleted inserted replaced
156:0655f2f0f3cd 157:79d6b17b80a3
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 parpg.bGrease import System 14 from parpg.bGrease import System
15 from collections import deque
16
17 class Script(object):
18 """Script object"""
19
20 def __init__(self, condition, actions, commands):
21 """Constructor"""
22 assert(isinstance(actions, deque))
23 self.condition = condition
24 self.actions = actions
25 self.commands = commands
26 self.running = False
27 self.finished = False
28 self.time = 0
29 self.wait = 0
30 self.cur_action = None
31
32 def update(self, time):
33 """Advance the script"""
34 if not self.running:
35 return
36 if self.cur_action and not self.cur_action.executed:
37 return
38 self.time += time
39 if self.wait <= self.time:
40 self.time = 0
41 try:
42 action = self.actions.popleft()
43 self.cur_action = action[0]
44 self.wait = action[1]
45 if len(action) >= 3:
46 vals = action[3:] if len(action) > 3 else ()
47 command = action[2]
48 self.commands[command](*vals, action=self.cur_action)
49 else:
50 self.cur_action.execute()
51 except IndexError:
52 self.finished = True
53 self.running = False
54
15 55
16 class ScriptingSystem(System): 56 class ScriptingSystem(System):
17 """ 57 """
18 System responsible for managing scripts attached to entities to define 58 System responsible for managing scripts attached to entities to define
19 their behavior. 59 their behavior.
20 """ 60 """
21 pass 61
62 def __init__(self, funcs):
63 """Constructor"""
64 self.funcs = funcs
65 self.vals = {}
66 self.scripts = []
67
68 def step(self, dt):
69 """Execute a time step for the system. Must be defined
70 by all system classes.
71
72 :param dt: Time since last step invocation
73 :type dt: float
74 """
75 for script in self.scripts:
76 assert(isinstance(script, Script))
77 if script.finished:
78 self.scripts.remove(script)
79 elif script.running:
80 script.update(dt)
81 elif eval(script.condition, self.funcs, self.vals):
82 script.running = True