commit:9e7d4c5aa2ab29a783b96e98cc24b95e6dbdeb6e
author:Chip Black
committer:Chip Black
date:Fri Apr 18 00:38:04 2008 -0500
parents:c4fa26fbf7a775bc07b6a8bc86ad40cb9f62bea7
Added Stage class for tying together Sprites, Actors, Collages, and
SurfaceSets into a single unit.
diff --git a/Collage.py b/Collage.py
line changes: +1/-5
index 2368791..ee5bb66
--- a/Collage.py
+++ b/Collage.py
@@ -6,7 +6,6 @@ import TexMan
 
 class Collage:
 	tiles = None
-	position = (0,0)
 	displaylist = None
 
 	def __init__(self, file):
@@ -69,10 +68,7 @@ class Collage:
 		glTexCoordPointer(2, GL_FLOAT, 0, array(texcoordarray,'f'))
 		glDrawArrays(GL_QUADS, 0, nvertices)
 		glEndList()
-	
+
 
 	def draw(self):
-		glPushMatrix()
-		glTranslatef(self.position[0], self.position[1], 0.0)
 		glCallList(self.displaylist)
-		glPopMatrix()

diff --git a/Stage.py b/Stage.py
line changes: +89/-0
index 0000000..b6afc2b
--- /dev/null
+++ b/Stage.py
@@ -0,0 +1,89 @@
+from Collage import *
+from Surface import *
+
+class Layer:
+	"""Container for layer data.
+
+	collage - A Collage object defining the visuals of the layer
+	scale - A float value for the size of the background layer
+	parallax - A float value specifying the relative scroll speed (where
+		   1.0 is typically the playfield scroll speed, >1.0 is faster,
+		   and <1.0 is slower)"""
+
+	def __init__(self, collage, scale=1.0, parallax=1.0):
+		if collage:
+			self.collage = Collage(collage)
+		self.scale = scale
+		self.parallax = parallax
+		self.position = (0, 0)
+		self.things = []
+
+
+	def add(self, thing):
+		self.things.append(thing)
+
+
+	def moveTo(self, x, y):
+		self.position = (x * self.parallax, y * self.parallax)
+
+
+	def draw(self):
+		glPushMatrix()
+		if self.scale != 1.0:
+			glScalef(self.scale, self.scale, self.scale)
+		glTranslatef(-self.position[0], -self.position[1], 0)
+		self.collage.draw()
+		for t in self.things:
+			t.draw()
+		glPopMatrix()
+
+
+class Stage:
+	layers = None
+	surfaces = None
+	drawsurfaces = False
+	position = None
+
+	def __init__(self):
+		self.layers = []
+		self.position = (0, 0)
+
+
+	def addLayerBack(self, layer):
+		self.layers.insert(0, layer)
+
+
+	def addLayerFront(self, layer):
+		self.layers.append(layer)
+
+
+	def setSurfaces(self, datafile):
+		self.surfaces = SurfaceSet(datafile)
+
+
+	def drawSurfaces(self):
+		self.drawsurfaces = True
+
+
+	def hideSurfaces(self):
+		self.drawsurfaces = False
+
+
+	def toggleDrawSurfaces(self):
+		self.drawsurfaces = not self.drawsurfaces
+
+
+	def moveTo(self, x, y):
+		self.position = [x,y]
+		for l in self.layers:
+			l.moveTo(x, y)
+
+
+	def draw(self):
+		for l in self.layers:
+			l.draw()
+		if self.drawsurfaces:
+			glPushMatrix()
+			glTranslate(-self.position[0], -self.position[1], 0)
+			self.surfaces.draw()
+			glPopMatrix()

diff --git a/data/example1 b/data/example1
line changes: +19/-0
index cb041dd..ae9cd8f
--- a/data/example1
+++ b/data/example1
@@ -25,6 +25,25 @@ surface 2 1 4 2
 solid 4 1 5 5
 solid 6 1 7 3
 
+tile 10 0
+tile 12 0
+tile 13 0
+tile 14 0
+tile 15 0
+tile 16 0
+tile 17 0
+tile 18 0
+
+tile 12 1
+tile 13 1
+tile 14 1
+tile 14 2
+
+solid 10 0 18 1
+surface 12 1 13 2
+solid 13 1 15 2
+solid 14 2 15 3
+
 tilesize 1 1
 texture img/ByteIco.png
 tile 263 263

diff --git a/platformtest.py b/platformtest.py
line changes: +18/-17
index a2c1d86..2387347
--- a/platformtest.py
+++ b/platformtest.py
@@ -5,6 +5,7 @@ from Sprite import *
 from Collage import *
 from Surface import *
 from Actor import *
+from Stage import *
 import Engine
 
 #resolution = (1280,1024)
@@ -15,9 +16,12 @@ Engine.init(resolution)
 
 pygame.display.set_caption("platformtest")
 
-ground = Collage('data/example1')
-surfaces = SurfaceSet('data/example1')
-stars = Collage('data/stars')
+stage = Stage()
+main = Layer('data/example1')
+stage.addLayerFront(main)
+stage.addLayerBack(Layer('data/stars', 1.0, 0.25))
+stage.setSurfaces('data/example1')
+
 candelabra = Sprite(['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'])
 candelabra.setPosition(64, 64)
 candelabra.setScale(2.0)
@@ -25,6 +29,8 @@ candelabra.setGravity(CENTER, BOTTOM)
 candelabra.setFramerate(10.0)
 candelabra.play()
 
+main.add(candelabra)
+
 class Richter(Actor):
 	def __init__(self, surfaces=None):
 		Actor.__init__(self, surfaces)
@@ -95,12 +101,9 @@ class Richter(Actor):
 		self.grounded = self.collideDelta((0,-1))
 
 
-richter = Richter(surfaces)
+richter = Richter(stage.surfaces)
 richter.setPosition(32, 64)
-
-
-class gamestate:
-	drawsurface = False
+main.add(richter)
 
 
 def input(e):
@@ -112,7 +115,7 @@ def input(e):
 		elif e.key == pygame.K_UP:
 			richter.jump()
 		elif e.key == pygame.K_1:
-			gamestate.drawsurface = not gamestate.drawsurface
+			stage.toggleDrawSurfaces()
 	elif e.type == pygame.KEYUP:
 		if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
 			richter.idle()
@@ -124,14 +127,8 @@ def update():
 	richter.update()
 	if richter.position[1] < 0:
 		richter.position = [richter.position[0],480]
+	stage.moveTo(richter.position[0] - resolution[0]/2, richter.position[1] - resolution[1]/3)
 
-def draw():
-	stars.draw()
-	ground.draw()
-	candelabra.draw()
-	richter.draw()
-	if gamestate.drawsurface:
-		surfaces.draw()
 
 n = 0
 def fpsthing():
@@ -143,4 +140,8 @@ def fpsthing():
 		print fps
 		n = 0
 
-Engine.engine([input],[update,fpsthing],[draw])
+Engine.engine(
+	[input],
+	[update, fpsthing],
+	[stage.draw]
+)