commit:cc9e297eb6e647dd39a21a78a34d97d0fd56485b
author:Chip Black
committer:Chip Black
date:Sun Jul 27 14:03:04 2008 -0500
parents:022cd9cdfc0eb90a797d6ea4635f93b0a463b1da
Added event handling support

Actor class now has several (not completely implemented) event handling
stubs, which are filled in by the Level loader.
diff --git a/Richter/Actor.py b/Richter/Actor.py
line changes: +17/-0
index 672b5b7..07a4ee6
--- a/Richter/Actor.py
+++ b/Richter/Actor.py
@@ -102,9 +102,26 @@ class Actor:
 		(collided, delta) = self.moveDelta(delta)
 		self.position[0] += delta[0]
 		self.position[1] += delta[1]
+		if collided:
+			self.hitWall()
 		return collided
 
 
+	def hitWall(self):
+		"Called when this object hits a wall after attempting to move"
+		pass
+
+
+	def collideStart(self, obj):
+		"Called when this object begins colliding with another"
+		pass
+
+
+	def collideEnd(self, obj):
+		"Called when this object stops colliding with another"
+		pass
+
+
 	def update(self):
 		"Override this function. Called once per frame."
 		pass

diff --git a/Richter/Level.py b/Richter/Level.py
line changes: +18/-1
index 20936bf..d27a79c
--- a/Richter/Level.py
+++ b/Richter/Level.py
@@ -1,6 +1,6 @@
 import sys
 import os
-import imp
+import re
 import yaml
 from Collage import Collage
 from Surface import SurfaceSet
@@ -9,6 +9,8 @@ from OpenGL.GL import *
 classpath = ['classes']
 sys.path += classpath
 
+function_re = re.compile('^(\w+)\(([^)]*)\)$')
+
 
 class Layer:
 	"""Container for layer data.
@@ -101,6 +103,21 @@ class Level:
 			properties['surfaces'] = layer.surfaces
 
 			self.env[name] = eval('%s(**properties)' % t['class'], self.env, {'properties': properties})
+
+			if t.has_key('events'):
+				for (fdef, str) in t['events'].iteritems():
+					m = function_re.match(fdef)
+					if not m:
+						print "Malformed function definition:", fdef
+						continue
+
+					code = "def %s:\n" % m.group(0)
+					for s in str.split("\n"):
+						if not s: continue
+						code += "\t" + s + "\n"
+					code += "%s.%s = %s\n" % (name, m.group(1), m.group(1))
+					code += "del %s\n" % m.group(1)
+					exec code in self.env
 			layer.add(self.env[name])