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
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
14
15
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
32class ProtagonistFormSelectionRenderer(render.RendererSelectionRenderer):
33 def select_renderer(self):
34 return self.game_object.form
35
36
37class Protagonist(GameObject):
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
46 def __init__(self, space, world, position):
47 physicser = self._make_physics(space, position)
48 renderer = self._make_renderer()
49 self.inventory = {}
50 self.form = self.HUMAN_FORM
51 self.angle = 0
52 self.is_moving = False
53 self.world = world
54
55 super(Protagonist, self).__init__(physicser, renderer)
56 self.zorder = ZORDER_MID
57
58 self.health_level = 100
59
60 self.go_human()
61
62 def _make_physics(self, space, position):
63 body = make_body(10, pymunk.inf, position, 0.8)
64 body.velocity_limit = 1000
65
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,
72 }
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 })
86
87 def _get_image(self, name, *transforms):
88 return resources.get_image('creatures', name, transforms=transforms)
89
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
95 def _make_renderer(self):
96 return ProtagonistFormSelectionRenderer({
97 self.HUMAN_FORM: render.FacingSelectionRenderer(
98 {
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),
123 }),
124 self.WOLF_FORM: render.FacingSelectionRenderer(
125 {
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),
150 }),
151 })
152
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
161 def get_render_angle(self):
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)]
183
184 def go_werewolf(self):
185 self.physicser.switch_form(self.form, self.WOLF_FORM)
186 self.form = self.WOLF_FORM
187 self.impulse_factor = 4000
188
189 def go_human(self):
190 self.physicser.switch_form(self.form, self.HUMAN_FORM)
191 self.form = self.HUMAN_FORM
192 self.impulse_factor = 500
193
194 def set_direction(self, dx, dy):
195 if (dx, dy) == (0, 0):
196 self.is_moving = False
197 return
198 self.is_moving = True
199 self.angle = pymunk.Vec2d((dx, dy)).angle
200 self.physicser.apply_impulse(
201 (dx * self.impulse_factor, dy * self.impulse_factor))
202
203 def set_position(self, position):
204 self.physicser.position = position
205
206 def copy_state(self, old_protagonist):
207 self.physicser.position = old_protagonist.physicser.position
208 self.physicser.switch_form(self.form, old_protagonist.form)
209 self.impulse_factor = old_protagonist.impulse_factor
210 self.form = old_protagonist.form
211 self.angle = old_protagonist.angle
212 self.inventory = old_protagonist.inventory
213
214 def toggle_form(self):
215 if self.form == self.WOLF_FORM:
216 self.go_human()
217 else:
218 self.go_werewolf()
219
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):
231 """Perform an action on the target.
232 """
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)
242
243 def attack(self):
244 """Attempt to hurt something.
245 """
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
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
263
264 def environmental_movement(self, dx, dy):
265 if (dx, dy) == (0, 0):
266 return
267 self.physicser.apply_impulse((dx, dy))
268
269 def get_health_level(self):
270 """Return current health level
271 """
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:
285 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
Note: See TracBrowser for help on using the repository browser.