diff nagslang/game_object.py @ 104:1be3eebb87c4

More consistent debug rendering.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 02 Sep 2013 13:21:53 +0200
parents d6a49f0c1e6e
children bce9cd8a4a8c
line wrap: on
line diff
--- a/nagslang/game_object.py	Mon Sep 02 13:05:25 2013 +0200
+++ b/nagslang/game_object.py	Mon Sep 02 13:21:53 2013 +0200
@@ -5,6 +5,7 @@
 import pymunk.pygame_util
 
 from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
+from nagslang.options import options
 
 
 class Puzzler(object):
@@ -74,8 +75,31 @@
 
 
 class Renderer(object):
+    def __init__(self, shape):
+        self._shape = shape
+
+    def _render_shape(self, surface, pos, angle):
+        # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
+        color = getattr(
+            self._shape, 'color', pygame.color.THECOLORS['lightblue'])
+        # We only explicitly draw Circle and Poly shapes. Everything else we
+        # forward to pymunk.
+        if isinstance(self._shape, pymunk.Circle):
+            centre = pymunk.pygame_util.to_pygame(
+                self._shape.body.position, surface)
+            radius = int(self._shape.radius)
+            pygame.draw.circle(surface, color, centre, radius, 2)
+        elif isinstance(self._shape, pymunk.Poly):
+            # polygon bounding box
+            points = [pymunk.pygame_util.to_pygame(p, surface)
+                      for p in self._shape.get_vertices()]
+            pygame.draw.lines(surface, color, True, points, 2)
+        else:
+            pymunk.pygame_util.draw(surface, self._shape)
+
     def render(self, surface, pos, angle):
-        raise NotImplementedError()
+        if options.debug:
+            self._render_shape(surface, pos, angle)
 
 
 def image_pos(image, pos):
@@ -84,15 +108,18 @@
 
 
 class ImageRenderer(Renderer):
-    def __init__(self, image):
+    def __init__(self, shape, image):
+        super(ImageRenderer, self).__init__(shape)
         self._image = image
 
     def render(self, surface, pos, angle):
         surface.blit(self._image, image_pos(self._image, pos))
+        super(ImageRenderer, self).render(surface, pos, angle)
 
 
 class FacingImageRenderer(Renderer):
-    def __init__(self, left_image, right_image):
+    def __init__(self, shape, left_image, right_image):
+        super(FacingImageRenderer, self).__init__(shape)
         self._images = {
             'left': left_image,
             'right': right_image,
@@ -106,15 +133,13 @@
     def render(self, surface, pos, angle):
         image = self.get_image(angle)
         surface.blit(image, image_pos(image, pos))
+        super(FacingImageRenderer, self).render(surface, pos, angle)
 
 
 class ShapeRenderer(Renderer):
-    def __init__(self, shape):
-        self._shape = shape
-
     def render(self, surface, pos, angle):
-        import pymunk.pygame_util
-        pymunk.pygame_util.draw(surface, self._shape)
+        self._render_shape(surface, pos, angle)
+        super(ShapeRenderer, self).render(surface, pos, angle)
 
 
 class GameObject(object):