Changeset 609:9ea26b835271


Ignore:
Timestamp:
09/07/13 22:32:32 (8 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Children:
610:29d67c51fd33, 617:2070ce83637b
Phase:
public
Rebase:
66303738323865633236373163383830656434646134316239343036646431303330666563373864
Message:

KeyedHatch

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r591 r609  
    77from nagslang import puzzle
    88from nagslang import render
    9 from nagslang.mutators import FLIP_H, ImageOverlay, rotator
     9from nagslang.mutators import FLIP_H, ImageOverlay, rotator, scaler
    1010from nagslang.constants import (
    1111    COLLISION_TYPE_DOOR, COLLISION_TYPE_FURNITURE, COLLISION_TYPE_PROJECTILE,
     
    509509
    510510    def __init__(self, space, end1, end2, key_state=None):
    511         body = make_body(None, None, (0, 0))
    512         self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 7)
     511        a = pymunk.Vec2d(end1)
     512        b = pymunk.Vec2d(end2)
     513        offset = b - a
     514        offset.length /= 2
     515        mid = (a + offset).int_tuple
     516        body = make_body(None, None, mid)
     517        self.shape = pymunk.Segment(
     518            body, body.world_to_local(tuple(end1)),
     519            body.world_to_local(tuple(end2)), 7)
    513520        self.shape.collision_type = COLLISION_TYPE_DOOR
    514521        if key_state is None:
     
    535542                ("end2", "coordinates"), ("key_state", "puzzler")]
    536543
    537     # The level knows that bulkheads are magical
     544    # The level knows that hatches are magical
     545    @classmethod
     546    def movable(cls):
     547        return True
     548
     549
     550class KeyedHatch(GameObject):
     551    zorder = ZORDER_FLOOR
     552
     553    def __init__(self, space, end1, end2, key_item):
     554        a = pymunk.Vec2d(end1)
     555        b = pymunk.Vec2d(end2)
     556        offset = b - a
     557        offset.length /= 2
     558        mid = (a + offset).int_tuple
     559        body = make_body(None, None, mid)
     560        self.shape = pymunk.Segment(
     561            body, body.world_to_local(tuple(end1)),
     562            body.world_to_local(tuple(end2)), 7)
     563        self.shape.collision_type = COLLISION_TYPE_DOOR
     564        self._key_item = key_item
     565        super(KeyedHatch, self).__init__(
     566            SingleShapePhysicser(space, self.shape),
     567            render.KeyedHatchRenderer(
     568                resources.get_image(
     569                    'objects', '%s.png' % (key_item,),
     570                    transforms=(scaler((32, 32)),))),
     571            puzzle.ParentAttrPuzzler('is_open'),
     572        )
     573        self.add_timer('door_open', 0.1)
     574
     575    @property
     576    def is_open(self):
     577        return self.check_timer('door_open')
     578
     579    def collide_with_protagonist(self, protagonist):
     580        if protagonist.has_item(self._key_item):
     581            self.start_timer('door_open')
     582            return False
     583        return True
     584
     585    @classmethod
     586    def requires(cls):
     587        return [("name", "string"), ("end1", "coordinates"),
     588                ("end2", "coordinates"), ("key_item", "item name")]
     589
     590    # The level knows that hatches are magical
    538591    @classmethod
    539592    def movable(cls):
  • nagslang/render.py

    r558 r609  
    4848
    4949
    50 class HatchRenderer(Renderer):
     50class HatchRendererMixin(object):
    5151    def draw_hatch_line(self, surface, a, b):
    5252        ai, bi = extend_line(a, b, -2)
     
    5757            surface, pygame.color.THECOLORS['lightblue'], ai, bi, 5)
    5858
    59     def render(self, surface):
     59    def render_hatch(self, surface):
    6060        shape = self.game_object.get_shape()
     61        a = shape.body.local_to_world(shape.a)
     62        b = shape.body.local_to_world(shape.b)
    6163        if self.game_object.puzzler.get_state():
    62             offset = vec_from_angle((shape.b - shape.a).angle, 10)
    63             ai = shape.a + offset
    64             bi = shape.b - offset
    65             self.draw_hatch_line(surface, shape.a, ai)
    66             self.draw_hatch_line(surface, bi, shape.b)
    67         else:
    68             mid = shape.a + (shape.b - shape.a) / 2
    69             self.draw_hatch_line(surface, shape.a, mid)
    70             self.draw_hatch_line(surface, mid, shape.b)
     64            offset = vec_from_angle((b - a).angle, 10)
     65            ai = a + offset
     66            bi = b - offset
     67            self.draw_hatch_line(surface, a, ai)
     68            self.draw_hatch_line(surface, bi, b)
     69        else:
     70            mid = a + (b - a) / 2
     71            self.draw_hatch_line(surface, a, mid)
     72            self.draw_hatch_line(surface, mid, b)
     73
     74
     75class HatchRenderer(Renderer, HatchRendererMixin):
     76    def render(self, surface):
     77        self.render_hatch(surface)
    7178
    7279
     
    95102        self.render_image(surface, self.get_image())
    96103        super(ImageRenderer, self).render(surface)
     104
     105
     106class KeyedHatchRenderer(ImageRenderer, HatchRendererMixin):
     107    def render(self, surface):
     108        self.render_hatch(surface)
     109        if not self.game_object.puzzler.get_state():
     110            self.render_image(surface, self.get_image())
    97111
    98112
Note: See TracChangeset for help on using the changeset viewer.