Ignore:
Timestamp:
09/01/13 17:31:39 (8 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

Move protagonist object to the right place.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.