Changeset 123:23b533d6f27e


Ignore:
Timestamp:
Sep 2, 2013, 2:43:15 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Rearrange game objects a bit.

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r107 r123  
    3030        self.glue = glue
    3131
     32    def set_game_object(self, game_object):
     33        self.game_object = game_object
     34
    3235    def get_state(self):
    3336        raise NotImplementedError()
     
    3538
    3639class FloorSwitchPuzzler(Puzzler):
    37     def __init__(self, space, shape):
    38         self._space = space
    39         self._shape = shape
    40 
    4140    def get_state(self):
    42         for shape in self._space.shape_query(self._shape):
     41        space = self.game_object.get_space()
     42        for shape in space.shape_query(self.game_object.get_shape()):
    4343            if shape.collision_type in SWITCH_PUSHERS:
    4444                return True
     
    5656class Physicser(object):
    5757    def __init__(self, space):
    58         self.space = space
     58        self._space = space
     59
     60    def get_space(self):
     61        return self._space
     62
     63    def set_game_object(self, game_object):
     64        self.game_object = game_object
     65
     66    def get_shape(self):
     67        raise NotImplementedError()
    5968
    6069    def add_to_space(self):
     
    7988        self._shape = shape
    8089
     90    def get_shape(self):
     91        return self._shape
     92
    8193    def add_to_space(self):
    82         self.space.add(self._shape)
     94        self.get_space().add(self._shape)
    8395        if not self._shape.body.is_static:
    84             self.space.add(self._shape.body)
     96            self.get_space().add(self._shape.body)
    8597
    8698    def remove_from_space(self):
    87         self.space.remove(self._shape)
     99        self.get_space().remove(self._shape)
    88100        if not self._shape.body.is_static:
    89             self.space.remove(self._shape.body)
     101            self.get_space().remove(self._shape.body)
    90102
    91103    def get_render_position(self, surface):
     
    101113
    102114class Renderer(object):
    103     def __init__(self, shape):
    104         self._shape = shape
    105 
    106     def _render_shape(self, surface, pos, angle):
     115    def set_game_object(self, game_object):
     116        self.game_object = game_object
     117
     118    def _render_shape(self, surface):
     119        shape = self.game_object.get_shape()
    107120        # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
    108         color = getattr(
    109             self._shape, 'color', pygame.color.THECOLORS['lightblue'])
     121        color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
    110122        # We only explicitly draw Circle and Poly shapes. Everything else we
    111123        # forward to pymunk.
    112         if isinstance(self._shape, pymunk.Circle):
    113             centre = pymunk.pygame_util.to_pygame(
    114                 self._shape.body.position, surface)
    115             radius = int(self._shape.radius)
     124        if isinstance(shape, pymunk.Circle):
     125            centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
     126            radius = int(shape.radius)
    116127            pygame.draw.circle(surface, color, centre, radius, 2)
    117         elif isinstance(self._shape, pymunk.Poly):
     128        elif isinstance(shape, pymunk.Poly):
    118129            # polygon bounding box
    119130            points = [pymunk.pygame_util.to_pygame(p, surface)
    120                       for p in self._shape.get_vertices()]
     131                      for p in shape.get_vertices()]
    121132            pygame.draw.lines(surface, color, True, points, 2)
    122133        else:
    123             pymunk.pygame_util.draw(surface, self._shape)
    124 
    125     def render(self, surface, pos, angle):
     134            pymunk.pygame_util.draw(surface, shape)
     135
     136    def render(self, surface):
     137        pos = self.game_object.get_render_position(surface)
     138        angle = self.game_object.get_render_angle()
    126139        if options.debug:
    127140            self._render_shape(surface, pos, angle)
     
    134147
    135148class ImageRenderer(Renderer):
    136     def __init__(self, shape, image):
    137         super(ImageRenderer, self).__init__(shape)
     149    def __init__(self, image):
    138150        self._image = image
    139151
    140     def render(self, surface, pos, angle):
     152    def render(self, surface):
     153        pos = self.game_object.get_render_position(surface)
    141154        surface.blit(self._image, image_pos(self._image, pos))
    142         super(ImageRenderer, self).render(surface, pos, angle)
     155        super(ImageRenderer, self).render(surface)
    143156
    144157
    145158class FacingImageRenderer(Renderer):
    146     def __init__(self, shape, left_image, right_image):
    147         super(FacingImageRenderer, self).__init__(shape)
     159    def __init__(self, left_image, right_image):
    148160        self._images = {
    149161            'left': left_image,
     
    156168        return self._images['left']
    157169
    158     def render(self, surface, pos, angle):
    159         image = self.get_image(angle)
     170    def render(self, surface):
     171        pos = self.game_object.get_render_position(surface)
     172        image = self.get_image(self.game_object.get_render_angle())
    160173        surface.blit(image, image_pos(image, pos))
    161         super(FacingImageRenderer, self).render(surface, pos, angle)
     174        super(FacingImageRenderer, self).render(surface)
    162175
    163176
    164177class ShapeRenderer(Renderer):
    165     def render(self, surface, pos, angle):
    166         self._render_shape(surface, pos, angle)
    167         super(ShapeRenderer, self).render(surface, pos, angle)
     178    def render(self, surface):
     179        self._render_shape(surface)
     180        super(ShapeRenderer, self).render(surface)
    168181
    169182
     
    176189    def __init__(self, physicser, renderer, puzzler=None):
    177190        self.physicser = physicser
     191        physicser.set_game_object(self)
    178192        self.physicser.add_to_space()
    179193        self.renderer = renderer
     194        renderer.set_game_object(self)
    180195        self.puzzler = puzzler
     196        if puzzler is not None:
     197            puzzler.set_game_object(self)
    181198        self.zorder = ZORDER_LOW
     199
     200    def get_space(self):
     201        return self.physicser.get_space()
     202
     203    def get_shape(self):
     204        return self.physicser.get_shape()
    182205
    183206    def get_render_position(self, surface):
     
    188211
    189212    def render(self, surface):
    190         return self.renderer.render(
    191             surface, self.get_render_position(surface),
    192             self.get_render_angle())
     213        return self.renderer.render(surface)
    193214
    194215
     
    202223        super(FloorSwitch, self).__init__(
    203224            SingleShapePhysicser(space, self.shape),
    204             ShapeRenderer(self.shape),
    205             FloorSwitchPuzzler(space, self.shape),
     225            ShapeRenderer(),
     226            FloorSwitchPuzzler(),
    206227        )
    207228
     
    223244        super(FloorLight, self).__init__(
    224245            SingleShapePhysicser(space, self.shape),
    225             ShapeRenderer(self.shape),
     246            ShapeRenderer(),
    226247            StateProxyPuzzler(state_source),
    227248        )
  • nagslang/protagonist.py

    r107 r123  
    5454        self._renderers = {
    5555            self.HUMAN_FORM: FacingImageRenderer(
    56                 self._shapes[self.HUMAN_FORM],
    5756                self._get_image('human_1.png'),
    5857                self._get_image('human_1.png', FLIP_H)),
    5958            self.WOLF_FORM: FacingImageRenderer(
    60                 self._shapes[self.WOLF_FORM],
    6159                self._get_image('werewolf_1.png'),
    6260                self._get_image('werewolf_1.png', FLIP_H)),
    6361        }
     62        for renderer in self._renderers.values():
     63            renderer.set_game_object(self)
    6464
    6565    @classmethod
Note: See TracChangeset for help on using the changeset viewer.