# HG changeset patch # User KarstenBock@gmx.net # Date 1321111659 -3600 # Node ID 79d6b17b80a37690819801502a46c95d81c387a5 # Parent 0655f2f0f3cd0a9621557bdf92b36d13313d6ef0 Implemented simple script system. diff -r 0655f2f0f3cd -r 79d6b17b80a3 entities/action.py --- a/entities/action.py Sat Nov 05 16:08:16 2011 +0100 +++ b/entities/action.py Sat Nov 12 16:27:39 2011 +0100 @@ -44,6 +44,7 @@ self.commands = commands or () self.controller = controller self.model = controller.model + self.executed = False def execute(self): """To be overwritten""" @@ -63,6 +64,7 @@ self.controller.resetMouseCursor() elif command == "StopDragging": data_drag.dragging = False + self.executed = True class ChangeMapAction(Action): """A change map scheduled""" diff -r 0655f2f0f3cd -r 79d6b17b80a3 systems/__init__.py --- a/systems/__init__.py Sat Nov 05 16:08:16 2011 +0100 +++ b/systems/__init__.py Sat Nov 12 16:27:39 2011 +0100 @@ -0,0 +1,1 @@ +from scriptingsystem import ScriptingSystem \ No newline at end of file diff -r 0655f2f0f3cd -r 79d6b17b80a3 systems/scriptingsystem.py --- a/systems/scriptingsystem.py Sat Nov 05 16:08:16 2011 +0100 +++ b/systems/scriptingsystem.py Sat Nov 12 16:27:39 2011 +0100 @@ -12,10 +12,71 @@ # along with this program. If not, see . from parpg.bGrease import System +from collections import deque + +class Script(object): + """Script object""" + + def __init__(self, condition, actions, commands): + """Constructor""" + assert(isinstance(actions, deque)) + self.condition = condition + self.actions = actions + self.commands = commands + self.running = False + self.finished = False + self.time = 0 + self.wait = 0 + self.cur_action = None + + def update(self, time): + """Advance the script""" + if not self.running: + return + if self.cur_action and not self.cur_action.executed: + return + self.time += time + if self.wait <= self.time: + self.time = 0 + try: + action = self.actions.popleft() + self.cur_action = action[0] + self.wait = action[1] + if len(action) >= 3: + vals = action[3:] if len(action) > 3 else () + command = action[2] + self.commands[command](*vals, action=self.cur_action) + else: + self.cur_action.execute() + except IndexError: + self.finished = True + self.running = False + class ScriptingSystem(System): """ System responsible for managing scripts attached to entities to define their behavior. """ - pass \ No newline at end of file + + def __init__(self, funcs): + """Constructor""" + self.funcs = funcs + self.vals = {} + self.scripts = [] + + def step(self, dt): + """Execute a time step for the system. Must be defined + by all system classes. + + :param dt: Time since last step invocation + :type dt: float + """ + for script in self.scripts: + assert(isinstance(script, Script)) + if script.finished: + self.scripts.remove(script) + elif script.running: + script.update(dt) + elif eval(script.condition, self.funcs, self.vals): + script.running = True \ No newline at end of file diff -r 0655f2f0f3cd -r 79d6b17b80a3 world.py --- a/world.py Sat Nov 05 16:08:16 2011 +0100 +++ b/world.py Sat Nov 12 16:27:39 2011 +0100 @@ -3,6 +3,7 @@ from parpg.mode import FifeMode from parpg import components +from parpg.systems import ScriptingSystem class World(FifeMode, BaseWorld): @@ -14,6 +15,7 @@ """Configure the game world's components, systems and renderers""" for name, component in components.components.iteritems(): setattr(self.components, name, component) + self.systems.scripting = ScriptingSystem({}) def pump(self, dt): for component in self.components: