source: nagslang/protagonist.py@ 281:9b56e954c674

Last change on this file since 281:9b56e954c674 was 281:9b56e954c674, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Protagonist actions, now required for operating doors.

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