source: nagslang/protagonist.py@ 285:ef4f6375c0b5

Last change on this file since 285:ef4f6375c0b5 was 285:ef4f6375c0b5, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Basic death

File size: 10.4 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
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 # It's easier to work with a vector than an angle here.
174 vec = Vec2d.unit()
175 vec.angle = self.angle
176 # We probably don't have exactly -1, 0, or 1 here.
177 x = int(round(vec.x))
178 y = int(round(vec.y))
179
180 return {
181 (0, 1): 'N',
182 (0, -1): 'S',
183 (-1, 0): 'W',
184 (1, 0): 'E',
185 (1, 1): 'NE',
186 (1, -1): 'SE',
187 (-1, 1): 'NW',
188 (-1, -1): 'SW',
189 }[(x, y)]
190
191 def go_werewolf(self):
192 self.physicser.switch_form(self.form, self.WOLF_FORM)
193 self.form = self.WOLF_FORM
194 self.impulse_factor = 4000
195
196 def go_human(self):
197 self.physicser.switch_form(self.form, self.HUMAN_FORM)
198 self.form = self.HUMAN_FORM
199 self.impulse_factor = 500
200
201 def set_direction(self, dx, dy):
202 if (dx, dy) == (0, 0):
203 self.is_moving = False
204 return
205 self.is_moving = True
206 self.angle = pymunk.Vec2d((dx, dy)).angle
207 self.physicser.apply_impulse(
208 (dx * self.impulse_factor, dy * self.impulse_factor))
209
210 def set_position(self, position):
211 self.physicser.position = position
212
213 def copy_state(self, old_protagonist):
214 self.physicser.position = old_protagonist.physicser.position
215 self.physicser.switch_form(self.form, old_protagonist.form)
216 self.impulse_factor = old_protagonist.impulse_factor
217 self.form = old_protagonist.form
218 self.angle = old_protagonist.angle
219 self.inventory = old_protagonist.inventory
220
221 def toggle_form(self):
222 if self.form == self.WOLF_FORM:
223 self.go_human()
224 else:
225 self.go_werewolf()
226
227 def get_current_interactible(self):
228 for shape in self.get_space().shape_query(self.get_shape()):
229 if shape.collision_type in NON_GAME_OBJECT_COLLIDERS:
230 # No game object here.
231 continue
232 interactible = shape.physicser.game_object.interactible
233 if interactible is not None:
234 return interactible
235 return None
236
237 def perform_action(self):
238 """Perform an action on the target.
239 """
240 interactible = self.get_current_interactible()
241 if interactible is None:
242 # Nothing to interact with.
243 return
244 action = interactible.select_action(self)
245 if action is None:
246 # Nothing to do with it.
247 return
248 return action.perform(self)
249
250 def attack(self):
251 """Attempt to hurt something.
252 """
253 if self.in_wolf_form():
254 pass
255 else:
256 vec = Vec2d.unit()
257 vec.angle = self.angle
258 vec.length = 100
259 FireEvent.post(self.physicser.position, vec)
260 print "Biff", self.physicser.position, vec
261
262 def in_wolf_form(self):
263 return self.form == self.WOLF_FORM
264
265 def in_human_form(self):
266 return self.form == self.HUMAN_FORM
267
268 def has_item(self, item):
269 return item in self.inventory
270
271 def environmental_movement(self, dx, dy):
272 if (dx, dy) == (0, 0):
273 return
274 self.physicser.apply_impulse((dx, dy))
275
276 def get_health_level(self):
277 """Return current health level
278 """
279 return self.health_level
280
281 def die(self):
282 # Handle player death - may be called due to other reasons
283 # than zero health
284 ScreenChange.post('dead')
285
286 def lose_health(self, amount):
287 if self.in_human_form():
288 self.health_level -= amount
289 else:
290 self.health_level -= amount / WEREWOLF_SOAK_FACTOR
291 if self.health_level <= PROTAGONIST_HEALTH_MIN_LEVEL:
292 self.die()
293
294 def gain_health(self, amount):
295 self.health_level += amount
296 if self.health_level > PROTAGONIST_HEALTH_MAX_LEVEL:
297 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
Note: See TracBrowser for help on using the repository browser.