Changeset 65:a99ac95a2940


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

Move protagonist object to the right place.

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/protagonist.py

    r28 r65  
     1import pygame
     2import pymunk
     3import pymunk.pygame_util
     4
     5from nagslang.game_object import (
     6    GameObject, SingleShapePhysicser, FacingImageRenderer)
     7from nagslang.mutators import FLIP_H, BLUE
     8from nagslang.resources import resources
    19
    210
    3 class Protagonist(object):
     11class Protagonist(GameObject):
    412    """Representation of our fearless protagonist.
    513
     
    1018    WOLF_FORM = 'wolf'
    1119
    12     def __init__(self):
     20    def __init__(self, position):
     21        self._setup_physics(position)
     22        self._setup_renderers()
    1323        self.inventory = {}
    1424        self.form = self.HUMAN_FORM
     25
     26        super(Protagonist, self).__init__(
     27            self._renderers[self.form], SingleShapePhysicser(self.shape))
     28
     29        self.go_human()
     30
     31    def _setup_physics(self, position):
     32        self.body = pymunk.Body(10, 10000)
     33        self.body.position = position
     34        self.body.velocity_func = self.velocity_func
     35
     36        self.shape = pymunk.Circle(self.body, 30)
     37        self.shape.elasticity = 1.0
     38        self.shape.friction = 10.0
     39
     40    def _setup_renderers(self):
     41        self._renderers = {
     42            self.HUMAN_FORM: FacingImageRenderer(
     43                resources.get_image(
     44                    'creatures', 'werewolf_1.png', transforms=(BLUE,)),
     45                resources.get_image(
     46                    'creatures', 'werewolf_1.png', transforms=(FLIP_H, BLUE))),
     47            self.WOLF_FORM: FacingImageRenderer(
     48                resources.get_image('creatures', 'werewolf_1.png'),
     49                resources.get_image(
     50                    'creatures', 'werewolf_1.png', transforms=(FLIP_H,))),
     51        }
    1552
    1653    @classmethod
     
    2259        return obj
    2360
     61    def velocity_func(self, body, gravity, damping, dt):
     62        return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
     63
     64    def go_werewolf(self):
     65        self.form = self.WOLF_FORM
     66        self.body.mass = 100
     67        self.body.moment = 10000
     68        self.body.velocity_limit = 1000
     69        self.shape.color = pygame.color.THECOLORS['red']
     70        self.impulse_factor = 4000
     71        self.damping = 0.9
     72        self.renderer = self._renderers[self.form]
     73
     74    def go_human(self):
     75        self.form = self.HUMAN_FORM
     76        self.body.mass = 10
     77        self.body.moment = 1000
     78        self.body.velocity_limit = 1000
     79        self.shape.color = pygame.color.THECOLORS['blue']
     80        self.impulse_factor = 500
     81        self.damping = 0.8
     82        self.renderer = self._renderers[self.form]
     83
     84    def set_direction(self, dx, dy):
     85        if (dx, dy) == (0, 0):
     86            return
     87        vec = pymunk.Vec2d((dx, dy))
     88        self.body.angle = vec.angle
     89        self.body.apply_impulse(
     90            (dx * self.impulse_factor, dy * self.impulse_factor))
     91
     92    def toggle_form(self):
     93        if self.form == self.WOLF_FORM:
     94            self.go_human()
     95        else:
     96            self.go_werewolf()
     97
    2498    def act_on(self, target):
    2599        """Perform an action on the target.
    26100        """
    27101        # TODO: Decide how best to do this.
    28         pass
    29 
    30     def change_to_form(self, form):
    31         """Change to a particular form.
    32 
    33         This will be a no-op if we're already in this form.
    34         """
    35         pass
    36 
    37     def swap_form(self):
    38         """Swap to your other form.
    39         """
    40102        pass
    41103
  • nagslang/screens/area.py

    r64 r65  
    55import pymunk.pygame_util
    66
    7 from nagslang.game_object import (
    8     GameObject, SingleShapePhysicser, FacingImageRenderer)
     7from nagslang.events import ScreenChange
     8from nagslang.level import Level
     9from nagslang.protagonist import Protagonist
    910from nagslang.screens.base import Screen
    10 from nagslang.level import Level
    11 from nagslang.events import ScreenChange
    12 from nagslang.resources import resources
    13 from nagslang.mutators import FLIP_H
    1411
    1512
     
    2926        elif ev.type == pygame.locals.KEYUP:
    3027            self.key_up(ev.key)
    31 
    32 
    33 class Protagonist(GameObject):
    34     def __init__(self, position):
    35         self.body = pymunk.Body(10, 10000)
    36         self.body.position = position
    37         self.body.velocity_func = self.velocity_func
    38 
    39         self.shape = pymunk.Circle(self.body, 30)
    40         self.shape.elasticity = 1.0
    41         self.shape.friction = 10.0
    42 
    43         super(Protagonist, self).__init__(
    44             FacingImageRenderer(
    45                 resources.get_image('creatures', 'werewolf_1.png'),
    46                 resources.get_image(
    47                     'creatures', 'werewolf_1.png', transforms=(FLIP_H,))),
    48             SingleShapePhysicser(self.shape))
    49 
    50         self.go_human()
    51 
    52     def velocity_func(self, body, gravity, damping, dt):
    53         return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
    54 
    55     def go_werewolf(self):
    56         self.werewolf = True
    57         self.body.mass = 100
    58         self.body.moment = 10000
    59         self.body.velocity_limit = 1000
    60         self.shape.color = pygame.color.THECOLORS['red']
    61         self.impulse_factor = 4000
    62         self.damping = 0.9
    63 
    64     def go_human(self):
    65         self.werewolf = False
    66         self.body.mass = 10
    67         self.body.moment = 1000
    68         self.body.velocity_limit = 1000
    69         self.shape.color = pygame.color.THECOLORS['blue']
    70         self.impulse_factor = 500
    71         self.damping = 0.8
    72 
    73     def set_direction(self, dx, dy):
    74         if (dx, dy) == (0, 0):
    75             return
    76         vec = pymunk.Vec2d((dx, dy))
    77         self.body.angle = vec.angle
    78         self.body.apply_impulse(
    79             (dx * self.impulse_factor, dy * self.impulse_factor))
    80 
    81     def toggle_form(self):
    82         if self.werewolf:
    83             self.go_human()
    84         else:
    85             self.go_werewolf()
    8628
    8729
Note: See TracChangeset for help on using the changeset viewer.