Changeset 63:7f038ee778ad


Ignore:
Timestamp:
Sep 1, 2013, 5:18:12 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Put werewolf facing direction magic back.

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r62 r63  
     1import math
    12
    23
     
    89        raise NotImplementedError()
    910
    10     def render_position(self):
     11    def render_position(self, surface):
     12        raise NotImplementedError()
     13
     14    def render_angle(self):
    1115        raise NotImplementedError()
    1216
     
    2125    def render_position(self):
    2226        return (0, 0)
     27
     28    def render_angle(self):
     29        return 0
    2330
    2431
     
    3744        import pymunk.pygame_util
    3845        return pymunk.pygame_util.to_pygame(pos, surface)
    39         return (int(pos.x), int(pos.y))
     46
     47    def render_angle(self):
     48        return self._shape.body.angle
    4049
    4150
    4251class Renderer(object):
    43     def render(self, surface, pos):
     52    def render(self, surface, pos, angle):
    4453        raise NotImplementedError()
     54
     55
     56def image_pos(image, pos):
     57    return (pos[0] - image.get_width() / 2,
     58            pos[1] - image.get_height() / 2)
    4559
    4660
     
    4963        self._image = image
    5064
    51     def render(self, surface, pos):
    52         surface.blit(self._image, pos)
     65    def render(self, surface, pos, angle):
     66        surface.blit(self._image, image_pos(self._image, pos))
     67
     68
     69class FacingImageRenderer(Renderer):
     70    def __init__(self, left_image, right_image):
     71        self._images = {
     72            'left': left_image,
     73            'right': right_image,
     74        }
     75
     76    def get_image(self, angle):
     77        if abs(angle) < math.pi / 2:
     78            return self._images['right']
     79        return self._images['left']
     80
     81    def render(self, surface, pos, angle):
     82        image = self.get_image(angle)
     83        surface.blit(image, image_pos(image, pos))
    5384
    5485
     
    5788        self._shape = shape
    5889
    59     def render(self, surface, pos):
     90    def render(self, surface, pos, angle):
    6091        import pymunk.pygame_util
    6192        pymunk.pygame_util.draw(surface, self._shape)
     
    79110    def render(self, surface):
    80111        return self.renderer.render(
    81             surface, self.physicser.render_position(surface))
     112            surface, self.physicser.render_position(surface),
     113            self.physicser.render_angle())
  • nagslang/screens/area.py

    r61 r63  
    77
    88from nagslang.game_object import (
    9     GameObject, SingleShapePhysicser, ImageRenderer)
     9    GameObject, SingleShapePhysicser, FacingImageRenderer)
    1010from nagslang.screens.base import Screen
    1111from nagslang.level import Level
     
    4343
    4444        super(Protagonist, self).__init__(
    45             ImageRenderer(resources.get_image('creatures', 'werewolf_1.png')),
     45            FacingImageRenderer(
     46                resources.get_image('creatures', 'werewolf_1.png'),
     47                resources.get_image(
     48                    'creatures', 'werewolf_1.png', transforms=(FLIP_H,))),
    4649            SingleShapePhysicser(self.shape))
    4750
     
    5053    def velocity_func(self, body, gravity, damping, dt):
    5154        return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
    52 
    53     def render_TODO(self, surface):
    54         if self.werewolf:
    55             pos = pymunk.pygame_util.to_pygame(self.body.position, surface)
    56             transforms = ()
    57             if abs(self.body.angle) < math.pi / 2:
    58                 transforms = (FLIP_H,)
    59 
    60             wolf = resources.get_image('creatures', 'werewolf_1.png',
    61                                        transforms=transforms)
    62             surface.blit(wolf, (pos[0] - wolf.get_width() / 2,
    63                                 pos[1] - wolf.get_height() / 2))
    64         else:
    65             pymunk.pygame_util.draw(surface, self.shape)
    6655
    6756    def go_werewolf(self):
Note: See TracChangeset for help on using the changeset viewer.