changeset 157:79d6b17b80a3

Implemented simple script system.
author KarstenBock@gmx.net
date Sat, 12 Nov 2011 16:27:39 +0100
parents 0655f2f0f3cd
children 04854cf6e1ac
files entities/action.py systems/__init__.py systems/scriptingsystem.py world.py
diffstat 4 files changed, 67 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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"""
--- 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
--- 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 <http://www.gnu.org/licenses/>.
 
 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
--- 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: