Changeset 47:82036437ebf6


Ignore:
Timestamp:
Sep 1, 2013, 4:22:46 PM (7 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
rebase_source:
ee7a153a285427f8e5cad20d1cb072edd73aa987
Message:

Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/screens/area.py

    r37 r47  
    2626
    2727
     28class Protagonist(object):
     29    def __init__(self, position):
     30        self.body = pymunk.Body(10, 10000)
     31        self.body.position = position
     32        self.body.velocity_func = self.velocity_func
     33
     34        self.shape = pymunk.Circle(self.body, 30)
     35        self.shape.elasticity = 1.0
     36        self.shape.friction = 10.0
     37
     38        self.go_human()
     39
     40    def add_space(self, space):
     41        space.add(self.body, self.shape)
     42
     43    def velocity_func(self, body, gravity, damping, dt):
     44        return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
     45
     46    def render(self, surface):
     47        pymunk.pygame_util.draw(surface, self.shape)
     48
     49    def go_werewolf(self):
     50        self.werewolf = True
     51        self.body.mass = 100
     52        self.body.moment = 10000
     53        self.body.velocity_limit = 1000
     54        self.shape.color = pygame.color.THECOLORS['red']
     55        self.impulse_factor = 4000
     56        self.damping = 0.9
     57
     58    def go_human(self):
     59        self.werewolf = False
     60        self.body.mass = 10
     61        self.body.moment = 1000
     62        self.body.velocity_limit = 1000
     63        self.shape.color = pygame.color.THECOLORS['blue']
     64        self.impulse_factor = 200
     65        self.damping = 0.8
     66
     67    def set_direction(self, dx, dy):
     68        if (dx, dy) == (0, 0):
     69            return
     70        vec = pymunk.Vec2d((dx, dy))
     71        self.body.angle = vec.angle
     72        self.body.apply_impulse(
     73            (dx * self.impulse_factor, dy * self.impulse_factor))
     74
     75    def toggle_form(self):
     76        if self.werewolf:
     77            self.go_human()
     78        else:
     79            self.go_werewolf()
     80
     81
    2882class AreaScreen(Screen):
    2983
     
    4094        corner = corners[-1]
    4195        for next_corner in corners:
    42             self.walls.append(pymunk.Segment(body, corner, next_corner, 5))
     96            wall = pymunk.Segment(body, corner, next_corner, 50)
     97            wall.elasticity = 1.0
     98            self.walls.append(wall)
    4399            corner = next_corner
    44100        self.space.add(*self.walls)
    45101
    46102    def add_protagonist(self):
    47         self.protagonist_body = pymunk.Body(100, 1000)
    48         self.protagonist_body.position = (400, 300)
    49         self.protagonist_body.velocity_limit = 5000
    50         self.protagonist = pymunk.Circle(self.protagonist_body, 30)
    51         self.space.add(self.protagonist_body, self.protagonist)
    52 
    53     def protagonist_move(self, dx, dy):
    54         vec = pymunk.Vec2d((dx, dy))
    55         self.protagonist_body.angle = vec.angle
    56         self.protagonist_body.apply_impulse((dx * 1000, dy * 1000))
     103        self.protagonist = Protagonist((400, 300))
     104        self.protagonist.add_space(self.space)
    57105
    58106    def handle_event(self, ev):
     
    60108            if ev.key == pygame.locals.K_ESCAPE:
    61109                ScreenChange.post('menu')
     110            if ev.key == pygame.locals.K_w:
     111                self.protagonist.toggle_form()
    62112        self.keys.handle_event(ev)
    63113
     
    65115        surface.fill(pygame.color.Color(0, 0, 0))
    66116        pymunk.pygame_util.draw(surface, *self.walls)
    67         pymunk.pygame_util.draw(surface, self.protagonist)
     117        self.protagonist.render(surface)
    68118
    69119    def tick_protagonist(self):
     
    76126                dx += tx
    77127                dy += ty
    78         self.protagonist_move(dx, dy)
     128        self.protagonist.set_direction(dx, dy)
    79129
    80130    def tick(self, seconds):
Note: See TracChangeset for help on using the changeset viewer.