changeset 216:023eea4ad4a5

Add support for trigger world changes when entering and exiting dialog states.
author Simon Cross <hodgestar@gmail.com>
date Thu, 07 Apr 2011 00:47:02 +0200
parents 4ac8ef4b44c3
children fcc5eca8eaca
files skaapsteker/dialogue.py
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/dialogue.py	Thu Apr 07 00:38:47 2011 +0200
+++ b/skaapsteker/dialogue.py	Thu Apr 07 00:47:02 2011 +0200
@@ -40,7 +40,9 @@
         state = self.states[self.state]
         next_state = state.event(my_locals)
         if next_state.name in self.states:
+            self.states[self.state].leave(my_locals)
             self.state = next_state.name
+            self.states[self.state].enter(my_locals)
 
     def choice(self, i):
         self.event(DsmEvent(choice=i))
@@ -92,6 +94,18 @@
             pseudo_path = base_path + ["auto_next"]
             self.triggers.append(Trigger("""True""", auto_next, pseudo_path))
 
+        on_entry = state_src.get("on_entry", None)
+        if on_entry is not None:
+            self.on_entry = compile(on_entry,
+                                    "<%s>" % ":".join(base_path + ["on_entry"]),
+                                    "exec")
+
+        on_exit = state_src.get("on_exit", None)
+        if on_exit is not None:
+            self.on_exit = compile(on_exit,
+                                   "<%s>" % ":".join(base_path + ["on_exit"]),
+                                   "exec")
+
     def __repr__(self):
         return "<%r name=%r>" % (self.__class__.__name__, self.name)
 
@@ -101,6 +115,12 @@
             if next_state is not None:
                 return next_state
 
+    def enter(self, my_locals):
+        self.on_entry(my_locals, {}, my_locals.copy())
+
+    def leave(self, my_locals):
+        self.on_exit(my_locals, {}, my_locals.copy())
+
 
 class Trigger(object):
     """Matches DSM events and triggers state transitions.