source: nagslang/protagonist.py@ 309:2b0494b3ce6a

Last change on this file since 309:2b0494b3ce6a was 309:2b0494b3ce6a, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Werewolf form heals every second.

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