source: nagslang/protagonist.py@ 249:cb391935791f

Last change on this file since 249:cb391935791f was 249:cb391935791f, checked in by David Sharpe, 8 years ago

Stuff.

File size: 9.0 KB
Line 
1import pymunk
2import pymunk.pygame_util
3from pymunk.vec2d import Vec2d
4
5from nagslang import render
6from nagslang.constants import COLLISION_TYPE_PLAYER, ZORDER_MID, \
7 WEREWOLF_SOAK_FACTOR, PROTAGONIST_HEALTH_MIN_LEVEL, \
8 PROTAGONIST_HEALTH_MAX_LEVEL
9from nagslang.game_object import GameObject, Physicser, make_body
10from nagslang.mutators import FLIP_H
11from nagslang.resources import resources
12
13
14class ProtagonistPhysicser(Physicser):
15 def __init__(self, space, form_shapes):
16 self._space = space
17 self._form_shapes = form_shapes
18
19 def switch_form(self, old_form, new_form):
20 self._space.remove(self._form_shapes[old_form])
21 shape = self._form_shapes[new_form]
22 self._space.add(shape)
23 for attr, value in shape.protagonist_body_props.iteritems():
24 setattr(shape.body, attr, value)
25
26 def get_shape(self):
27 return self._form_shapes[self.game_object.form]
28
29
30class ProtagonistFormSelectionRenderer(render.RendererSelectionRenderer):
31 def select_renderer(self):
32 return self.game_object.form
33
34
35class Protagonist(GameObject):
36 """Representation of our fearless protagonist.
37
38 TODO: Factor out a bunch of this stuff when we need it for other objects.
39 """
40
41 HUMAN_FORM = 'human'
42 WOLF_FORM = 'wolf'
43
44 def __init__(self, space, position):
45 physicser = self._make_physics(space, position)
46 renderer = self._make_renderer()
47 self.inventory = {}
48 self.form = self.HUMAN_FORM
49 self.angle = 0
50 self.is_moving = False
51
52 super(Protagonist, self).__init__(physicser, renderer)
53 self.zorder = ZORDER_MID
54
55 self.health_level = 100
56
57 self.go_human()
58
59 def _make_physics(self, space, position):
60 body = make_body(10, pymunk.inf, position, 0.8)
61 body.velocity_limit = 1000
62
63 human = pymunk.Poly(body, [(-15, -30), (15, -30), (15, 30), (-15, 30)])
64 human.elasticity = 1.0
65 human.collision_type = COLLISION_TYPE_PLAYER
66 human.protagonist_body_props = {
67 'mass': 10,
68 'damping': 0.8,
69 }
70
71 wolf = pymunk.Circle(body, 30)
72 wolf.elasticity = 1.0
73 wolf.collision_type = COLLISION_TYPE_PLAYER
74 wolf.protagonist_body_props = {
75 'mass': 100,
76 'damping': 0.9,
77 }
78
79 return ProtagonistPhysicser(space, {
80 self.HUMAN_FORM: human,
81 self.WOLF_FORM: wolf,
82 })
83
84 def _get_image(self, name, *transforms):
85 return resources.get_image('creatures', name, transforms=transforms)
86
87 def _make_renderer(self):
88 return ProtagonistFormSelectionRenderer({
89 self.HUMAN_FORM: render.FacingSelectionRenderer(
90 {
91 'N': render.MovementAnimatedRenderer(
92 [self._get_image('human_N_1.png'),
93 self._get_image('human_N_2.png')], 3),
94 'S': render.MovementAnimatedRenderer(
95 [self._get_image('human_S_1.png'),
96 self._get_image('human_S_2.png')], 3),
97 'W': render.MovementAnimatedRenderer(
98 [self._get_image('human_W_1.png'),
99 self._get_image('human_W_2.png')], 3),
100 'E': render.MovementAnimatedRenderer(
101 [self._get_image('human_W_1.png', FLIP_H),
102 self._get_image('human_W_2.png', FLIP_H)], 3),
103 'NW': render.MovementAnimatedRenderer(
104 [self._get_image('human_NW_1.png'),
105 self._get_image('human_NW_2.png')], 3),
106 'NE': render.MovementAnimatedRenderer(
107 [self._get_image('human_NW_1.png', FLIP_H),
108 self._get_image('human_NW_2.png', FLIP_H)], 3),
109 'SW': render.MovementAnimatedRenderer(
110 [self._get_image('human_SW_1.png'),
111 self._get_image('human_SW_2.png')], 3),
112 'SE': render.MovementAnimatedRenderer(
113 [self._get_image('human_SW_1.png', FLIP_H),
114 self._get_image('human_SW_2.png', FLIP_H)], 3),
115 }),
116 self.WOLF_FORM: render.FacingSelectionRenderer(
117 {
118 'N': render.MovementAnimatedRenderer(
119 [self._get_image('werewolf_N_1.png'),
120 self._get_image('werewolf_N_2.png')], 3),
121 'S': render.MovementAnimatedRenderer(
122 [self._get_image('werewolf_S_1.png'),
123 self._get_image('werewolf_S_2.png')], 3),
124 'W': render.MovementAnimatedRenderer(
125 [self._get_image('werewolf_W_1.png'),
126 self._get_image('werewolf_W_2.png')], 3),
127 'E': render.MovementAnimatedRenderer(
128 [self._get_image('werewolf_W_1.png', FLIP_H),
129 self._get_image('werewolf_W_2.png', FLIP_H)], 3),
130 'NW': render.MovementAnimatedRenderer(
131 [self._get_image('werewolf_NW_1.png'),
132 self._get_image('werewolf_NW_2.png')], 3),
133 'NE': render.MovementAnimatedRenderer(
134 [self._get_image('werewolf_NW_1.png', FLIP_H),
135 self._get_image('werewolf_NW_2.png', FLIP_H)], 3),
136 'SW': render.MovementAnimatedRenderer(
137 [self._get_image('werewolf_SW_1.png'),
138 self._get_image('werewolf_SW_2.png')], 3),
139 'SE': render.MovementAnimatedRenderer(
140 [self._get_image('werewolf_SW_1.png', FLIP_H),
141 self._get_image('werewolf_SW_2.png', FLIP_H)], 3),
142 }),
143 })
144
145 @classmethod
146 def from_saved_state(cls, saved_state):
147 """Create an instance from the provided serialised state.
148 """
149 obj = cls()
150 # TODO: Update from saved state.
151 return obj
152
153 def get_render_angle(self):
154 # No image rotation when rendering, please.
155 return 0
156
157 def get_facing_direction(self):
158 # It's easier to work with a vector than an angle here.
159 vec = Vec2d.unit()
160 vec.angle = self.angle
161 # We probably don't have exactly -1, 0, or 1 here.
162 x = int(round(vec.x))
163 y = int(round(vec.y))
164
165 return {
166 (0, 1): 'N',
167 (0, -1): 'S',
168 (-1, 0): 'W',
169 (1, 0): 'E',
170 (1, 1): 'NE',
171 (1, -1): 'SE',
172 (-1, 1): 'NW',
173 (-1, -1): 'SW',
174 }[(x, y)]
175
176 def go_werewolf(self):
177 self.physicser.switch_form(self.form, self.WOLF_FORM)
178 self.form = self.WOLF_FORM
179 self.impulse_factor = 4000
180
181 def go_human(self):
182 self.physicser.switch_form(self.form, self.HUMAN_FORM)
183 self.form = self.HUMAN_FORM
184 self.impulse_factor = 500
185
186 def set_direction(self, dx, dy):
187 if (dx, dy) == (0, 0):
188 self.is_moving = False
189 return
190 self.is_moving = True
191 self.angle = pymunk.Vec2d((dx, dy)).angle
192 self.physicser.apply_impulse(
193 (dx * self.impulse_factor, dy * self.impulse_factor))
194
195 def set_position(self, position):
196 self.physicser.position = position
197
198 def copy_state(self, old_protagonist):
199 self.physicser.position = old_protagonist.physicser.position
200 self.physicser.switch_form(self.form, old_protagonist.form)
201 self.impulse_factor = old_protagonist.impulse_factor
202 self.form = old_protagonist.form
203 self.angle = old_protagonist.angle
204 self.inventory = old_protagonist.inventory
205
206 def toggle_form(self):
207 if self.form == self.WOLF_FORM:
208 self.go_human()
209 else:
210 self.go_werewolf()
211
212 def act_on(self, target):
213 """Perform an action on the target.
214 """
215 # TODO: Decide how best to do this.
216 pass
217
218 def attack(self):
219 """Attempt to hurt something.
220 """
221 pass
222
223 def in_wolf_form(self):
224 return self.form == self.WOLF_FORM
225
226 def in_human_form(self):
227 return self.form == self.HUMAN_FORM
228
229 def has_item(self, item):
230 return item in self.inventory
231
232 def environmental_movement(self, dx, dy):
233 if (dx, dy) == (0, 0):
234 return
235 self.physicser.apply_impulse((dx, dy))
236
237 def get_health_level(self):
238 """Return current health level
239 """
240 return self.health_level
241
242 def lose_health(self, amount):
243 if self.in_human_form():
244 self.health_level -= amount
245 else:
246 self.health_level -= amount / WEREWOLF_SOAK_FACTOR
247 if self.health_level < PROTAGONIST_HEALTH_MIN_LEVEL:
248 self.health_level = PROTAGONIST_HEALTH_MIN_LEVEL
249
250 def gain_health(self, amount):
251 self.health_level += amount
252 if self.health_level > PROTAGONIST_HEALTH_MAX_LEVEL:
253 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
Note: See TracBrowser for help on using the repository browser.