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
Line 
1import pymunk
2import pymunk.pygame_util
3from pymunk.vec2d import Vec2d
4
5from nagslang import render
6from nagslang.constants import COLLISION_TYPE_PLAYER, ZORDER_MID, \
7 WEREWOLF_SOAK_FACTOR, PROTAGONIST_HEALTH_MIN_LEVEL, \
8 PROTAGONIST_HEALTH_MAX_LEVEL
9from nagslang.events import FireEvent
10from nagslang.game_object import GameObject, Physicser, make_body
11from nagslang.mutators import FLIP_H
12from nagslang.resources import resources
13
14
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
31class ProtagonistFormSelectionRenderer(render.RendererSelectionRenderer):
32 def select_renderer(self):
33 return self.game_object.form
34
35
36class Protagonist(GameObject):
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
45 def __init__(self, space, position):
46 physicser = self._make_physics(space, position)
47 renderer = self._make_renderer()
48 self.inventory = {}
49 self.form = self.HUMAN_FORM
50 self.angle = 0
51 self.is_moving = False
52
53 super(Protagonist, self).__init__(physicser, renderer)
54 self.zorder = ZORDER_MID
55
56 self.health_level = 100
57
58 self.go_human()
59
60 def _make_physics(self, space, position):
61 body = make_body(10, pymunk.inf, position, 0.8)
62 body.velocity_limit = 1000
63
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,
70 }
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 })
84
85 def _get_image(self, name, *transforms):
86 return resources.get_image('creatures', name, transforms=transforms)
87
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
93 def _make_renderer(self):
94 return ProtagonistFormSelectionRenderer({
95 self.HUMAN_FORM: render.FacingSelectionRenderer(
96 {
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),
121 }),
122 self.WOLF_FORM: render.FacingSelectionRenderer(
123 {
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),
148 }),
149 })
150
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
159 def get_render_angle(self):
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)]
181
182 def go_werewolf(self):
183 self.physicser.switch_form(self.form, self.WOLF_FORM)
184 self.form = self.WOLF_FORM
185 self.impulse_factor = 4000
186
187 def go_human(self):
188 self.physicser.switch_form(self.form, self.HUMAN_FORM)
189 self.form = self.HUMAN_FORM
190 self.impulse_factor = 500
191
192 def set_direction(self, dx, dy):
193 if (dx, dy) == (0, 0):
194 self.is_moving = False
195 return
196 self.is_moving = True
197 self.angle = pymunk.Vec2d((dx, dy)).angle
198 self.physicser.apply_impulse(
199 (dx * self.impulse_factor, dy * self.impulse_factor))
200
201 def set_position(self, position):
202 self.physicser.position = position
203
204 def copy_state(self, old_protagonist):
205 self.physicser.position = old_protagonist.physicser.position
206 self.physicser.switch_form(self.form, old_protagonist.form)
207 self.impulse_factor = old_protagonist.impulse_factor
208 self.form = old_protagonist.form
209 self.angle = old_protagonist.angle
210 self.inventory = old_protagonist.inventory
211
212 def toggle_form(self):
213 if self.form == self.WOLF_FORM:
214 self.go_human()
215 else:
216 self.go_werewolf()
217
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 """
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
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
244
245 def environmental_movement(self, dx, dy):
246 if (dx, dy) == (0, 0):
247 return
248 self.physicser.apply_impulse((dx, dy))
249
250 def get_health_level(self):
251 """Return current health level
252 """
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:
266 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
Note: See TracBrowser for help on using the repository browser.