source: nagslang/protagonist.py@ 276:3153196517fc

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

Move protagonist to the world

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