changeset 609:9ea26b835271

KeyedHatch
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 08 Sep 2013 00:32:32 +0200
parents 9b9d529ba5d9
children 29d67c51fd33 2070ce83637b
files nagslang/game_object.py nagslang/render.py
diffstat 2 files changed, 81 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/nagslang/game_object.py	Sun Sep 08 00:25:29 2013 +0200
+++ b/nagslang/game_object.py	Sun Sep 08 00:32:32 2013 +0200
@@ -6,7 +6,7 @@
 from nagslang import environment
 from nagslang import puzzle
 from nagslang import render
-from nagslang.mutators import FLIP_H, ImageOverlay, rotator
+from nagslang.mutators import FLIP_H, ImageOverlay, rotator, scaler
 from nagslang.constants import (
     COLLISION_TYPE_DOOR, COLLISION_TYPE_FURNITURE, COLLISION_TYPE_PROJECTILE,
     COLLISION_TYPE_SWITCH, COLLISION_TYPE_SHEEP, COLLISION_TYPE_SHEEP_PEN,
@@ -508,8 +508,15 @@
     zorder = ZORDER_FLOOR
 
     def __init__(self, space, end1, end2, key_state=None):
-        body = make_body(None, None, (0, 0))
-        self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 7)
+        a = pymunk.Vec2d(end1)
+        b = pymunk.Vec2d(end2)
+        offset = b - a
+        offset.length /= 2
+        mid = (a + offset).int_tuple
+        body = make_body(None, None, mid)
+        self.shape = pymunk.Segment(
+            body, body.world_to_local(tuple(end1)),
+            body.world_to_local(tuple(end2)), 7)
         self.shape.collision_type = COLLISION_TYPE_DOOR
         if key_state is None:
             puzzler = puzzle.YesPuzzler()
@@ -534,7 +541,53 @@
         return [("name", "string"), ("end1", "coordinates"),
                 ("end2", "coordinates"), ("key_state", "puzzler")]
 
-    # The level knows that bulkheads are magical
+    # The level knows that hatches are magical
+    @classmethod
+    def movable(cls):
+        return True
+
+
+class KeyedHatch(GameObject):
+    zorder = ZORDER_FLOOR
+
+    def __init__(self, space, end1, end2, key_item):
+        a = pymunk.Vec2d(end1)
+        b = pymunk.Vec2d(end2)
+        offset = b - a
+        offset.length /= 2
+        mid = (a + offset).int_tuple
+        body = make_body(None, None, mid)
+        self.shape = pymunk.Segment(
+            body, body.world_to_local(tuple(end1)),
+            body.world_to_local(tuple(end2)), 7)
+        self.shape.collision_type = COLLISION_TYPE_DOOR
+        self._key_item = key_item
+        super(KeyedHatch, self).__init__(
+            SingleShapePhysicser(space, self.shape),
+            render.KeyedHatchRenderer(
+                resources.get_image(
+                    'objects', '%s.png' % (key_item,),
+                    transforms=(scaler((32, 32)),))),
+            puzzle.ParentAttrPuzzler('is_open'),
+        )
+        self.add_timer('door_open', 0.1)
+
+    @property
+    def is_open(self):
+        return self.check_timer('door_open')
+
+    def collide_with_protagonist(self, protagonist):
+        if protagonist.has_item(self._key_item):
+            self.start_timer('door_open')
+            return False
+        return True
+
+    @classmethod
+    def requires(cls):
+        return [("name", "string"), ("end1", "coordinates"),
+                ("end2", "coordinates"), ("key_item", "item name")]
+
+    # The level knows that hatches are magical
     @classmethod
     def movable(cls):
         return True
--- a/nagslang/render.py	Sun Sep 08 00:25:29 2013 +0200
+++ b/nagslang/render.py	Sun Sep 08 00:32:32 2013 +0200
@@ -47,7 +47,7 @@
         pass
 
 
-class HatchRenderer(Renderer):
+class HatchRendererMixin(object):
     def draw_hatch_line(self, surface, a, b):
         ai, bi = extend_line(a, b, -2)
         a, b, ai, bi = points_to_pygame(surface, (a, b, ai, bi))
@@ -56,18 +56,25 @@
         pygame.draw.line(
             surface, pygame.color.THECOLORS['lightblue'], ai, bi, 5)
 
-    def render(self, surface):
+    def render_hatch(self, surface):
         shape = self.game_object.get_shape()
+        a = shape.body.local_to_world(shape.a)
+        b = shape.body.local_to_world(shape.b)
         if self.game_object.puzzler.get_state():
-            offset = vec_from_angle((shape.b - shape.a).angle, 10)
-            ai = shape.a + offset
-            bi = shape.b - offset
-            self.draw_hatch_line(surface, shape.a, ai)
-            self.draw_hatch_line(surface, bi, shape.b)
+            offset = vec_from_angle((b - a).angle, 10)
+            ai = a + offset
+            bi = b - offset
+            self.draw_hatch_line(surface, a, ai)
+            self.draw_hatch_line(surface, bi, b)
         else:
-            mid = shape.a + (shape.b - shape.a) / 2
-            self.draw_hatch_line(surface, shape.a, mid)
-            self.draw_hatch_line(surface, mid, shape.b)
+            mid = a + (b - a) / 2
+            self.draw_hatch_line(surface, a, mid)
+            self.draw_hatch_line(surface, mid, b)
+
+
+class HatchRenderer(Renderer, HatchRendererMixin):
+    def render(self, surface):
+        self.render_hatch(surface)
 
 
 def image_pos(image, pos):
@@ -96,6 +103,13 @@
         super(ImageRenderer, self).render(surface)
 
 
+class KeyedHatchRenderer(ImageRenderer, HatchRendererMixin):
+    def render(self, surface):
+        self.render_hatch(surface)
+        if not self.game_object.puzzler.get_state():
+            self.render_image(surface, self.get_image())
+
+
 class ImageStateRenderer(ImageRenderer):
     def __init__(self, state_images):
         self._state_images = state_images