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

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

More interesting claw attack.

File size: 11.0 KB
RevLine 
[65]1import pymunk
2import pymunk.pygame_util
3
[207]4from nagslang import render
[281]5from nagslang.constants import (
6 COLLISION_TYPE_PLAYER, ZORDER_MID, WEREWOLF_SOAK_FACTOR,
7 PROTAGONIST_HEALTH_MIN_LEVEL, PROTAGONIST_HEALTH_MAX_LEVEL,
[333]8 NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, CLAW_DAMAGE)
[313]9from nagslang.events import FireEvent, ClawEvent
[215]10from nagslang.game_object import GameObject, Physicser, make_body
[76]11from nagslang.mutators import FLIP_H
[65]12from nagslang.resources import resources
[285]13from nagslang.events import ScreenChange
[334]14from nagslang.utils import vec_from_angle, vec_with_length
[28]15
16
[215]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
[218]33class ProtagonistFormSelectionRenderer(render.RendererSelectionRenderer):
34 def select_renderer(self):
[229]35 return self.game_object.form
[218]36
37
[65]38class Protagonist(GameObject):
[28]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
[277]47 def __init__(self, space, world, position):
[215]48 physicser = self._make_physics(space, position)
[218]49 renderer = self._make_renderer()
[28]50 self.inventory = {}
51 self.form = self.HUMAN_FORM
[218]52 self.angle = 0
53 self.is_moving = False
[277]54 self.world = world
[28]55
[218]56 super(Protagonist, self).__init__(physicser, renderer)
[107]57 self.zorder = ZORDER_MID
[65]58
[246]59 self.health_level = 100
60
[65]61 self.go_human()
62
[215]63 def _make_physics(self, space, position):
64 body = make_body(10, pymunk.inf, position, 0.8)
65 body.velocity_limit = 1000
[65]66
[215]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,
[93]73 }
[215]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 })
[65]87
[66]88 def _get_image(self, name, *transforms):
89 return resources.get_image('creatures', name, transforms=transforms)
90
[276]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
[285]96 def reset(self):
97 self.health_level = 100
98 self.is_moving = False
99
100 self.go_human()
101
[218]102 def _make_renderer(self):
103 return ProtagonistFormSelectionRenderer({
[217]104 self.HUMAN_FORM: render.FacingSelectionRenderer(
105 {
[229]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),
[217]130 }),
131 self.WOLF_FORM: render.FacingSelectionRenderer(
132 {
[229]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),
[217]157 }),
[218]158 })
[65]159
[28]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
[93]168 def get_render_angle(self):
[229]169 # No image rotation when rendering, please.
170 return 0
171
172 def get_facing_direction(self):
[334]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)
[229]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)]
[93]191
[65]192 def go_werewolf(self):
[215]193 self.physicser.switch_form(self.form, self.WOLF_FORM)
[65]194 self.form = self.WOLF_FORM
195 self.impulse_factor = 4000
[215]196
[65]197 def go_human(self):
[215]198 self.physicser.switch_form(self.form, self.HUMAN_FORM)
[65]199 self.form = self.HUMAN_FORM
200 self.impulse_factor = 500
[215]201
[65]202 def set_direction(self, dx, dy):
203 if (dx, dy) == (0, 0):
[218]204 self.is_moving = False
[65]205 return
[218]206 self.is_moving = True
[334]207 vec = vec_with_length((dx, dy), self.impulse_factor)
208 self.angle = vec.angle
209 self.physicser.apply_impulse(vec)
[65]210
[179]211 def set_position(self, position):
[215]212 self.physicser.position = position
[179]213
214 def copy_state(self, old_protagonist):
[215]215 self.physicser.position = old_protagonist.physicser.position
216 self.physicser.switch_form(self.form, old_protagonist.form)
[217]217 self.impulse_factor = old_protagonist.impulse_factor
[179]218 self.form = old_protagonist.form
219 self.angle = old_protagonist.angle
220 self.inventory = old_protagonist.inventory
221
[65]222 def toggle_form(self):
223 if self.form == self.WOLF_FORM:
224 self.go_human()
225 else:
226 self.go_werewolf()
227
[281]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):
[28]239 """Perform an action on the target.
240 """
[281]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)
[28]250
251 def attack(self):
252 """Attempt to hurt something.
253 """
[265]254 if self.in_wolf_form():
[312]255 self.claw()
[265]256 else:
[286]257 self.shoot()
258
259 def shoot(self):
260 if not self.has_item('gun'):
261 return
[334]262 vec = vec_from_angle(self.angle, 1000)
[333]263 FireEvent.post(
264 self.physicser.position, vec, BULLET_DAMAGE, COLLISION_TYPE_PLAYER)
[28]265
[312]266 def claw(self):
[335]267 vec = vec_from_angle(self.angle, 300)
[333]268 ClawEvent.post(self.physicser.position, vec, CLAW_DAMAGE)
[312]269
[28]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
[73]278
279 def environmental_movement(self, dx, dy):
280 if (dx, dy) == (0, 0):
281 return
[215]282 self.physicser.apply_impulse((dx, dy))
[246]283
284 def get_health_level(self):
[249]285 """Return current health level
286 """
[246]287 return self.health_level
288
[285]289 def die(self):
290 # Handle player death - may be called due to other reasons
291 # than zero health
292 ScreenChange.post('dead')
293
[246]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
[285]299 if self.health_level <= PROTAGONIST_HEALTH_MIN_LEVEL:
300 self.die()
[246]301
302 def gain_health(self, amount):
303 self.health_level += amount
304 if self.health_level > PROTAGONIST_HEALTH_MAX_LEVEL:
[248]305 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
[309]306
307 def update(self, dt):
[333]308 if int(self.lifetime + dt) > int(self.lifetime):
309 if self.form == self.WOLF_FORM:
310 self.gain_health(1)
[309]311 super(Protagonist, self).update(dt)
Note: See TracBrowser for help on using the repository browser.