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.
(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
import sys
import os
-import imp
+import re
import yaml
from Collage import Collage
from Surface import SurfaceSet
classpath = ['classes']
sys.path += classpath
+function_re = re.compile('^(\w+)\(([^)]*)\)$')
+
class Layer:
"""Container for layer data.
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])
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:
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()