source: nagslang/protagonist.py@ 335:78b805549b4e

Last change on this file since 335:78b805549b4e was 335:78b805549b4e, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

More interesting claw attack.

File size: 11.0 KB
Line 
1import pymunk
2import pymunk.pygame_util
3
4from nagslang import render
5from nagslang.constants import (
6 COLLISION_TYPE_PLAYER, ZORDER_MID, WEREWOLF_SOAK_FACTOR,
7 PROTAGONIST_HEALTH_MIN_LEVEL, PROTAGONIST_HEALTH_MAX_LEVEL,
8 NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, CLAW_DAMAGE)
9from nagslang.events import FireEvent, ClawEvent
10from nagslang.game_object import GameObject, Physicser, make_body
11from nagslang.mutators import FLIP_H
12from nagslang.resources import resources
13from nagslang.events import ScreenChange
14from nagslang.utils import vec_from_angle, vec_with_length
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
63 def _make_physics(self, space, position):
64 body = make_body(10, pymunk.inf, position, 0.8)
65 body.velocity_limit = 1000
66
67 human = pymunk.Poly(body, [(-15, -30), (15, -30), (15, 30), (-15, 30)])
68 human.elasticity = 1.0
69 human.collision_type = COLLISION_TYPE_PLAYER
70 human.protagonist_body_props = {
71 'mass': 10,
72 'damping': 0.8,
73 }
74
75 wolf = pymunk.Circle(body, 30)
76 wolf.elasticity = 1.0
77 wolf.collision_type = COLLISION_TYPE_PLAYER
78 wolf.protagonist_body_props = {
79 'mass': 100,
80 'damping': 0.9,
81 }
82
83 return ProtagonistPhysicser(space, {
84 self.HUMAN_FORM: human,
85 self.WOLF_FORM: wolf,
86 })
87
88 def _get_image(self, name, *transforms):
89 return resources.get_image('creatures', name, transforms=transforms)
90
91 def change_space(self, new_space):
92 self.physicser.remove_from_space()
93 self.physicser.set_space(new_space)
94 self.physicser.add_to_space()
95
96 def reset(self):
97 self.health_level = 100
98 self.is_moving = False
99
100 self.go_human()
101
102 def _make_renderer(self):
103 return ProtagonistFormSelectionRenderer({
104 self.HUMAN_FORM: render.FacingSelectionRenderer(
105 {
106 'N': render.MovementAnimatedRenderer(
107 [self._get_image('human_N_1.png'),
108 self._get_image('human_N_2.png')], 3),
109 'S': render.MovementAnimatedRenderer(
110 [self._get_image('human_S_1.png'),
111 self._get_image('human_S_2.png')], 3),
112 'W': render.MovementAnimatedRenderer(
113 [self._get_image('human_W_1.png'),
114 self._get_image('human_W_2.png')], 3),
115 'E': render.MovementAnimatedRenderer(
116 [self._get_image('human_W_1.png', FLIP_H),
117 self._get_image('human_W_2.png', FLIP_H)], 3),
118 'NW': render.MovementAnimatedRenderer(
119 [self._get_image('human_NW_1.png'),
120 self._get_image('human_NW_2.png')], 3),
121 'NE': render.MovementAnimatedRenderer(
122 [self._get_image('human_NW_1.png', FLIP_H),
123 self._get_image('human_NW_2.png', FLIP_H)], 3),
124 'SW': render.MovementAnimatedRenderer(
125 [self._get_image('human_SW_1.png'),
126 self._get_image('human_SW_2.png')], 3),
127 'SE': render.MovementAnimatedRenderer(
128 [self._get_image('human_SW_1.png', FLIP_H),
129 self._get_image('human_SW_2.png', FLIP_H)], 3),
130 }),
131 self.WOLF_FORM: render.FacingSelectionRenderer(
132 {
133 'N': render.MovementAnimatedRenderer(
134 [self._get_image('werewolf_N_1.png'),
135 self._get_image('werewolf_N_2.png')], 3),
136 'S': render.MovementAnimatedRenderer(
137 [self._get_image('werewolf_S_1.png'),
138 self._get_image('werewolf_S_2.png')], 3),
139 'W': render.MovementAnimatedRenderer(
140 [self._get_image('werewolf_W_1.png'),
141 self._get_image('werewolf_W_2.png')], 3),
142 'E': render.MovementAnimatedRenderer(
143 [self._get_image('werewolf_W_1.png', FLIP_H),
144 self._get_image('werewolf_W_2.png', FLIP_H)], 3),
145 'NW': render.MovementAnimatedRenderer(
146 [self._get_image('werewolf_NW_1.png'),
147 self._get_image('werewolf_NW_2.png')], 3),
148 'NE': render.MovementAnimatedRenderer(
149 [self._get_image('werewolf_NW_1.png', FLIP_H),
150 self._get_image('werewolf_NW_2.png', FLIP_H)], 3),
151 'SW': render.MovementAnimatedRenderer(
152 [self._get_image('werewolf_SW_1.png'),
153 self._get_image('werewolf_SW_2.png')], 3),
154 'SE': render.MovementAnimatedRenderer(
155 [self._get_image('werewolf_SW_1.png', FLIP_H),
156 self._get_image('werewolf_SW_2.png', FLIP_H)], 3),
157 }),
158 })
159
160 @classmethod
161 def from_saved_state(cls, saved_state):
162 """Create an instance from the provided serialised state.
163 """
164 obj = cls()
165 # TODO: Update from saved state.
166 return obj
167
168 def get_render_angle(self):
169 # No image rotation when rendering, please.
170 return 0
171
172 def get_facing_direction(self):
173 # Our angle is quantised to 45 degree intervals, so possible values for
174 # x and y in a unit vector are +/-(0, sqrt(2)/2, 1) with some floating
175 # point imprecision. Rounding will normalise these to (-1.0, 0.0, 1.0)
176 # which we can safely turn into integers and use as dict keys.
177 vec = vec_from_angle(self.angle)
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 vec = vec_with_length((dx, dy), self.impulse_factor)
208 self.angle = vec.angle
209 self.physicser.apply_impulse(vec)
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 self.claw()
256 else:
257 self.shoot()
258
259 def shoot(self):
260 if not self.has_item('gun'):
261 return
262 vec = vec_from_angle(self.angle, 1000)
263 FireEvent.post(
264 self.physicser.position, vec, BULLET_DAMAGE, COLLISION_TYPE_PLAYER)
265
266 def claw(self):
267 vec = vec_from_angle(self.angle, 300)
268 ClawEvent.post(self.physicser.position, vec, CLAW_DAMAGE)
269
270 def in_wolf_form(self):
271 return self.form == self.WOLF_FORM
272
273 def in_human_form(self):
274 return self.form == self.HUMAN_FORM
275
276 def has_item(self, item):
277 return item in self.inventory
278
279 def environmental_movement(self, dx, dy):
280 if (dx, dy) == (0, 0):
281 return
282 self.physicser.apply_impulse((dx, dy))
283
284 def get_health_level(self):
285 """Return current health level
286 """
287 return self.health_level
288
289 def die(self):
290 # Handle player death - may be called due to other reasons
291 # than zero health
292 ScreenChange.post('dead')
293
294 def lose_health(self, amount):
295 if self.in_human_form():
296 self.health_level -= amount
297 else:
298 self.health_level -= amount / WEREWOLF_SOAK_FACTOR
299 if self.health_level <= PROTAGONIST_HEALTH_MIN_LEVEL:
300 self.die()
301
302 def gain_health(self, amount):
303 self.health_level += amount
304 if self.health_level > PROTAGONIST_HEALTH_MAX_LEVEL:
305 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
306
307 def update(self, dt):
308 if int(self.lifetime + dt) > int(self.lifetime):
309 if self.form == self.WOLF_FORM:
310 self.gain_health(1)
311 super(Protagonist, self).update(dt)
Note: See TracBrowser for help on using the repository browser.