commit:5ad9010143e548abdf1e44590cb9eb0c2e6444a5
author:Chip Black
committer:Chip Black
date:Sun Jul 27 02:22:36 2008 -0500
parents:a0fd02f419ee5d0a2413dceb0e3417f8a400e7e2
Further modifications to support Level loading

YAML level format no longer supports arbitrary class loading; leaving
Sprite support would have complicated things unnecessarily.  Now all
level things must be Actor subclasses. (Maybe rename that section to
"actors"?)  Level loader automagically loads from the classes directory,
which now contains the three examples from platformtest.py.
diff --git a/Richter/Actor.py b/Richter/Actor.py
line changes: +2/-2
index bc1bb84..672b5b7
--- a/Richter/Actor.py
+++ b/Richter/Actor.py
@@ -1,11 +1,11 @@
 from Sprite import *
 
 class Actor:
-	def __init__(self, surfaces=None):
+	def __init__(self, surfaces = None, position = [0,0]):
 		self.states = {}
 		self.currentstate = None
 		self.surfaces = surfaces
-		self.position = [0,0]
+		self.position = position
 
 	
 	def defineState(self, statename, sprite):

diff --git a/Richter/Level.py b/Richter/Level.py
line changes: +8/-3
index 2c5671f..365780a
--- a/Richter/Level.py
+++ b/Richter/Level.py
@@ -1,3 +1,4 @@
+import sys
 import os
 import imp
 import yaml
@@ -5,7 +6,10 @@ from Collage import Collage
 from Surface import SurfaceSet
 from Stage import Stage, Layer
 
-classpath = ['classes', 'Richter']
+classpath = ['classes']
+for path in classpath:
+	sys.path.append(path)
+
 
 def findClass(classname):
 	"A fun bit of metaprogramming, loading a class based on its name"
@@ -32,13 +36,12 @@ class Level:
 		self.stage = Stage()
 
 		for layer in layers:
-			print layer
 			collage = None
 			surfaces = None
 			scale = 1.0
 			parallax = 1.0
 
-			print 'Name:', layer['name']
+			print 'Creating layer', layer['name']
 			if layer.has_key('collage'):
 				collage = Collage(os.path.join(dir, 'collages', layer['collage']))
 			if layer.has_key('surfaces'):
@@ -58,9 +61,11 @@ class Level:
 
 	def compileThings(self, layer, things):
 		for (name, t) in things.iteritems():
+			print "Creating", t['class'], name
 			c = findClass(t['class'])
 			properties = {}
 			if t.has_key('properties'):
 				properties = t['properties']
+			properties['surfaces'] = layer.surfaces
 			self.env[name] = c(**properties)
 			layer.add(self.env[name])

diff --git a/classes/Candelabra.py b/classes/Candelabra.py
line changes: +11/-0
index 0000000..c7eb293
--- /dev/null
+++ b/classes/Candelabra.py
@@ -0,0 +1,11 @@
+from Richter.Actor import Actor
+from Richter.Sprite import *
+
+class Candelabra(Actor):
+	def __init__(self, **kwargs):
+		Actor.__init__(self, **kwargs)
+
+		self.defineState('default',
+			Sprite(['Sprites/candelabra_short/cand_s_%d.png' % i for i in range(1,4)], scale=2.0, gravity=(CENTER,BOTTOM), framerate=10.0)
+		)
+		self.setState('default')

diff --git a/classes/Richter.py b/classes/Richter.py
line changes: +2/-2
index 9337591..afd3fbd
--- a/classes/Richter.py
+++ b/classes/Richter.py
@@ -1,5 +1,5 @@
-from Actor import Actor
-from Sprite import *
+from Richter.Actor import Actor
+from Richter.Sprite import *
 
 class Richter(Actor):
 	def __init__(self, **kwargs):

diff --git a/classes/Zombie.py b/classes/Zombie.py
line changes: +47/-0
index 0000000..63c676c
--- /dev/null
+++ b/classes/Zombie.py
@@ -0,0 +1,47 @@
+from Richter.Actor import Actor
+from Richter.Sprite import *
+
+class Zombie(Actor):
+	def __init__(self, **kwargs):
+		Actor.__init__(self, **kwargs)
+
+		self.defineState('walk_r',
+			Sprite(['Sprites/zombie soldier/zombiesoldierwalk_%d.png' % n for n in range(1,5)],
+			framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
+		self.defineState('walk_l',
+			Sprite(['Sprites/zombie soldier/zombiesoldierwalk_%d.png' % n for n in range(1,5)],
+			mirror=True, framerate=10.0, scale=2.0, gravity=(CENTER,BOTTOM)))
+
+		self.setState('walk_r')
+		self.direction = 0
+		self.disabled = 0
+		self.topbox = pygame.Rect(self.states['walk_l'].hitbox)
+		self.topbox.height = 10
+
+	def update(self):
+		if self.disabled > 0:
+			if self.disabled % 2:
+				self.setState('walk_r')
+			else:
+				self.setState('walk_l')
+			self.disabled -= 1
+			if self.disabled == 0:
+				if self.direction == 0:
+					self.setState('walk_r')
+				else:
+					self.setState('walk_l')
+		else:
+			if self.direction == 0:
+				if self.move((1, 0)):
+					self.direction = 1
+					self.setState('walk_l')
+			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
+			if self.topbox.collidepoint(richter.position):
+				self.disabled = 120
+				richter.bounce()

diff --git a/levels/example1/level.yaml b/levels/example1/level.yaml
line changes: +2/-13
index fd7173c..668aeb7
--- a/levels/example1/level.yaml
+++ b/levels/example1/level.yaml
@@ -14,19 +14,8 @@
           print "region"
 
     candelabra:
-      class: Sprite
-      properties:
-        file:
-          - Sprites/candelabra_short/cand_s_1.png
-          - Sprites/candelabra_short/cand_s_2.png
-          - Sprites/candelabra_short/cand_s_3.png
-          - Sprites/candelabra_short/cand_s_4.png
-        position: [64,64]
-        scale: 2.0
-        gravity: [CENTER,BOTTOM]
-        framerate: 10.0
-      init: |
-        candelabra.play()
+      class: Candelabra
+      properties: {position: [64,64]}
 
     zombie1:
       class: Zombie