commit:42de7ac41c5e40fc5a189e3ca023b7ec8dfa4411
author:Chip Black
committer:Chip Black
date:Sun Jul 27 14:03:04 2008 -0500
parents:84948fe8a45614e0006e6159cc66a823eceeeb86
Added event handling support

Actor class now has several (not completely implemented) event handling
stubs, which are filled in by the Level loader.  The Zombie has been
modified to implement wall collisions with the new hitWall event.
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])
 
 

diff --git a/classes/Zombie.py b/classes/Zombie.py
line changes: +14/-13
index 3e3956c..f0157e3
--- a/classes/Zombie.py
+++ b/classes/Zombie.py
@@ -15,8 +15,16 @@ class Zombie(Actor):
 		self.setState('walk_r')
 		self.direction = 0
 		self.disabled = 0
-		self.topbox = pygame.Rect(self.states['walk_l'].hitbox)
-		self.topbox.height = 10
+
+
+	def hitWall(self):
+		if self.direction == 0:
+			self.direction = 1
+			self.setState('walk_l')
+		elif self.direction == 1:
+			self.direction = 0
+			self.setState('walk_r')
+
 
 	def update(self):
 		if self.disabled > 0:
@@ -32,18 +40,11 @@ class Zombie(Actor):
 					self.setState('walk_l')
 		else:
 			if self.direction == 0:
-				if self.move((1, 0)):
-					self.direction = 1
-					self.setState('walk_l')
+				self.move((1, 0))
 			elif self.direction == 1:
-				if self.move((-1, 0)):
-					self.direction = 0
-					self.setState('walk_r')
-
-			self.topbox.bottom = self.position[1] + 72
-			self.topbox.left = self.position[0] - 31
+				self.move((-1, 0))
 
 			# put in future collide event
-			#if self.topbox.collidepoint(richter.position):
+			#if self.topbox.collidepoint(player.position):
 			#	self.disabled = 120
-			#	richter.bounce()
+			#	player.bounce()