changeset 104:1be3eebb87c4

More consistent debug rendering.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 02 Sep 2013 13:21:53 +0200
parents adf3cd83bf7a
children 0131e4606e1a
files nagslang/game_object.py nagslang/protagonist.py
diffstat 2 files changed, 35 insertions(+), 29 deletions(-) [+]
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):
--- a/nagslang/protagonist.py	Mon Sep 02 13:05:25 2013 +0200
+++ b/nagslang/protagonist.py	Mon Sep 02 13:21:53 2013 +0200
@@ -1,13 +1,10 @@
 import pymunk
 import pymunk.pygame_util
 
-import pygame
-
 from nagslang.constants import COLLISION_TYPE_PLAYER
 from nagslang.game_object import (
     GameObject, SingleShapePhysicser, FacingImageRenderer)
 from nagslang.mutators import FLIP_H
-from nagslang.options import options
 from nagslang.resources import resources
 
 
@@ -55,9 +52,11 @@
     def _setup_renderers(self):
         self._renderers = {
             self.HUMAN_FORM: FacingImageRenderer(
+                self._shapes[self.HUMAN_FORM],
                 self._get_image('human_1.png'),
                 self._get_image('human_1.png', FLIP_H)),
             self.WOLF_FORM: FacingImageRenderer(
+                self._shapes[self.WOLF_FORM],
                 self._get_image('werewolf_1.png'),
                 self._get_image('werewolf_1.png', FLIP_H)),
         }
@@ -133,21 +132,3 @@
         if (dx, dy) == (0, 0):
             return
         self._body.apply_impulse((dx, dy))
-
-    def render(self, surface):
-        if options.debug:
-            # Less general that pymunk.pygame_utils.draw, but also a
-            # lot less noisy
-            color = pygame.color.THECOLORS['lightblue']
-            if hasattr(self._shapes[self.form], 'get_vertices'):
-                # polygon bounding box
-                points = [pymunk.pygame_util.to_pygame(p, surface)
-                          for p in self._shapes[self.form].get_vertices()]
-                pygame.draw.lines(surface, color, True, points, 2)
-            else:
-                # draw the circle
-                centre = pymunk.pygame_util.to_pygame(
-                    self._shapes[self.form].body.position, surface)
-                radius = int(self._shapes[self.form].radius)
-                pygame.draw.circle(surface, color, centre, radius, 2)
-        super(Protagonist, self).render(surface)