source: nagslang/protagonist.py@ 97:c177cdc41477

Last change on this file since 97:c177cdc41477 was 94:9ef5c5810dcd, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Comment out shape drawing for protagonist. Useful for debugging, but pymunk vomits all over stdout when drawing polygons.

File size: 4.2 KB
Line 
1import pymunk
2import pymunk.pygame_util
3
4from nagslang.constants import COLLISION_TYPE_PLAYER
5from nagslang.game_object import (
6 GameObject, SingleShapePhysicser, FacingImageRenderer)
7from nagslang.mutators import FLIP_H
8from nagslang.resources import resources
9
10
11class Protagonist(GameObject):
12 """Representation of our fearless protagonist.
13
14 TODO: Factor out a bunch of this stuff when we need it for other objects.
15 """
16
17 HUMAN_FORM = 'human'
18 WOLF_FORM = 'wolf'
19
20 def __init__(self, space, position):
21 self._setup_physics(space, position)
22 self._setup_renderers()
23 self.inventory = {}
24 self.form = self.HUMAN_FORM
25
26 super(Protagonist, self).__init__(
27 self._physicsers[self.form], self._renderers[self.form])
28
29 self.go_human()
30
31 def _setup_physics(self, space, position):
32 self._body = pymunk.Body(10, pymunk.inf)
33 self._body.position = position
34 self._body.velocity_func = self.velocity_func
35
36 self._shapes = {
37 self.HUMAN_FORM: pymunk.Poly(
38 self._body, [(-15, -30), (15, -30), (15, 30), (-15, 30)]),
39 self.WOLF_FORM: pymunk.Circle(self._body, 30),
40 }
41 self._physicsers = {}
42 for form, shape in self._shapes.iteritems():
43 shape.elasticity = 1.0
44 shape.friction = 10.0
45 shape.collision_type = COLLISION_TYPE_PLAYER
46 self._physicsers[form] = SingleShapePhysicser(space, shape)
47 self.angle = 0
48
49 def _get_image(self, name, *transforms):
50 return resources.get_image('creatures', name, transforms=transforms)
51
52 def _setup_renderers(self):
53 self._renderers = {
54 self.HUMAN_FORM: FacingImageRenderer(
55 self._get_image('human_1.png'),
56 self._get_image('human_1.png', FLIP_H)),
57 self.WOLF_FORM: FacingImageRenderer(
58 self._get_image('werewolf_1.png'),
59 self._get_image('werewolf_1.png', FLIP_H)),
60 }
61
62 @classmethod
63 def from_saved_state(cls, saved_state):
64 """Create an instance from the provided serialised state.
65 """
66 obj = cls()
67 # TODO: Update from saved state.
68 return obj
69
70 def velocity_func(self, body, gravity, damping, dt):
71 return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
72
73 def get_render_angle(self):
74 return self.angle
75
76 def go_werewolf(self):
77 self._physicsers[self.form].remove_from_space()
78 self.form = self.WOLF_FORM
79 self._physicsers[self.form].add_to_space()
80 self._body.mass = 100
81 self._body.velocity_limit = 1000
82 self.impulse_factor = 4000
83 self.damping = 0.9
84 self.renderer = self._renderers[self.form]
85
86 def go_human(self):
87 self._physicsers[self.form].remove_from_space()
88 self.form = self.HUMAN_FORM
89 self._physicsers[self.form].add_to_space()
90 self._body.mass = 10
91 self._body.velocity_limit = 1000
92 self.impulse_factor = 500
93 self.damping = 0.8
94 self.renderer = self._renderers[self.form]
95
96 def set_direction(self, dx, dy):
97 if (dx, dy) == (0, 0):
98 return
99 self.angle = pymunk.Vec2d((dx, dy)).angle
100 self._body.apply_impulse(
101 (dx * self.impulse_factor, dy * self.impulse_factor))
102
103 def toggle_form(self):
104 if self.form == self.WOLF_FORM:
105 self.go_human()
106 else:
107 self.go_werewolf()
108
109 def act_on(self, target):
110 """Perform an action on the target.
111 """
112 # TODO: Decide how best to do this.
113 pass
114
115 def attack(self):
116 """Attempt to hurt something.
117 """
118 pass
119
120 def in_wolf_form(self):
121 return self.form == self.WOLF_FORM
122
123 def in_human_form(self):
124 return self.form == self.HUMAN_FORM
125
126 def has_item(self, item):
127 return item in self.inventory
128
129 def environmental_movement(self, dx, dy):
130 if (dx, dy) == (0, 0):
131 return
132 self._body.apply_impulse((dx, dy))
133
134 def render(self, surface):
135 # Uncomment to draw pymunk shape as well:
136 # pymunk.pygame_util.draw(surface, self._shapes[self.form])
137 super(Protagonist, self).render(surface)
Note: See TracBrowser for help on using the repository browser.