Factored camera functionality out to Camera.py
+class Camera:
+ stage = None
+ resolution = None
+ focus = None
+ camerapos = None
+ bbox = None
+
+ def __init__(self, stage, resolution):
+ self.stage = stage
+ self.resolution = resolution
+ self.camerapos = [0,0]
+
+
+ def setFocus(self, obj):
+ self.focus = obj
+
+
+ def clearFocus(self):
+ self.focus = None
+
+
+ def setPosition(self, x, y):
+ self.camerapos = [x, y]
+
+
+ def setBbox(self, bbox):
+ self.bbox = bbox
+
+
+ def clearBbox(self):
+ self.bbox = None
+
+
+ def update(self):
+ if self.focus:
+ self.camerapos = [self.focus.position[0] - self.resolution[0]/2,
+ self.focus.position[1] - self.resolution[1]/4]
+
+ if self.bbox:
+ # This is a wee bit confusing since Rects assume (0,0) in the
+ # upper left, but we want it in the lower left. Tops and
+ # bottoms are reversed.
+ if self.bbox.height < self.resolution[1] or self.camerapos[1] < self.bbox.top:
+ self.camerapos[1] = self.bbox.top
+ elif self.camerapos[1] + self.resolution[1] > self.bbox.bottom:
+ self.camerapos[1] = self.bbox.bottom - self.resolution[1]
+
+ if self.bbox.width < self.resolution[0] or self.camerapos[0] < self.bbox.left:
+ self.camerapos[0] = self.bbox.left
+ elif self.camerapos[0] + self.resolution[0] > self.bbox.right:
+ self.camerapos[0] = self.bbox.right - self.resolution[0]
+
+ self.stage.moveTo(self.camerapos[0], self.camerapos[1])
from Surface import *
from Actor import *
from Stage import *
+from Camera import *
import Engine
#resolution = (1280,1024)
main.add(candelabra)
+camera = Camera(stage, resolution)
+camera.setBbox(main.bbox)
+
class Richter(Actor):
def __init__(self, surfaces=None):
Actor.__init__(self, surfaces)
richter = Richter(stage.surfaces)
richter.setPosition(32, 64)
main.add(richter)
+camera.setFocus(richter)
def input(e):
richter.update()
if richter.position[1] < 0:
richter.position = [richter.position[0],480]
- stagepos = [richter.position[0] - resolution[0]/2, richter.position[1] - resolution[1]/4]
-
- ### Stage bounding box stuff should be moved upstream
- # This is a wee bit confusing since Rects assume (0,0) in the upper
- # left, but we want it in the lower left. Flip tops and bottoms.
- if main.bbox.height < resolution[1] or stagepos[1] < main.bbox.top:
- stagepos[1] = main.bbox.top
- elif stagepos[1] + resolution[1] > main.bbox.bottom:
- stagepos[1] = main.bbox.bottom - resolution[1]
-
- if main.bbox.width < resolution[0] or stagepos[0] < main.bbox.left:
- stagepos[0] = main.bbox.left
- elif stagepos[0] + resolution[0] > main.bbox.right:
- stagepos[0] = main.bbox.right- resolution[0]
-
- stage.moveTo(stagepos[0], stagepos[1])
-
n = 0
def fpsthing():
Engine.engine(
[input],
- [update, fpsthing],
+ [update, camera.update, fpsthing],
[stage.draw]
)